英文:
Include exception message in JSON error response
问题
如果电子邮件地址已存在,则抛出异常并附带消息("消息:" + tempEmailId + "的用户已存在")。当我在Postman中进行测试时,我无法获取异常消息。你能帮我解决一下吗?问题出在哪里?
控制器类:
@RestController
public class RegistrationController {
@Autowired
private RegistrationService service;
@PostMapping("/registeruser")
public User registerUser(@RequestBody User user) throws Exception {
String tempEmailId = user.getEmailId();
if (tempEmailId != null && !"".equals(tempEmailId)) {
User userObject = service.fetchUserByEmailId(tempEmailId);
if (userObject != null) {
throw new Exception("用户" + tempEmailId + "已存在");
}
}
User userObject = null;
userObject = service.saveUser(user);
return userObject;
}
}
存储库:
public interface RegistrationRepository extends JpaRepository<User, Integer> {
public User findByEmailId(String emailId); // 在这里声明
}
服务:
@Service
public class RegistrationService {
@Autowired
private RegistrationRepository repo;
public User saveUser(User user) {
return repo.save(user);
}
public User fetchUserByEmailId(String email) {
return repo.findByEmailId(email);
}
}
英文:
If email address already exists then throw an exception with a message("message:"User with "+tempEmailId+" already exists"). I don't get my exception message when I test in postman. Can you please help me ? Where is the issue?
Controller class:
@RestController
public class RegistrationController {
@Autowired
private RegistrationService service;
@PostMapping("/registeruser")
public User registerUser(@RequestBody User user) throws Exception {
String tempEmailId = user.getEmailId();
if(tempEmailId !=null && !"".equals(tempEmailId)) {
User userObject = service.fetchUserByEmailId(tempEmailId);
if(userObject!=null) {
throw new Exception("User with "+tempEmailId+" is already exist");
}
}
User userObject = null;
userObject = service.saveUser(user);
return userObject;
}
}
Repository:
public interface RegistrationRepository extends JpaRepository<User, Integer> {
public User findByEmailId(String emailId); // Here we declare
}
Service:
@Service
public class RegistrationService {
@Autowired
private RegistrationRepository repo;
public User saveUser(User user) {
return repo.save(user);
}
public User fetchUserByEmailId(String email) {
return repo.findByEmailId(email);
}
}
答案1
得分: 6
如果您使用的是Spring Boot 2.3版本或更高版本,则必须将属性server.error.include-message
设置为always
:
默认错误页面内容的更改
默认情况下,错误消息和任何绑定错误不再包含在默认错误页面中。这减少了向客户端泄露信息的风险。
server.error.include-message
和server.error.include-binding-errors
可用于控制是否包含消息和绑定错误。支持的值为always
、on-param
和never
。
英文:
If you are using Spring Boot version 2.3 or higher, the property server.error.include-message
must be set to always
:
Quoted from Spring Boot 2.3 Release Notes:
> Changes to the Default Error Page’s Content
>
> The error message and any binding errors are no longer included in the default error page by default. This reduces the risk of leaking information to a client. server.error.include-message
and server.error.include-binding-errors
can be used to control the inclusion of the message and binding errors respectively. Supported values are always
, on-param
, and never
.
答案2
得分: 0
你可以将你的控制器响应包装为 ResponseEntity
。
@PostMapping("/registeruser")
public ResponseEntity<Object> registerUser(@RequestBody User user) throws Exception {
String tempEmailId = user.getEmailId();
if (tempEmailId != null && !tempEmailId.isEmpty()) {
User userObject = service.fetchUserByEmailId(tempEmailId);
if (userObject != null) {
return new ResponseEntity<>("用户 " + tempEmailId + " 已存在", HttpStatus.BAD_REQUEST);
}
}
return new ResponseEntity<>(service.saveUser(user), HttpStatus.OK);
}
或者(更可取的方式)你可以创建一个特定的异常,比如 UserAlreadyExistException
,在控制器中抛出它,并在带有 @RestControllerAdvice
注解的类中拦截它。
例如:
@RestControllerAdvice
public class RegistrationControllerAdvice {
@ExceptionHandler({UserAlreadyExistException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String userAlreadyExist(UserAlreadyExistException ex, WebRequest req) {
return ex.getMessage();
}
}
你可以返回任何可序列化为 JSON 的数据结构,而不仅仅是字符串。
英文:
You can wrap your controller responses to ResponseEntity
.
@PostMapping("/registeruser")
public ResponseEntity<Object> registerUser(@RequestBody User user) throws Exception {
String tempEmailId = user.getEmailId();
if(tempEmailId != null && !tempEmailId.isEmpty()) {
User userObject = service.fetchUserByEmailId(tempEmailId);
if(userObject!=null) {
return new ResponseEntity<>("User with "+tempEmailId+" is already exist", HttpStatus.BAD_REQUEST);
}
}
return new ResponseEntity<>(service.saveUser(user), HttpStatus.OK);
}
Or (preferable variant) your can create specific exception, for example UserAlreadyExistException
, throw it in controller and intercept in class annotated with @RestControllerAdvice
.
For example:
@RestControllerAdvice
public class RegistrationControllerAdvice {
@ExceptionHandler({UserAlreadyExistException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String userAlreadyExist(UserAlreadyExistException ex, WebRequest req) {
return ex.getMessage();
}
}
Instead of String you can return any data structure serializable to JSON.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论