SlideShare a Scribd company logo
1 of 9
Download to read offline
Creating a WhatsApp Clone - Part XIV
Next we’ll jump to the webservice package
@Controller
@RequestMapping("/user")
@RestController
public class UserWebService {
@Autowired
private UserService users;
@ExceptionHandler(LoginException.class)
@ResponseStatus(value=HttpStatus.FORBIDDEN)
public @ResponseBody
ErrorDAO handleLoginException(LoginException e) {
return new ErrorDAO(e.getMessage(), 0);
}
@ExceptionHandler(SignupException.class)
@ResponseStatus(value=HttpStatus.FORBIDDEN)
public @ResponseBody
ErrorDAO handleSignupException(SignupException e) {
return new ErrorDAO(e.getMessage(), 0);
}
UserWebService
This is pretty much identical to the facebook clone app

We create a webservice mapping for user services. Technically we could have broken this down to more webservices but there is no real reason as we don't have that
much functionality here.
@Controller
@RequestMapping("/user")
@RestController
public class UserWebService {
@Autowired
private UserService users;
@ExceptionHandler(LoginException.class)
@ResponseStatus(value=HttpStatus.FORBIDDEN)
public @ResponseBody
ErrorDAO handleLoginException(LoginException e) {
return new ErrorDAO(e.getMessage(), 0);
}
@ExceptionHandler(SignupException.class)
@ResponseStatus(value=HttpStatus.FORBIDDEN)
public @ResponseBody
ErrorDAO handleSignupException(SignupException e) {
return new ErrorDAO(e.getMessage(), 0);
}
@RequestMapping(method=RequestMethod.POST, value="/login")
public @ResponseBody
UserWebService
This is a thin wrapper around UserService that contains no actual functionality. It only translates the logic in that class to web calls
@Controller
@RequestMapping("/user")
@RestController
public class UserWebService {
@Autowired
private UserService users;
@ExceptionHandler(LoginException.class)
@ResponseStatus(value=HttpStatus.FORBIDDEN)
public @ResponseBody
ErrorDAO handleLoginException(LoginException e) {
return new ErrorDAO(e.getMessage(), 0);
}
@ExceptionHandler(SignupException.class)
@ResponseStatus(value=HttpStatus.FORBIDDEN)
public @ResponseBody
ErrorDAO handleSignupException(SignupException e) {
return new ErrorDAO(e.getMessage(), 0);
}
@RequestMapping(method=RequestMethod.POST, value="/login")
public @ResponseBody
UserWebService
If an exception is thrown in this class it’s implicitly translated to an ErrorDAO which is translated to an error JSON
@ExceptionHandler(SignupException.class)
@ResponseStatus(value=HttpStatus.FORBIDDEN)
public @ResponseBody
ErrorDAO handleSignupException(SignupException e) {
return new ErrorDAO(e.getMessage(), 0);
}
@RequestMapping(method=RequestMethod.POST, value="/login")
public @ResponseBody
UserDAO login(@RequestHeader String auth, @RequestBody UserDAO u)
throws LoginException {
return users.login(u.getPhone(), auth);
}
@RequestMapping(method=RequestMethod.POST, value="/signup")
public @ResponseBody
UserDAO signup(@RequestBody UserDAO user)
throws SignupException {
return users.signup(user);
}
@RequestMapping(method=RequestMethod.GET, value="/verify")
public @ResponseBody
UserWebService
Login and signup are almost identical with the small exception that login expects an auth header value. Both are simple post methods that return the DAO object as
JSON body to the client
UserDAO signup(@RequestBody UserDAO user)
throws SignupException {
return users.signup(user);
}
@RequestMapping(method=RequestMethod.GET, value="/verify")
public @ResponseBody
String verifyPhone(
@RequestParam String userId,
@RequestParam String code) {
if(users.verifyPhone(userId, code)) {
return "OK";
}
return "ERROR";
}
@RequestMapping(method=RequestMethod.POST, value="/update")
public String update(@RequestHeader String auth,
@RequestBody UserDAO user) {
users.update(auth, user);
return "OK";
}
@RequestMapping(value="/avatar/{id:.+}", method=RequestMethod.GET)
UserWebService
Verify and update return string values to indicate that they succeeded
users.update(auth, user);
return "OK";
}
@RequestMapping(value="/avatar/{id:.+}", method=RequestMethod.GET)
public ResponseEntity<byte[]> getAvatar(
@PathVariable("id") String id) {
byte[] av = users.getAvatar(id);
if(av != null) {
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).
body(av);
}
return ResponseEntity.notFound().build();
}
@RequestMapping(method=RequestMethod.GET, value="/set-avatar")
public String setAvatar(
@RequestHeader String auth,
@RequestParam String userId,
@RequestParam String mediaId) {
users.setAvatar(auth, userId, mediaId);
return "OK";
}
UserWebService
I added the implementation to set/get avatar via URL but this isn’t mapped in the client side. This can probably be implemented in the same way as the facebook clone
users.setAvatar(auth, userId, mediaId);
return "OK";
}
@RequestMapping(method=RequestMethod.GET, value="/findRegisteredUser")
public List<UserDAO> findRegisteredUser(String phone) {
UserDAO d = users.findRegisteredUser(phone);
if(d == null) {
return new ArrayList<>();
}
return Arrays.asList(d);
}
@RequestMapping(method=RequestMethod.GET,
value="/findRegisteredUserById")
public List<UserDAO> findRegisteredUserById(String id) {
UserDAO d = users.findRegisteredUserById(id);
if(d == null) {
return new ArrayList<>();
}
return Arrays.asList(d);
}
@RequestMapping(method=RequestMethod.POST, value="/sendMessage")
UserWebService
These methods return their result as an array of one element or as a zero length array. Since there is no way in JSON to return null like the business logic method does.
So we return a blank array list or a list with one element.
return new ArrayList<>();
}
return Arrays.asList(d);
}
@RequestMapping(method=RequestMethod.POST, value="/sendMessage")
public MessageDAO sendMessage(@RequestHeader String auth,
@RequestBody MessageDAO m) {
return users.sendMessage(m);
}
@RequestMapping(method=RequestMethod.POST, value="/ackMessage")
public void ackMessage(@RequestHeader String auth,
@RequestBody String id) {
users.ackMessage(id);
}
@RequestMapping(method=RequestMethod.GET, value="/updatePushKey")
public void updatePushKey(@RequestHeader String auth, String id,
String key) {
users.updatePushKey(auth, id, key);
}
}
UserWebService
And that’s the end of the class, the rest of the methods delegate directly to the user service bean.

