[英]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.