Java 如何更改返回的与@NotNull注释对应的JSON响应
我有一个简单的代码,当customerId不在RequestBody中时返回错误json VO等级:Java 如何更改返回的与@NotNull注释对应的JSON响应,java,spring,spring-boot,jackson,Java,Spring,Spring Boot,Jackson,我有一个简单的代码,当customerId不在RequestBody中时返回错误json VO等级: public class OrderVO { private int orderId; @NotNull(message = "CustomerId Cant be null") private Long customerId; } 控制器方法: @RequestMapping(value="/testOrderbyOrderid", method=RequestMe
public class OrderVO {
private int orderId;
@NotNull(message = "CustomerId Cant be null")
private Long customerId;
}
控制器方法:
@RequestMapping(value="/testOrderbyOrderid", method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
public void testOrderJson (@Valid @RequestBody OrderVO orderVO ) {
}
当前当RequestBody中不存在customerId时,返回的JSON结构如下所示:
{
"timestamp": "2019-05-14T17:08:01.318+0000",
"status": 400,
"error": "Bad Request",
"errors": [
{
"codes": [ ],
"arguments": [ ],
"defaultMessage": "CustomerId Cant be null",
"objectName": "orderVO",
"field": "customerId",
"rejectedValue": null,
"bindingFailure": false,
"code": "NotNull"
}
],
"message": "Validation failed for object='orderVO'. Error count: 1",
"path": "/testOrderbyOrderid"
}
如何将@Notnull返回的上述Json结构更改为如下所示的Json结构:
{
"timestamp": "2019-05-14T17:08:01.318+0000",
"status": 400,
"error": "Bad Request",
"message": "CustomerId Cant be null"
}
<>编辑-I已经知道,我们可以在控件建议中抛出自定义异常并处理它,但是考虑如果验证所需的字段数为20,检查null和Spple异常所需的代码量也会放大,使得代码看起来很难看。这就是我发布此Qn的原因。将以下方法添加到控制器建议注释的异常处理程序中:
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status,
WebRequest request) {
//return your custom error
//you can access to field errors using
//ex.getBindingResult().getFieldErrors()
}
@ExceptionHandler(value = { javax.validation.ConstraintViolationException.class })
protected ResponseEntity<Object> handleConstraintViolation(
javax.validation.ConstraintViolationException ex) {
//return your custom error message
}
@ExceptionHandler(value = { org.hibernate.exception.ConstraintViolationException.class })
protected ResponseEntity<Object> handleHibernateConstraintViolation(
org.hibernate.exception.ConstraintViolationException ex) {
//return your custom error message
}
@覆盖
受保护的响应句柄方法无效(
MethodArgumentNotValidException ex、HttpHeaders、HttpStatus状态、,
WebRequest(请求){
//返回您的自定义错误
//您可以使用访问字段错误
//例如:getBindingResult().getFieldErrors()
}
@ExceptionHandler(值={javax.validation.ConstraintViolationException.class})
保护性响应安全性操作培训(
javax.validation.ConstraintViolationException(ex){
//返回自定义错误消息
}
@ExceptionHandler(值={org.hibernate.exception.ConstraintViolationException.class})
受保护的反应性处理冬眠应变振荡(
org.hibernate.exception.ConstraintViolationException(示例){
//返回自定义错误消息
}
我想我们也可以这样编写控制器建议,而无需扩展ResponseEntityExceptionHandler&重写其任何方法
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = { MethodArgumentNotValidException.class })
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex){
CustomException cex = new CustomException(ex.getBindingResult().getFieldError().getDefaultMessage());
return new ResponseEntity<>(cex, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
}
@RestControllerAdvice
公共类GlobalExceptionHandler{
@ExceptionHandler(值={MethodArgumentNotValidException.class})
受保护的响应句柄MethodArgumentNotValid无效(MethodArgumentNotValidException ex){
CustomException cex=新的CustomException(例如getBindingResult().getFieldError().getDefaultMessage());
返回新的ResponseEntity(cex,new HttpHeaders(),HttpStatus.INTERNAL_SERVER_ERROR);
}
你可以使用错误处理来构建自定义响应。在@ SukUpUp中发现了类似的问题。我已经知道,我们可以抛出自定义异常并在控制器建议中处理它,但是考虑如果验证所需字段的NUMMBOER为20,检查和抛出异常所需的量也会放大,使得代码看起来很难看。这就是为什么。很抱歉,我已经发布了这个QnI。它对我不起作用。我仍然得到相同的JSON结构。到目前为止没有任何更改。我也在controllerAdvice上放置了调试点,但是控件没有到达这些方法。@ASharma7我更新了我的答案。注意重写方法我想我们可以跳过这个重写并保留controllerAdvice sim请看我的答案below@ASharma7是的。但是在异常处理程序的父级ResponseEntityExceptionHandler
中有一些我们不需要自己编写的处理程序。该链接可能对其他人有用: