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