Results 1 to 2 of 2

Thread: How to add post processing after controller or extend view resolver in spring mvc

  1. #1
    Join Date
    Mar 2012
    Posts
    2

    Default How to add post processing after controller or extend view resolver in spring mvc

    Hi,
    I've coded a exception handler in my project to format all json response. like this:
    Code:
    public @ResponseBody ResponseMessage save(final User user){
        ResponseMessage rm = ResponseMessage.handleException(
            new handler(){  // this is a interface.
                return userRepository.save(user); // this is a repository object, which may throw exceptions.
            }
        );
        return rm;
    }
    this code will wrap user object to a response message, which has a status info and a message info.
    Code:
    {
    status:'success',
    message:{id:'1',username:'jmars'}
    }
    or exception:
    Code:
    {
    status:'warning',
    message:{code:'e002',message:'The database can't be connected.'}
    }
    So I got lots of the same code fragments, response message script, in my project.

    I wonder if there is a way to simplify this process, so that people do not need to code the message transformation explicitly. It might be an aop, interceptor, filter, controller post processor, or view resolver, something else.


    Jan
    Last edited by jmars; Mar 28th, 2012 at 12:30 AM.

  2. #2
    Join Date
    Mar 2012
    Posts
    2

    Default

    I suppose the new programming model is more simple. It might be:

    Code:
    public @ResponseMessage User save(User user) throws Exception{
        return userRepository.save(user);
    }
    Then the post processor we declared will wrap the return value or the exception to ResponseMessage.
    How can I implement it? or is there any other way to reduce the original code?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •