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è)抽象類(lèi)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)換的類(lèi)
*/
@Override
protected boolean supports(Class<?> arg0) {
if(arg0==Phone.class){
return true;
}
return false;
}
/**
* 用于將返回的對(duì)象轉(zhuǎn)換成字符串顯示在網(wǎng)頁(yè)
*/
@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測(cè)試
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路徑訪問(wèn)過(guò)來(lái)時(shí),看到使用了@RequestBody注解,這個(gè)注解標(biāo)識(shí)這個(gè)類(lèi)要被消息轉(zhuǎn)換器處理,就會(huì)springmvcxml文件中讀到消息轉(zhuǎn)換器,然后進(jìn)入supports方法
//判斷這個(gè)類(lèi)是否被指定的轉(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路徑訪問(wèn)過(guò)來(lái)時(shí),看到使用了@RequestBody注解,這個(gè)注解標(biāo)識(shí)這個(gè)類(lèi)要被消息轉(zhuǎn)換器處理,就會(huì)springmvcxml文件中讀到消息轉(zhuǎn)換器,然后進(jìn)入supports方法
判斷這個(gè)類(lèi)是否被指定的轉(zhuǎn)換器支持,如果支持,就調(diào)用readInternal方法,進(jìn)行切割,然后將值傳遞到對(duì)象中.
處理完成為對(duì)象之后,就會(huì)調(diào)用writeInternal轉(zhuǎn)換為響應(yīng)頭
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javax.validation包里@NotNull等注解的使用方式
這篇文章主要介紹了javax.validation包里@NotNull等注解的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
SpringBoot如何實(shí)現(xiàn)并發(fā)任務(wù)并返回結(jié)果
這篇文章主要介紹了SpringBoot如何實(shí)現(xiàn)并發(fā)任務(wù)并返回結(jié)果問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
InputStreamReader和BufferedReader用法及實(shí)例講解
這篇文章主要介紹了InputStreamReader和BufferedReader用法及實(shí)例講解的相關(guān)資料,需要的朋友可以參考下2015-12-12
Java的GUI編程之列表和組合框的設(shè)計(jì)使用
這篇文章主要介紹了Java的GUI編程之列表和組合框的設(shè)計(jì)使用,是Java圖形界面編程中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10
Java實(shí)現(xiàn)的mysql事務(wù)處理操作示例
這篇文章主要介紹了Java實(shí)現(xiàn)的mysql事務(wù)處理操作,結(jié)合實(shí)例形式較為詳細(xì)的分析了Java基于JDBC操作mysql數(shù)據(jù)庫(kù)實(shí)現(xiàn)事務(wù)處理的相關(guān)概念、操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-08-08
SpringSecurity請(qǐng)求授權(quán)規(guī)則配置與注解詳解
這篇文章主要介紹了SpringSecurity請(qǐng)求授權(quán)規(guī)則配置與注解詳解,我們常使用@Secured與@PreAuthorize兩個(gè)注解在進(jìn)入方法前進(jìn)行角色、權(quán)限的控制,進(jìn)入方法前數(shù)據(jù)的過(guò)濾@PreFilter注解偶爾會(huì)看到,需要的朋友可以參考下2023-12-12
Mabitis中的#與$符號(hào)區(qū)別及用法介紹
這篇文章主要介紹了Mabitis中的#與$符號(hào)區(qū)別,需要的朋友可以參考下2017-02-02

