![](/img/trans.png)
[英]HTTP Status 400 - The request sent by the client was syntactically incorrect with spring
[英]Spring RestController - HTTP Status 400 The request sent by the client was syntactically incorrect
我在春天有這個RestController,大多數時候它都很好
@RestController
@RequestMapping("/restful/submitPrinterData")
public class SubmitPrinterDataController {
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> SubmitPrinterData(@RequestBody PrinterData printerData) { }
有時,我會收到格式正確的JSON數據,服務器會拋出:
HTTP Status 400 The request sent by the client was syntactically incorrect
我不知道為什么spring不將JSON轉換為PrinterData並且我看不到Spring日志,JSON本身就像5K行,因此無法跟蹤/驗證它
我試圖在eclipse中添加一個Java異常斷點以在eclipse框架中中斷,以獲取json中發生故障的場景,但沒有成功。
如何調試此問題或將日志記錄添加到Spring框架中以幫助調試此類問題?
通過將其放入application.properties文件來增加Spring日志記錄:
logging.level.org.springframework.web=DEBUG
我會接受第一個答案,盡管它對我沒有用(我對此發表了單獨的問題),
我最終添加了新的測試方法SubmitPrinterDataTest
此方法將JSON作為字符串獲取,然后使用Jackson進行轉換,然后捕獲所有轉換問題,然后將其作為JSON返回,因此非常容易發現問題。
@RestController
@RequestMapping("/restful/submitPrinterData")
public class SubmitPrinterDataController {
@RequestMapping(value="/test",method = RequestMethod.POST)
public ResponseEntity<String> SubmitPrinterDataTest(@RequestBody String printerData) {
ObjectMapper mapper = new ObjectMapper();
try {
PrinterData printerData= mapper.readValue(appResponse, PrinterData.class);
}catch(Exception ex) {
ex.printStackTrace();
String err = StringEscapeUtils.escapeJson(ex.getMessage());
return Helper.getRsponse(HttpStatus.INTERNAL_SERVER_ERROR,"{\"error\":\""+err+"\"}");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.