![](/img/trans.png)
[英]Handle DefaultHandlerExceptionResolver : org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character
[英]How to handle "org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error" in Spring?
我正在使用 @RestController 將 API 添加到現有的 Spring 引導項目中。
我的Spring 開機體驗是:這是我第一次使用。
當我發出格式正確的請求時,RestController 工作正常,但如果請求不正確(這是我所期望的),則會引發錯誤。 但是,我無法弄清楚如何向用戶提供有意義的錯誤響應。
這是 Controller class:
@RestController
public class SomeController {
@PostMapping(value = "/update")
@ResponseBody
public ResponseEntity update(@RequestBody Config config) {
//do stuff
return ResponseEntity.ok(//response);
}
}
這是配置 class:
@Getter
@Setter
@AllArgsConstructor
@Builder
public class Config {
private String thing1;
private String thing2;
}
如果我運行應用程序並點擊localhost:8080/update
:
{
"thing1": "dog",
"thing2": "cat",
}
然后一切都很好,我們可以//做一些事情。
但是,如果我點擊localhost:8080/update
:
{
"thing1": "dog",
"thing2": 123,
}
我得到一個異常: .wsmsDefaultHandlerExceptionResolver: Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type...
這是意料之中的,因為我提供了一個數字,並且 thing2 需要一個字符串。
我得到的回應是: <h1>sorry</h1>
我不確定這是 Spring 默認值還是在應用程序某處配置(如果是我找不到它)。
我想將其更改為有意義的內容,例如:“您為 thing2 提供了不正確的值”。
但由於代碼永遠不會到達 //do stuff 我不知道如何處理這個?
我試圖通過以下方式例外 class:
@ControllerAdvice
@ExceptionHandler(HttpMessageNotReadableException.class)
但我從 IDE 得到“無法解析符號 HttpMessageNotReadableException”。 我什至不確定是否例外 class 是處理它的正確方法嗎?
編輯:在本教程(https://www.toptal.com/java/spring-boot-rest-api-error-handling )中,Spring 默認消息類似於:
{
"timestamp": 1500597044204,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "JSON parse error: Unrecognized token 'three': was expecting ('true', 'false' or 'null'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'aaa': was expecting ('true', 'false' or 'null')\n at [Source: java.io.PushbackInputStream@cba7ebc; line: 4, column: 17]",
"path": "/birds"
}
但我只是得到<h1>sorry</h1>
。 我實際上想要更像上面的東西。
這應該可以工作,但 Spring 並不意味着在 HTTP 請求可以正確反序列化之前處理錯誤。 您可以在反序列化后使用 DTO 類字段(如 @NotNull、@NotEmpty、@Positive 等)上的注釋來為請求數據編寫規則...
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice
public class ErrorController {
@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<String> handleException(HttpMessageNotReadableException exception, HttpServletRequest request) {
return new ResponseEntity("You gave an incorrect value for ....", HttpStatus.BAD_REQUEST);
}
}
我首選的解決方案是覆蓋 handleHttpMessageNotReadable 方法:) 而不是使用 @ExceptionHandlers
@Override
protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
//handle exception
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.