I have two web services based on Spring boot, REST and MVC. The first service(DesignService) project calls the second service(UserService) project passing a email as parameter and gets the user object(User.java) back. How can I handle if no user is found.
DesignService project
repository.java
@Autowired
RestTemplate restTemplate;
getUser(String email){
restTemplate.postForObject("url for User service",email,User.class); //calls User service for Object[1]
}
UserService
UserController.java
@RequestMapping()
public ResponseEntity<User> getUser(@RequestBody String email){
User user = repository.getUser(email); //return a user object from DB
return new ResponseEntity<User>(user);[2]
}
Repository.java
public User getUser(String email){
//query db for user
return user; //what to return if no user is found [3]
}
* excuse me if am wrong in some syntax as I have typed it directly .
Return null, or if you're on Java 8, an empty Optional
If no user is found, return an empty Response with a 404 (NOT_FOUND) status, ie
new ResponseEntity(HttpStatus.NOT_FOUND);
Finally, why are you POSTing to an endpoint that only returns a resource? You should be doing a GET, and from the looks of it, your request mapping should only support GET (method = RequestMethod.GET).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.