I have started to study new functionallity in Spring 3.1 and created an small example, but I need an helping hand here.
The following is from my controller class.
Saving an person can throw an SavePersonException (which then is rollbacked in the service, DAO layer)
I'm here using the resttemplate, and setting firstname to more than 20 chars, results in an SavePersonException.Code:@RequestMapping(value = "/savePerson", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE }, consumes = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE }) @ResponseBody public void savePerson(@RequestBody Person person) throws SavePersonException { personService.save(person); } . . @ExceptionHandler(SavePersonException.class) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public Error handleException(SavePersonException e, HttpServletResponse response) { return new Error(e.getMessage()); }
But how can I get the Error object (entity object, just like Person), which have detailed information about the error?
An workaround could be to always return some general responseobject and in that put info about the operation (succeded and failures), but the code will be filled with more clutter...Code:@Test public void savePerson2() { Person person = TestData.getNilsHult(); person.setFirstName("0123456789012345678901234"); try { template.postForObject(BASE_URL + "/savePerson", person, Person.class); } catch (RestClientException e) { System.out.println("--> " + e.getMessage()); } }
Code:MyResponseObject response = template.postForObject(BASE_URL + "/savePerson", person, MyResponseObject.class); response.getDetailErrorInfo() and so on . . @RequestMapping(value = "/savePerson", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE }, consumes = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE }) @ResponseBody public MyResponseObject savePerson(@RequestBody Person person) throws SavePersonException { personService.save(person); return new MyResponseObject(aaa, bbb, ccc); }


Reply With Quote
