spring消息轉(zhuǎn)換器使用詳解
本文實(shí)例為大家分享了spring消息轉(zhuǎn)換器的具體代碼,供大家參考,具體內(nèi)容如下
//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;
}
}
//消息轉(zhuǎn)換器 要實(shí)現(xiàn)一個(gè)抽象類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> {
/**
* 將請(qǐng)求頭數(shù)據(jù)轉(zhuǎn)換成Phone
*/
@Override
protected Phone readInternal(Class<? extends Phone> arg0,
HttpInputMessage msg) throws IOException,
HttpMessageNotReadableException {
//參數(shù)必須使用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;
}
/**
* 當(dāng)前的轉(zhuǎn)換器支持轉(zhuǎn)換的類
*/
@Override
protected boolean supports(Class<?> arg0) {
if(arg0==Phone.class){
return true;
}
return false;
}
/**
* 用于將返回的對(duì)象轉(zhuǎn)換成字符串顯示在網(wǎng)頁
*/
@Override
protected 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:消息轉(zhuǎn)換器,只有post提交方式才會(huì)被轉(zhuǎn)換器攔截
<?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/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--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>
<!--消息轉(zhuǎn)換器 必須使用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測試
package com.crazy.goods.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.crazy.goods.tools.Phone;
/**
* @author Administrator
* 創(chuàng)建時(shí)間:2017年7月1日下午3:11:27
*/
@Controller
public class ReservePageServelt {
// /**
// * forward:轉(zhuǎn)發(fā)
// * redirect:重定向
// * @param req
// * @param resp
// * @return
// * @throws ServletException
// * @throws IOException
// */
// @RequestMapping(value="/add",method={RequestMethod.GET})
// public String doGet(HttpServletRequest req, HttpServletResponse resp/*,@PathVariable("testid") String testid*/) throws ServletException, IOException {
// req.getRequestDispatcher("/reversegood.jsp").forward(req, resp);
// return "/reversegood.jsp";
// resp.getWriter().print(testid);
// }
//消息轉(zhuǎn)換器思路,
//原理就是將請(qǐng)求體或者請(qǐng)求頭的數(shù)據(jù)轉(zhuǎn)換為action方法的參數(shù),同時(shí)將方法的返回值的內(nèi)容轉(zhuǎn)換為響應(yīng)頭
//當(dāng)url路徑訪問過來時(shí),看到使用了@RequestBody注解,這個(gè)注解標(biāo)識(shí)這個(gè)類要被消息轉(zhuǎn)換器處理,就會(huì)springmvcxml文件中讀到消息轉(zhuǎn)換器,然后進(jìn)入supports方法
//判斷這個(gè)類是否被指定的轉(zhuǎn)換器支持,如果支持,就調(diào)用readInternal方法,進(jìn)行切割,然后將值傳遞到對(duì)象中,處理完成為對(duì)象之后,就會(huì)調(diào)用writeInternal轉(zhuǎn)換為響應(yīng)頭
@RequestMapping(value="/add")
@ResponseBody
public Phone messageConvertor( @RequestBody Phone phone,HttpServletResponse response) {
System.out.println(phone.getQno()+phone.getNumber());
return phone;
}
}
總結(jié):消息轉(zhuǎn)換器的原理就是,自定義將請(qǐng)求體的數(shù)據(jù)轉(zhuǎn)換為形參(對(duì)象),然后將方法的返回值內(nèi)容轉(zhuǎn)換為響應(yīng)頭
步驟:
當(dāng)url路徑訪問過來時(shí),看到使用了@RequestBody注解,這個(gè)注解標(biāo)識(shí)這個(gè)類要被消息轉(zhuǎn)換器處理,就會(huì)springmvcxml文件中讀到消息轉(zhuǎn)換器,然后進(jìn)入supports方法
判斷這個(gè)類是否被指定的轉(zhuǎn)換器支持,如果支持,就調(diào)用readInternal方法,進(jìn)行切割,然后將值傳遞到對(duì)象中.
處理完成為對(duì)象之后,就會(huì)調(diào)用writeInternal轉(zhuǎn)換為響應(yīng)頭
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringMVC中的DispatcherServlet請(qǐng)求分析
這篇文章主要介紹了SpringMVC中的DispatcherServlet請(qǐng)求分析, DispatcherServlet作為一個(gè)Servlet,那么當(dāng)有請(qǐng)求到Tomcat等Servlet服務(wù)器時(shí),會(huì)調(diào)用其service方法,再調(diào)用到其父類GenericServlet的service方法,需要的朋友可以參考下2024-01-01
在java8中使用流區(qū)分質(zhì)數(shù)與非質(zhì)數(shù)詳解
這篇文章主要介紹了在java8中使用流區(qū)分質(zhì)數(shù)與非質(zhì)數(shù)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12
Java?SpringBoot集成文件之如何使用POI導(dǎo)出Word文檔
這篇文章主要介紹了Java?SpringBoot集成文件之如何使用POI導(dǎo)出Word文檔,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08
手把手帶你理解java線程池之工作隊(duì)列workQueue
這篇文章主要介紹了java線程池之工作隊(duì)列workQueue,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
springboot+swagger2.10.5+mybatis-plus 入門詳解
這篇文章主要介紹了springboot+swagger2.10.5+mybatis-plus 入門,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12

