Java 弹簧控制器建议不返回响应体?
我有以下Java 弹簧控制器建议不返回响应体?,java,spring,error-handling,controller-advice,Java,Spring,Error Handling,Controller Advice,我有以下ControllerAdvice,它处理JsonParseException(我使用Spring和Jackson) @ControllerAdvice 公共类ControllerExceptionHandler扩展了ResponseEntityExceptionHandler{ @ExceptionHandler(JsonParseException.class) 公共响应handleInvalidJson(JsonParseException-ex,WebRequest-request
ControllerAdvice
,它处理JsonParseException
(我使用Spring和Jackson)
@ControllerAdvice
公共类ControllerExceptionHandler扩展了ResponseEntityExceptionHandler{
@ExceptionHandler(JsonParseException.class)
公共响应handleInvalidJson(JsonParseException-ex,WebRequest-request){
映射体=新LinkedHashMap();
put(“timestamp”,LocalDateTime.now());
body.put(“消息”,“无效Json”);
返回新的响应属性(body,HttpStatus.BAD_请求);
}
}
出于某种原因,当我向服务器发送错误的json请求时,它不起作用,只返回400。当我更改
HttpStatus
时,它仍然返回400,因此建议似乎没有真正运行。ResponseEntityExceptionHandler
已经实现了许多不同的异常处理程序httpmessagegenoteradableexception
是其中之一:
else if (ex instanceof HttpMessageNotReadableException) {
HttpStatus status = HttpStatus.BAD_REQUEST;
return handleHttpMessageNotReadable((HttpMessageNotReadableException) ex, headers, status, request);
}
只需删除继承:
@ControllerAdvice
public class TestExceptionHandler {
@ExceptionHandler(JsonParseException.class)
public ResponseEntity<Map<String,Object>> handleInvalidJson(JsonParseException ex, WebRequest request){
Map<String,Object> body = new LinkedHashMap<>();
body.put("timestamp", LocalDateTime.now());
body.put("message","Invalid Json");
return new ResponseEntity<>(body, HttpStatus.I_AM_A_TEAPOT);
}
}
@ControllerAdvice
公共类TestExceptionHandler{
@ExceptionHandler(JsonParseException.class)
公共响应handleInvalidJson(JsonParseException-ex,WebRequest-request){
映射体=新LinkedHashMap();
put(“timestamp”,LocalDateTime.now());
body.put(“消息”,“无效Json”);
返回新的响应(body,HttpStatus.I\u AM\u A\u茶壶);
}
}
@ControllerAdvice
public class TestExceptionHandler {
@ExceptionHandler(JsonParseException.class)
public ResponseEntity<Map<String,Object>> handleInvalidJson(JsonParseException ex, WebRequest request){
Map<String,Object> body = new LinkedHashMap<>();
body.put("timestamp", LocalDateTime.now());
body.put("message","Invalid Json");
return new ResponseEntity<>(body, HttpStatus.I_AM_A_TEAPOT);
}
}