Announcement Announcement Module
Collapse
No announcement yet.
Combining problem with Rest , Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Combining problem with Rest ,

    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)

    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());
        }
    I'm here using the resttemplate, and setting firstname to more than 20 chars, results in an SavePersonException.
    But how can I get the Error object (entity object, just like Person), which have detailed information about the error?
    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());
            }        
        }
    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:
    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);
        }
    Last edited by Jyrg; Mar 20th, 2012, 02:12 PM.
Working...
X