Flask接口签名sign原理与实例代码浅析
296
2023-05-02
spring消息转换器使用详解
本文实例为大家分享了spring消息转换器的具体代码,供大家参考,具体内容如下
//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
*/
@Override
protected 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;
}
/**
* 当前的转换器支持转换的类
*/
@Override
protected boolean supports(Class> arg0) {
if(arg0==Phone.class){
return true;
}
return false;
}
/**
* 用于将返回的对象转换http://成字符串显示在网页
*/
@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:消息转换器,只有post提交方式才会被转换器拦截
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:tx="http://springframework.org/schema/tx" xmlns:aop="http://springframework.org/schema/aop" xmlns:mvc="http://springframework.org/schema/mvc" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd ">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context"
xmlns:tx="http://springframework.org/schema/tx"
xmlns:aop="http://springframework.org/schema/aop"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd
http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd
http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd
">
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
* 创建时间:2017年7月1日下午3:11:27
*/
@Controller
public class ReservePageServelt {
// /**
// * forward:转发
// * 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);
// }
//消息转换器思路,
//原理就是将请求体或者请求头的数据转换为action方法的参数,同时将方法的返回值的内容转换为响应头
//当url路径访问过来时,看到使用了@RequestBody注解,这个注解标识这个类要被消息转换器处理,就会springmvcxml文件中读到消息转换器,然后进入supports方法
//判断这个类是否被指定的转换器支持,如果支持,就调用readInternal方法,进行切割,然后将值传递到对象中,处理完成为对象之后,就会调用writeInternal转换为响应头
@RequestMapping(value="/add")
@ResponseBody
public Phone messageConvertor( @RequestBody Phone phone,HttpServletResponse response) {
System.out.println(phone.getQno()+phone.getNumber());
return phone;
}
}
总结:消息转换器的原理就是,自定义将请求体的数据转换为形参(对象),然后将方法的返回值内容转换为响应头
步骤:
当url路径访问过来时,看到使用了@RequestBody注解,这个注解标识这个类要被消息转换器处理,就会springmvcxml文件中读到消息转换器,然后进入supports方法
判断这个类是否被指定的转换器支持,如果支持,就调用readInternal方法,进行切割,然后将值传递到对象中.
处理完成为对象之后,就会调用writeInternal转换为响应头
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~