java - Custom HttpMessageConverter in Spring MVC -


when implementing restful api wrap data in object looks this.

{error: null, code: 200, data: {...actual data...}} 

this results in repetitive code use everywhere wrap data:

@transactional @requestmapping(value = "/", method = requestmethod.get) public @responsebody result<list<bookshortdto>> books() {      list<book> books = booksdao.readbooks();     return result.ok(books); // gets repeated everywhere } 

so question how modify (maybe use of custom httpmessageconverter maybe other ways?) return booksdao.readbooks() , wrapped automatically.

like @ralph suggested can use handlermethodreturnvaluehandler wrap handlers return value.

the easiest way achieve extending requestresponsebodymethodprocessor , alter it's behavior bit. best create custom annotation mark handler methods with. make sure handlermethodreturnvaluehandler called instead of others included requestmappinghandleradapter default.

@target({elementtype.method}) @retention(retentionpolicy.runtime) public @interface resultresponsebody {} 

here simple implementation of custom handlermethodreturnvaluehandler named resultresponsehandlermethodprocessor support values returned methods annotated resultresponsebody. it's pretty simple. override supportsreturntype() , handlereturnvalue() methods suit needs (wrap return value result type).

public class resultresponsehandlermethodprocessor extends requestresponsebodymethodprocessor {     public resultresponsehandlermethodprocessor(final list<httpmessageconverter<?>> messageconverters) {         super(messageconverters);     }      public resultresponsehandlermethodprocessor(final list<httpmessageconverter<?>> messageconverters, final contentnegotiationmanager contentnegotiationmanager) {         super(messageconverters, contentnegotiationmanager);     }      @override     public boolean supportsreturntype(final methodparameter returntype) {         return returntype.getmethodannotation(resultresponsebody.class) != null;     }      @override     public void handlereturnvalue(final object returnvalue, final methodparameter returntype, final modelandviewcontainer mavcontainer, final nativewebrequest webrequest) throws ioexception, httpmediatypenotacceptableexception {         super.handlereturnvalue(result.ok(returnvalue), returntype, mavcontainer, webrequest);     } } 

the thing left add class list of custom handlermethodreturnvaluehandlers , provide mappingjackson2httpmessageconverter instance.

@enablewebmvc @configuration public class applicationconfiguration extends webmvcconfigureradapter     @override     public void addreturnvaluehandlers(final list<handlermethodreturnvaluehandler> returnvaluehandlers) {         list<httpmessageconverter<?>> messageconverters = new arraylist<>();         messageconverters.add(new mappingjackson2httpmessageconverter());         returnvaluehandlers.add(new resultresponsehandlermethodprocessor(messageconverters));     } } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -