简体   繁体   中英

Spring RestController - HTTP Status 400 The request sent by the client was syntactically incorrect

I have this RestController in spring, most of the time it is working just fine

@RestController
@RequestMapping("/restful/submitPrinterData")
public class SubmitPrinterDataController {

@RequestMapping(method = RequestMethod.POST)    
public ResponseEntity<String> SubmitPrinterData(@RequestBody PrinterData printerData) {  }                      

from time to time, I get meformd JSON data and the server is throwing :

 HTTP Status 400 The request sent by the client was syntactically incorrect

I am clueless of why spring is not converting the JSON to PrinterData and I am unable to see Spring logs, the JSON itself is like 5K lines so its impossible to track/validate it

I tried to add a java exception break point in eclipse to break in the eclipse framework to get a scenes of what is broken in the json without success.
在此处输入图片说明

How can I debug this issue or add logging to the spring framework that will help me debug such issues?

通过将其放入application.properties文件来增加Spring日志记录:

logging.level.org.springframework.web=DEBUG

I will accept the first answer although it didn't work for me (I posted separate question about this),
I ended up adding a new test method SubmitPrinterDataTest
This method is getting the JSON as string and then converting it using Jackson, it then catching any conversion issues, and returning it as JSON so its really easy to see the issue.

@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+"\"}");
            }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM