//domain
package com.crazy.goods.tools;/** * 0755-351512 * @author Administrator * */public class Phone {private String qno;private String number;public String getQno() {return qno; }public void setQno(String qno) {this.qno = qno; }public String getNumber() {return number; }public void setNumber(String number) {this.number = number; } }
//訊息轉換器 要實作一個抽象類別AbstractHttpMessageConverter
package com.crazy.goods.tools;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import org.springframework.http.HttpInputMessage;import org.springframework.http.HttpOutputMessage;import org.springframework.http.converter.AbstractHttpMessageConverter;import org.springframework.http.converter.HttpMessageNotReadableException;import org.springframework.http.converter.HttpMessageNotWritableException;public class MyMessageConvertor extends AbstractHttpMessageConverter<Phone> {/** * 将请求头数据转换成Phone */ @Overrideprotected Phone readInternal(Class<? extends Phone> arg0, HttpInputMessage msg) throws IOException, HttpMessageNotReadableException {//参数必须使用post提交必须在body中InputStream is=msg.getBody(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String param=br.readLine(); String phone=param.split("=")[1]; Phone phoneObj=new Phone(); phoneObj.setQno(phone.split("-")[0]); phoneObj.setNumber(phone.split("-")[1]);return phoneObj; }/** * 当前的转换器支持转换的类 */@Overrideprotected boolean supports(Class<?> arg0) {if(arg0==Phone.class){return true; }return false; }/** * 用于将返回的对象转换成字符串显示在网页 */@Overrideprotected void writeInternal(Phone phone, HttpOutputMessage arg1)throws IOException, HttpMessageNotWritableException { String p=phone.getQno()+"-"+phone.getNumber(); arg1.getBody().write(p.getBytes("UTF-8")); } }
//springmvc.xml 要設定bean:訊息轉換器,只有post提交方式才會被轉換器攔截
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/context http://www.springframework.org/schema/tx http://www.springframework.org/schema/aop http://www.springframework.org/schema/mvc "> <!--springmvc只能扫描控制层 --> <context:component-scan base-package="com.crazy.goods"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan> <!--消息转换器 必须使用post提交 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="com.crazy.goods.tools.MyMessageConvertor"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/x-www-form-urlencoded</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> </beans>
servlet測試
<br>
package com.crazy.goods.servlet;
<br>
import java.io.IOException;
<br>
import javax.servlet.ServletException;<br>import javax.servlet.annotation.WebServlet;<br>import javax.servlet.http. HttpServlet;<br>import javax.servlet.http.HttpServletRequest;<br>import javax.servlet.http.HttpServletResponse;
<br>
import org.springframework.stereotype.Controller;<br>
<br><br>import org.springframework.stereotype.Controller;## web.bind.annotation.PathVariable;<br>import org.springframework.web.bind.annotation.RequestBody;<br>import org.springframework.web.bind.annotation.RequestMapping;<br>import org.springframework.web. bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;
<br>import com.crazy.goods.tools.Phone;
<br><br>/** <br> * @author Administrator<br> * 建立時間:2017年7月1日下午3:11:27<br>*/<br>@Controllerpublic class ReservePageServelt {
<br><br>// /**<br>// * forward:轉送<br>// * redirect:重定向<br>// * @param req<br>// * @param resp<br>// * @return<br>// * @throws ServletException<br>// * @throws IOException<br>//*/<br>// @RequestMapping(value="/add",method ={RequestMethod.GET})<br>// public String doGet(HttpServletRequest req, HttpServletResponse resp/*,@PathVariable("testid") String testid*/) throws ServletException, IOIO ( "/reversegood.jsp").forward(req, resp);<br>// return "/reversegood.jsp";<br>// resp.getWriter().print(testid);<br>// } <br><br><br> //訊息轉換器思路,<br><br> //原理就是將請求體或請求頭的資料轉換為action方法的參數,同時將方法的傳回值的內容轉換為回應頭<br> //當url路徑存取過來時,看到使用了@RequestBody註解,這個註解標識這個類別要被訊息轉換器處理,就會springmvcxml檔案中讀到訊息轉換器,然後進入supports方法<br> //判斷這個類別是否被指定的轉換器支持,如果支持,就呼叫readInternal方法,進行切割,然後將值傳遞到物件中,處理完成為物件之後,就會呼叫writeInternal轉換為回應頭<br> @RequestMapping(value="/add")<br> @ResponseBody<br> public Phone messageConvertor( @RequestBody Phone phone,HttpServletResponse response) {<br> System.out.println(phone.getQno()+phone. getNumber());<br> return phone;<br><br> }<br><br>}rrreee
總結:訊息轉換器的原理就是,自訂將請求體的資料轉換為形參(物件),然後將方法的回傳值內容轉換為回應頭
#步驟:
當url路徑存取過來時,看到使用了@RequestBody註解,這個註解標識這個類別要被訊息轉換器處理,就會springmvcxml檔案中讀到訊息轉換器,然後進入supports方法<br>判斷這個類別是否被指定的轉換器支持,如果支持,就呼叫readInternal方法,進行切割,然後將值傳遞到物件中.
處理完成為物件之後,就會呼叫writeInternal轉換為回應頭
以上是spring之訊息轉換器的實例代碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!