Java – using spring MVC and accepting post requests with wrong JSON will result in returning the default 400 error code server page
I'm using the rest API Receiving a post message with bad JSON (such as {sdfasdf}) will cause spring to return a 400 error request to the wrong default server page I don't want to return a page, I want to return a custom JSON error object
I can do this when I throw an exception using @ exceptionhandler So if it is a blank request or a blank JSON object (such as {}), it will throw a NullPointerException. I can grab it with my exceptionhandler and do whatever I like
The problem is that spring doesn't throw exceptions when it's just invalid syntax, at least I can't see it It just returns the default error page from the server, whether tomcat, GlassFish, etc
So my question is how can I "intercept" spring and cause it to use my exception handler or otherwise prevent the error page from displaying instead of returning a JSON error object?
This is my code:
@RequestMapping(value = "/trackingNumbers",method = RequestMethod.POST,consumes = "application/json") @ResponseBody public ResponseEntity<String> setTrackingNumber(@RequestBody TrackingNumber trackingNumber) { HttpStatus status = null; ResponseStatus responseStatus = null; String result = null; ObjectMapper mapper = new ObjectMapper(); trackingNumbeRSService.setTrackingNumber(trackingNumber); status = HttpStatus.CREATED; result = trackingNumber.getCompany(); ResponseEntity<String> response = new ResponseEntity<String>(result,status); return response; } @ExceptionHandler({NullPointerException.class,EOFException.class}) @ResponseBody public ResponseEntity<String> resolveException() { HttpStatus status = null; ResponseStatus responseStatus = null; String result = null; ObjectMapper mapper = new ObjectMapper(); responseStatus = new ResponseStatus("400","That is not a valid form for a TrackingNumber object " + "({\"company\":\"EXAMPLE\",\"pro_bill_id\":\"EXAMPLE123\",\"tracking_num\":\"EXAMPLE123\"})"); status = HttpStatus.BAD_REQUEST; try { result = mapper.writeValueAsString(responseStatus); } catch (IOException e1) { e1.printStackTrace(); } ResponseEntity<String> response = new ResponseEntity<String>(result,status); return response; }
Solution
This is a problem with spring spr-7439 - the JSON (Jackson) @ requestbody grouping raises an embarrassing exception - in spring 3.1m2, it has been fixed that spring throws an org. Org in a missing or invalid case springframework. http. converter. Httpmessagenotreadableexception message body
In your code, because it is abstract, you can't create a responsestatus, but I use spring 3.2 0.release in jetty 9.0 3. Use the local test method running on v20130506 to catch this exception
@ExceptionHandler({org.springframework.http.converter.HttpMessageNotReadableException.class}) @ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseBody public String resolveException() { return "error"; }
I received a 400 status "error" string response
The defects have been discussed at this spring forum
Note: I started using jetty 9.0 0.m4 for testing, but there are other internal problems that prevent the completion of @ exceptionhandler, so according to your container (jetty, Tomcat, other) version, you may need to get a better version, any version of spring you use