Spring MVC precisely controls the return field Simplefilterprovider, @JasonView, and the selection of the VO view model

Source: Internet
Author: User

The advantage of using simplefilterprovider,propertyfilter is decoupling and flexibility. But the drawbacks are obvious.

Let's take a look at the way @jsonview is handled.

There is a configuration method in Webmvcconfigurationsupport, if the program has com.fasterxml.jackson.databind.ObjectMapper && The reference to the Com.fasterxml.jackson.core.JsonGenerator package will implement the Requestbodyadvice Jsonviewrequestbodyadvice and

Implemented the Responsebodyadvice Jsonviewresponsebodyadvice add in
@Bean PublicRequestmappinghandleradapter Requestmappinghandleradapter () {Requestmappinghandleradapter adapter=Createrequestmappinghandleradapter ();        Adapter.setcontentnegotiationmanager (Mvccontentnegotiationmanager ());        Adapter.setmessageconverters (Getmessageconverters ());        Adapter.setwebbindinginitializer (Getconfigurablewebbindinginitializer ());        Adapter.setcustomargumentresolvers (Getargumentresolvers ());        Adapter.setcustomreturnvaluehandlers (Getreturnvaluehandlers ()); if(jackson2present) { adapter.setrequestbodyadvice (collections.singletonlist ( new Jsonviewrequestbodyadvice ())); Adapter.setresponsebodyadvice (Collections.singletonlist (New Jsonviewresponsebodyadvice ())); } asyncsupportconfigurer Configurer=NewAsyncsupportconfigurer ();        Configureasyncsupport (Configurer); if(Configurer.gettaskexecutor ()! =NULL) {Adapter.settaskexecutor (Configurer.gettaskexecutor ()); }        if(Configurer.gettimeout ()! =NULL) {adapter.setasyncrequesttimeout (Configurer.gettimeout ());        } adapter.setcallableinterceptors (Configurer.getcallableinterceptors ());        Adapter.setdeferredresultinterceptors (Configurer.getdeferredresultinterceptors ()); returnadapter; }

The final deal is that requestresponsebodyadvicechain this chain of responsibility

Using the Simplefilterprovider,propertyfilter custom control JSON return value, you will get the request directly, forcing the modification of the request header to JSON, and output, the end of the requests. The chain of responsibility was interrupted, So in the @restcontroller, it will fail.

Using @jsonview to compare with spring's design pattern, but the coupling is very strong, and you will find that if you want to return to the JPA page @JsonView is useless

Also to the page to do the following packaging

/** * @authorLaizhenwei * @date 2018/06/09*/@JsonView (View.class) Public classPagevo<t>extendsPageimpl<t>ImplementsPage<t> {    Private Static Final LongSerialversionuid = -3530103094806749298l;  PublicPagevo (FinalList<t> content,FinalPageable pageable,Final LongTotal ) {        Super(content, pageable, total); }     PublicPagevo (FinalList<t>content) {        Super(content); }     PublicPagevo (FinalPage<t> page,Finalpageable pageable) {        Super(Page.getcontent (), pageable, page.gettotalelements ()); }

So still write Vo conversion, anyway to waste meta-space

Spring MVC precisely controls the return field Simplefilterprovider, @JasonView, and the selection of the VO view model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.