Я создаю RESTAPI в Spring boot + JPA (MYSQL).Если я ищу для идентификатора пользователя, который не присутствует в БД, я получаю ответ ниже как ошибка:
{
"timestamp": 1545657449608,
"status": 500,
"error": "Internal Server Error",
"exception": "**org.springframework.http.converter.HttpMessageNotWritableException**",
"message": "Could not write content: Unable to find com.bookmyshow.user.User with id asd (through reference chain: com.bookmyshow.user.User_$$_jvst809_0[\"username\"]); nested exception is **com.fasterxml.jackson.databind.JsonMappingException**: Unable to find com.bookmyshow.user.User with id asd (through reference chain: com.bookmyshow.user.User_$$_jvst809_0[\"username\"])",
"path": "/users/asd"
}
UserRegistrationController
package com.bookmyshow.user;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserRegistrationController {
@Autowired
private UserDAO userDAO;
@RequestMapping("/users/{id}")
public ResponseEntity<?> getUser(@PathVariable String id) {
User user= userDAO.getUser(id);
if(user==null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok().body(user);
}
Пользователь DAO
package com.bookmyshow.user;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserDAO {
@Autowired
UserRepository userRepository;
public User getUser(String id) {
System.out.println("USERDAO:"+id);
return userRepository.getOne(id);
}