如果你的項目中用的是默認的Jackson解析JSON的話
後端代碼:
public ResponseEntity<Object> getTradeConfig(@RequestBody AccountTradeReqDTO accountTradeReqDTO) {
}
前端代碼:
{"tradeFlag": 1}
此時會報錯:
2021-11-30 16:36:34.087 [http-nio-8080-exec-5] ERROR [com.xxx.xxx.xx.web.admin.exception.handler.GlobalExceptionHandler.handleException: 48] - org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
at [Source: (PushbackInputStream); line: 1, column: 1]
此時有兩種解決方案:
第一種方案
修改前端代碼,要把JSON對象轉換爲JSON字符串:
"{\"tradeFlag\": 1}"
後端代碼不變
public ResponseEntity<Object> getTradeConfig(@RequestBody AccountTradeReqDTO accountTradeReqDTO) {
}
第二種方案
前端代碼不變
{"tradeFlag": 1}
後端代碼中去掉`@RequestBody`這個註解
public ResponseEntity<Object> getTradeConfig(AccountTradeReqDTO accountTradeReqDTO) {
}