Recent projects do not use this, the first self-taught to run through the example of the first post, for their future reference it!
If there is no place to look point!
First, custom class implementation Abstracthttpmessageconverter
1 PackageCom.dzf.converter;2 3 Importjava.io.IOException;4 ImportJava.nio.charset.Charset;5 6 ImportOrg.springframework.http.HttpInputMessage;7 ImportOrg.springframework.http.HttpOutputMessage;8 ImportOrg.springframework.http.MediaType;9 ImportOrg.springframework.http.converter.AbstractHttpMessageConverter;Ten Importorg.springframework.http.converter.HttpMessageNotReadableException; One Importorg.springframework.http.converter.HttpMessageNotWritableException; A Importorg.springframework.util.StreamUtils; - - ImportCom.alibaba.fastjson.JSONObject; the ImportCom.dzf.vo.ResultInfo; - /** - * Custom Message converters - * @authorDINGZF + * @date January 20, 2018 - * @time 19:26:39 + */ A Public classMymessageconverterextendsAbstracthttpmessageconverter<resultinfo> { at - - PublicMymessageconverter () { - Super(Charset.forname ("Utf-8"),NewMediaType ("Application", "X-result") ;//application/x-result own defined media data type - } - in //The model that is mapped - @Override to protected BooleanSupports (class<?>clazz) { + returnResultinfo.class. IsAssignableFrom (clazz); - } the * @Override $ protectedResultinfo readinternal (class<?extendsResultinfo>Clazz, Httpinputmessage inputmessage)Panax Notoginseng throwsIOException, httpmessagenotreadableexception { -String copytostring = streamutils.copytostring (Inputmessage.getbody (), Charset.forname ("Utf-8") );//Transfer to JSON-type data theResultinfo result =(Resultinfo) jsonobject.parse (copytostring);//Convert to the data type you want to write on demand + returnresult; A } the + @Override - protected voidwriteinternal (Resultinfo T, httpoutputmessage outputmessage) $ throwsIOException, httpmessagenotwritableexception { $String str = t.getcode () + "-" +T.getdesc ();//return to foreground data - outputmessage.getbody (). Write (Str.getbytes ()); - } the -}
Ii. Add our custom message converters to the SPRINGMVC configuration file
1 <!-- 2 Open annotation mode for SPRINGMVC3 MVC request Map processor and adapter configuration -4 <Mvc:annotation-driven>5 <mvc:message-convertersRegister-defaults= "true">6 <Beanclass= "Org.springframework.http.converter.StringHttpMessageConverter" ><!--String Converters --7 < Propertyname= "Supportedmediatypes">8 <List>9 <value>Application/json;charset=utf-8</value>Ten <value>Text/html;charset=utf-8</value> One </List> A </ Property> - </Bean> - <Beanclass= "Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /><!--JSON converter -- the <Beanclass= "Com.dzf.converter.MyMessageConverter"> <!--your own defined message converters-- - < Propertyname= "Supportedmediatypes"> - <List> - <value>Application/json;charset=utf-8</value> + <value>Application/x-result;charset=utf-8</value> - <value>Text/html;charset=utf-8</value> + </List> A </ Property> at </Bean> - </mvc:message-converters> - </Mvc:annotation-driven>
Third, in the foreground specify the format of sending data
1 function Test6 () {2 $.ajax ({3 type: ' Post ',4 URL: ' Json/test6 ',5 contentType: ' Application/x-result ',6 data:{code: "$", desc: "I am Dingzhen"},7 success:function (text) {8 alert (text);9 },Ten error:function (data) { One alert ("Background exception! ") A }, - Asyn:false, - Cache:false the }); -}
Iv. Server-side specifies the returned data format
1 /**2 * Test custom message Converters3 * produces= "application/x-result;charset=utf-8" must be added to the request header.4 * @paramRequest5 * @paramResponse6 * @return7 */8@RequestMapping (value= "/test6",produces= "Application/x-result;charset=utf-8")9 @ResponseBodyTen Publicresultinfo Test6 (httpservletrequest request,httpservletresponse response) { OneResultinfo result =Newresultinfo (); AResult.setcode ("200"); -Result.setdesc ("The request was successful! "); -map<string,string> map =NewHashmap<string,string>(); theMap.put ("name", "Crimson"); -Map.put ("Age", "22"); - result.setdata (map); - returnresult; +}
Here, it's almost over!
Note the point:
1. The contenttype of the request header must be set to the value of your custom data format
2. Return the data format if you need to use your custom data format, plus the routing settings. namely:produces= "Application/x-result;charset=utf-8"
springmvc-Custom Message Converters