Announcement Announcement Module
Collapse
No announcement yet.
How to add post processing after controller or extend view resolver in spring mvc Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 01:30 AM.

  • #2
    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?

    Comment

    Working...
    X