带有java验证API的SpringRestController。错误消息太冗长
我必须使用Spring编写一个Rest控制器带有java验证API的SpringRestController。错误消息太冗长,java,spring,rest,validation,dto,Java,Spring,Rest,Validation,Dto,我必须使用Spring编写一个Rest控制器 @PostMapping(value = "/mycontroller", produces = "application/json") @ResponseBody @ResponseStatus(HttpStatus.CREATED) public MyDTOOuptup myMethod(@Valid @RequestBody MyDTO input) { ... body ... }
@PostMapping(value = "/mycontroller", produces = "application/json")
@ResponseBody
@ResponseStatus(HttpStatus.CREATED)
public MyDTOOuptup myMethod(@Valid @RequestBody MyDTO input) {
... body ...
}
我编写了一个DTO输入对象,它表示控制器的请求主体。
在DTO中,我添加了一些验证规则来验证输入,然后将请求分析到控制器中
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyDTO {
@NotNull(message="my custom error message for field_a")
@JsonProperty("field_A")
private String fieldA;
@NotNull(message="my custom error message for field_b")
@JsonProperty("field_B")
private String fieldB;
}
它很好用。在输入错误的情况下,我将收到400错误的请求,并在响应正文中给出相应的错误描述
但是,我看到这个json正文太冗长了
{
"timestamp": "2020-03-31T14:29:42.401+0000",
"status": 400,
"error": "Bad Request",
"errors": [
{
"codes": [
"NotNull.myDTO.field_a",
"NotNull.field_a",
"NotNull.java.lang.String",
"NotNull"
],
"arguments": [
{
"codes": [
"myDTO.field_a",
"field_a"
],
"arguments": null,
"defaultMessage": "field_a",
"code": "field_a"
}
],
"defaultMessage": "my custom error message for field_a",
"objectName": "myDTO",
"field": "productId",
"rejectedValue": null,
"bindingFailure": false,
"code": "NotNull"
}
],
"message": "Validation failed for object='myDTO'. Error count: 1",
"path": "/mycontroller"
}
如何指定我只需要错误描述消息或类似的内容?
是否有智能/超薄结构?您可以定义自己的自定义异常并添加异常处理程序
class ExceptionResponse {
private boolean success = false;
private String errorCode;
private String errorMessage;
private String exception;
private List<String> errors;
private String path;
private String timestamp = LocalDateTime.now().toString();
}
@ControllerAdvice
public class CustomExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ExceptionResponse> invalidInput(MethodArgumentNotValidException ex, HttpServletRequest request) {
ExceptionResponse response = getExceptionResponse(); //generate exception response
return ResponseEntity.badRequest().contentType(MediaType.APPLICATION_JSON_UTF8).body(response);
}
}
类异常响应{
私有布尔成功=假;
私有字符串错误码;
私有字符串错误消息;
私有字符串异常;
私有列表错误;
私有字符串路径;
私有字符串timestamp=LocalDateTime.now().toString();
}
异常处理程序
class ExceptionResponse {
private boolean success = false;
private String errorCode;
private String errorMessage;
private String exception;
private List<String> errors;
private String path;
private String timestamp = LocalDateTime.now().toString();
}
@ControllerAdvice
public class CustomExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ExceptionResponse> invalidInput(MethodArgumentNotValidException ex, HttpServletRequest request) {
ExceptionResponse response = getExceptionResponse(); //generate exception response
return ResponseEntity.badRequest().contentType(MediaType.APPLICATION_JSON_UTF8).body(response);
}
}
@ControllerAdvice
公共类CustomExceptionHandler{
@ExceptionHandler(MethodArgumentNotValidException.class)
公共响应无效输入(MethodArgumentNotValidException ex,HttpServletRequest){
ExceptionResponse=getExceptionResponse();//生成异常响应
返回ResponseEntity.badRequest().contentType(MediaType.APPLICATION_JSON_UTF8).body(响应);
}
}
我希望有帮助 您可以定义自己的自定义异常并添加异常处理程序
class ExceptionResponse {
private boolean success = false;
private String errorCode;
private String errorMessage;
private String exception;
private List<String> errors;
private String path;
private String timestamp = LocalDateTime.now().toString();
}
@ControllerAdvice
public class CustomExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ExceptionResponse> invalidInput(MethodArgumentNotValidException ex, HttpServletRequest request) {
ExceptionResponse response = getExceptionResponse(); //generate exception response
return ResponseEntity.badRequest().contentType(MediaType.APPLICATION_JSON_UTF8).body(response);
}
}
类异常响应{
私有布尔成功=假;
私有字符串错误码;
私有字符串错误消息;
私有字符串异常;
私有列表错误;
私有字符串路径;
私有字符串timestamp=LocalDateTime.now().toString();
}
异常处理程序
class ExceptionResponse {
private boolean success = false;
private String errorCode;
private String errorMessage;
private String exception;
private List<String> errors;
private String path;
private String timestamp = LocalDateTime.now().toString();
}
@ControllerAdvice
public class CustomExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ExceptionResponse> invalidInput(MethodArgumentNotValidException ex, HttpServletRequest request) {
ExceptionResponse response = getExceptionResponse(); //generate exception response
return ResponseEntity.badRequest().contentType(MediaType.APPLICATION_JSON_UTF8).body(response);
}
}
@ControllerAdvice
公共类CustomExceptionHandler{
@ExceptionHandler(MethodArgumentNotValidException.class)
公共响应无效输入(MethodArgumentNotValidException ex,HttpServletRequest){
ExceptionResponse=getExceptionResponse();//生成异常响应
返回ResponseEntity.badRequest().contentType(MediaType.APPLICATION_JSON_UTF8).body(响应);
}
}
我希望有帮助 这些属性以
地图的形式提供
的默认实现。尽可能提供以下属性:
如果要修改错误
,必须首先获取带有属性的映射,然后修改并最终返回它。属性是使用该方法获取的。从地图中删除是相当简单的
下面是应该可以工作的代码。返回的bean应该是
@Bean
公共ErrorAttributes ErrorAttributes(){
返回新的DefaultErrorAttributes(){
@凌驾
公共映射getErrorAttributes(WebRequest WebRequest,布尔includeStackTrace){
Map errorAttributes=super.getErrorAttributes(webRequest,includeStackTrace);
errorAttributes。删除(“错误”);
返回错误属性;
}
};
}
这些属性以地图的形式提供
的默认实现。尽可能提供以下属性:
如果要修改错误
,必须首先获取带有属性的映射,然后修改并最终返回它。属性是使用该方法获取的。从地图中删除是相当简单的
下面是应该可以工作的代码。返回的bean应该是
@Bean
公共ErrorAttributes ErrorAttributes(){
返回新的DefaultErrorAttributes(){
@凌驾
公共映射getErrorAttributes(WebRequest WebRequest,布尔includeStackTrace){
Map errorAttributes=super.getErrorAttributes(webRequest,includeStackTrace);
errorAttributes。删除(“错误”);
返回错误属性;
}
};
}