More Related Content

Similar to Creating a Whatsapp Clone - Part XIV - Transcript.pdf

Similar to Creating a Whatsapp Clone - Part XIV - Transcript.pdf (20)

Creating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdfCreating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdf
 
Creating a Whatsapp Clone - Part XIII - Transcript.pdf
Creating a Whatsapp Clone - Part XIII - Transcript.pdfCreating a Whatsapp Clone - Part XIII - Transcript.pdf
Creating a Whatsapp Clone - Part XIII - Transcript.pdf
 
Creating a Facebook Clone - Part XLV - Transcript.pdf
Creating a Facebook Clone - Part XLV - Transcript.pdfCreating a Facebook Clone - Part XLV - Transcript.pdf
Creating a Facebook Clone - Part XLV - Transcript.pdf
 
Easy rest service using PHP reflection api
Easy rest service using PHP reflection apiEasy rest service using PHP reflection api
Easy rest service using PHP reflection api
 
Angular Tutorial Freshers and Experienced
Angular Tutorial Freshers and ExperiencedAngular Tutorial Freshers and Experienced
Angular Tutorial Freshers and Experienced
 
Crafting beautiful software
Crafting beautiful softwareCrafting beautiful software
Crafting beautiful software
 
Creating a Facebook Clone - Part XXVIII - Transcript.pdf
Creating a Facebook Clone - Part XXVIII - Transcript.pdfCreating a Facebook Clone - Part XXVIII - Transcript.pdf
Creating a Facebook Clone - Part XXVIII - Transcript.pdf
 
Mashing up JavaScript – Advanced Techniques for modern Web Apps
Mashing up JavaScript – Advanced Techniques for modern Web AppsMashing up JavaScript – Advanced Techniques for modern Web Apps
Mashing up JavaScript – Advanced Techniques for modern Web Apps
 
Mashing up JavaScript
Mashing up JavaScriptMashing up JavaScript
Mashing up JavaScript
 
Struts 2 + Spring
Struts 2 + SpringStruts 2 + Spring
Struts 2 + Spring
 
