Spring MVC Third Day "annotation implementation SPRINGMVC handler return value to object Configuration"

Source: Internet
Author: User

This is used in the foreground to initiate the request handler, background forgery data response to the front desk,

Solution: Erase the previous viewresolver, and configure the handler information for the corresponding request requests as follows

Before the handler returned is directly the logical view name, and need to configure the corresponding fixed view resolver, not very convenient, this time need to return object, make handler more flexible.

Since the value after return is no longer a logical view name, let @responsebody help.

Here are two new annotations:

@RequestBody convert the HTTP request body to the appropriate Httpmessageconverter object.

The @ResponseBody returns the content or object as the HTTP response body, and calls the adapter transform object that is appropriate for Httpmessageconverter to write to the output stream.

Httpmessageconverter interface, you need to turn on <mvc:annotation-driven/>.
The annotationmethodhandleradapter will initialize 7 converters, which can be initialized by calling Annotationmethodhandleradapter 's Getmessageconverts () method to get a collection of converters list

List

Reference
Bytearrayhttpmessageconverter Stringhttpmessageconverter Resourcehttpmessageconverter SourceHttpMessageConverter Xmlawareformhttpmessageconverter Jaxb2rootelementhttpmessageconverter Mappingjacksonhttpmessageconverter

It can be understood that, as long as there is a corresponding protocol parser, you can pass a few lines of configuration, a few annotations to complete the protocol-the transformation of the object work!

Ps:spring the default JSON protocol parsing is done by Jackson.

1 @Controller2  Public classMycontroller {3    //Processor Method4@RequestMapping (value= "/first.do")5 @ResponseBody6      PublicObject Dofirst () {7         return1;8     }9       Ten     //Processor Method-----String One@RequestMapping (value= "/second.do", produces= "Text/html;charset=utf-8") A @ResponseBody -      PublicObject Dosecond () { -          the         returnChinese characters; -     } -      -      +     //Processor Method-----UserInfo -@RequestMapping (value= "/third.do") + @ResponseBody A          PublicObject Dothird () { atUserInfo info=NewUserInfo (); -Info.setage (12); -Info.setname ("Happy"); -             returninfo; -         } -          in         //Processor Methods-----Map -@RequestMapping (value= "/four.do") to @ResponseBody +          PublicObject Dofour () { -UserInfo info=NewUserInfo (); theInfo.setage (12); *Info.setname ("Happy"); $             Panax Notoginseng              -Map<string,userinfo> map=NewHashmap<string,userinfo>(); theMap.put ("Info", info); +             returnmap; A         } the          +         //Processor Method-----List -@RequestMapping (value= "/five.do") $ @ResponseBody $          PublicObject dofive () { -UserInfo info=NewUserInfo (); -Info.setage (12); theInfo.setname ("Happy"); -         Wuyi          theList<userinfo> list=NewArraylist<userinfo>(); - List.add (info); Wu             returnlist; -         } About}

These handler are all applicable to Ajax requests. You do not specify return view information.

Spring MVC Third Day "annotation implementation SPRINGMVC handler return value to object Configuration"

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.