Testando API's de forma unitária mocando as dependências
Testando API's de forma unitária mocando as dependênciasTestando API's de forma unitária mocando as dependências
Testando API's de forma unitária mocando as dependências
 
Android Testing
Android TestingAndroid Testing
Android Testing
 
May 2010 - RestEasy
May 2010 - RestEasyMay 2010 - RestEasy
May 2010 - RestEasy
 
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobileJavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
 
Creating an Uber Clone - Part XXXIV - Transcript.pdf
Creating an Uber Clone - Part XXXIV - Transcript.pdfCreating an Uber Clone - Part XXXIV - Transcript.pdf
Creating an Uber Clone - Part XXXIV - Transcript.pdf
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hacking
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
Lesson_07_Spring_Security_Register_NEW.pdf
Lesson_07_Spring_Security_Register_NEW.pdfLesson_07_Spring_Security_Register_NEW.pdf
Lesson_07_Spring_Security_Register_NEW.pdf
 
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
 
What's Coming in Spring 3.0
What's Coming in Spring 3.0What's Coming in Spring 3.0
What's Coming in Spring 3.0
 

More from ShaiAlmog1

More from ShaiAlmog1 (20)

The Duck Teaches Learn to debug from the masters. Local to production- kill ...
The Duck Teaches  Learn to debug from the masters. Local to production- kill ...The Duck Teaches  Learn to debug from the masters. Local to production- kill ...
The Duck Teaches Learn to debug from the masters. Local to production- kill ...
 
create-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdfcreate-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdf
 
create-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdfcreate-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdf
 
create-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfcreate-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdf
 
create-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfcreate-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdf
 
create-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfcreate-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdf
 
create-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdfcreate-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdf
 
create-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfcreate-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdf
 
create-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdfcreate-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdf
 
create-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdfcreate-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdf
 
create-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdfcreate-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdf
 
create-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfcreate-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdf
 
create-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdfcreate-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdf
 
Creating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdfCreating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdf
 
Creating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdfCreating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdf
 
Creating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdfCreating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdf
 
Creating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdfCreating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdf
 
Creating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdfCreating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdf
 
Creating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdfCreating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdf
 
Creating a Whatsapp Clone - Part IX.pdf
Creating a Whatsapp Clone - Part IX.pdfCreating a Whatsapp Clone - Part IX.pdf
Creating a Whatsapp Clone - Part IX.pdf
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Creating a Whatsapp Clone - Part XIV - Transcript.pdf

  • 1. Creating a WhatsApp Clone - Part XIV Next we’ll jump to the webservice package
  • 2. @Controller @RequestMapping("/user") @RestController public class UserWebService { @Autowired private UserService users; @ExceptionHandler(LoginException.class) @ResponseStatus(value=HttpStatus.FORBIDDEN) public @ResponseBody ErrorDAO handleLoginException(LoginException e) { return new ErrorDAO(e.getMessage(), 0); } @ExceptionHandler(SignupException.class) @ResponseStatus(value=HttpStatus.FORBIDDEN) public @ResponseBody ErrorDAO handleSignupException(SignupException e) { return new ErrorDAO(e.getMessage(), 0); } UserWebService This is pretty much identical to the facebook clone app We create a webservice mapping for user services. Technically we could have broken this down to more webservices but there is no real reason as we don't have that much functionality here.
  • 3. @Controller @RequestMapping("/user") @RestController public class UserWebService { @Autowired private UserService users; @ExceptionHandler(LoginException.class) @ResponseStatus(value=HttpStatus.FORBIDDEN) public @ResponseBody ErrorDAO handleLoginException(LoginException e) { return new ErrorDAO(e.getMessage(), 0); } @ExceptionHandler(SignupException.class) @ResponseStatus(value=HttpStatus.FORBIDDEN) public @ResponseBody ErrorDAO handleSignupException(SignupException e) { return new ErrorDAO(e.getMessage(), 0); } @RequestMapping(method=RequestMethod.POST, value="/login") public @ResponseBody UserWebService This is a thin wrapper around UserService that contains no actual functionality. It only translates the logic in that class to web calls
  • 4. @Controller @RequestMapping("/user") @RestController public class UserWebService { @Autowired private UserService users; @ExceptionHandler(LoginException.class) @ResponseStatus(value=HttpStatus.FORBIDDEN) public @ResponseBody ErrorDAO handleLoginException(LoginException e) { return new ErrorDAO(e.getMessage(), 0); } @ExceptionHandler(SignupException.class) @ResponseStatus(value=HttpStatus.FORBIDDEN) public @ResponseBody ErrorDAO handleSignupException(SignupException e) { return new ErrorDAO(e.getMessage(), 0); } @RequestMapping(method=RequestMethod.POST, value="/login") public @ResponseBody UserWebService If an exception is thrown in this class it’s implicitly translated to an ErrorDAO which is translated to an error JSON
  • 5. @ExceptionHandler(SignupException.class) @ResponseStatus(value=HttpStatus.FORBIDDEN) public @ResponseBody ErrorDAO handleSignupException(SignupException e) { return new ErrorDAO(e.getMessage(), 0); } @RequestMapping(method=RequestMethod.POST, value="/login") public @ResponseBody UserDAO login(@RequestHeader String auth, @RequestBody UserDAO u) throws LoginException { return users.login(u.getPhone(), auth); } @RequestMapping(method=RequestMethod.POST, value="/signup") public @ResponseBody UserDAO signup(@RequestBody UserDAO user) throws SignupException { return users.signup(user); } @RequestMapping(method=RequestMethod.GET, value="/verify") public @ResponseBody UserWebService Login and signup are almost identical with the small exception that login expects an auth header value. Both are simple post methods that return the DAO object as JSON body to the client
  • 6. UserDAO signup(@RequestBody UserDAO user) throws SignupException { return users.signup(user); } @RequestMapping(method=RequestMethod.GET, value="/verify") public @ResponseBody String verifyPhone( @RequestParam String userId, @RequestParam String code) { if(users.verifyPhone(userId, code)) { return "OK"; } return "ERROR"; } @RequestMapping(method=RequestMethod.POST, value="/update") public String update(@RequestHeader String auth, @RequestBody UserDAO user) { users.update(auth, user); return "OK"; } @RequestMapping(value="/avatar/{id:.+}", method=RequestMethod.GET) UserWebService Verify and update return string values to indicate that they succeeded
  • 7. users.update(auth, user); return "OK"; } @RequestMapping(value="/avatar/{id:.+}", method=RequestMethod.GET) public ResponseEntity<byte[]> getAvatar( @PathVariable("id") String id) { byte[] av = users.getAvatar(id); if(av != null) { return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG). body(av); } return ResponseEntity.notFound().build(); } @RequestMapping(method=RequestMethod.GET, value="/set-avatar") public String setAvatar( @RequestHeader String auth, @RequestParam String userId, @RequestParam String mediaId) { users.setAvatar(auth, userId, mediaId); return "OK"; } UserWebService I added the implementation to set/get avatar via URL but this isn’t mapped in the client side. This can probably be implemented in the same way as the facebook clone
  • 8. users.setAvatar(auth, userId, mediaId); return "OK"; } @RequestMapping(method=RequestMethod.GET, value="/findRegisteredUser") public List<UserDAO> findRegisteredUser(String phone) { UserDAO d = users.findRegisteredUser(phone); if(d == null) { return new ArrayList<>(); } return Arrays.asList(d); } @RequestMapping(method=RequestMethod.GET, value="/findRegisteredUserById") public List<UserDAO> findRegisteredUserById(String id) { UserDAO d = users.findRegisteredUserById(id); if(d == null) { return new ArrayList<>(); } return Arrays.asList(d); } @RequestMapping(method=RequestMethod.POST, value="/sendMessage") UserWebService These methods return their result as an array of one element or as a zero length array. Since there is no way in JSON to return null like the business logic method does. So we return a blank array list or a list with one element.
  • 9. return new ArrayList<>(); } return Arrays.asList(d); } @RequestMapping(method=RequestMethod.POST, value="/sendMessage") public MessageDAO sendMessage(@RequestHeader String auth, @RequestBody MessageDAO m) { return users.sendMessage(m); } @RequestMapping(method=RequestMethod.POST, value="/ackMessage") public void ackMessage(@RequestHeader String auth, @RequestBody String id) { users.ackMessage(id); } @RequestMapping(method=RequestMethod.GET, value="/updatePushKey") public void updatePushKey(@RequestHeader String auth, String id, String key) { users.updatePushKey(auth, id, key); } } UserWebService And that’s the end of the class, the rest of the methods delegate directly to the user service bean.