springMVC配置消息转换器

本文介绍如何在Spring MVC中配置自定义的消息转换器,通过mvc:annotation-driven注册并使用@RequestBody和@ResponseBody注解来处理HTTP请求和响应。文章详细展示了配置步骤及实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


对于定义的消息转换器 必须通过 mvc:annotation-driven进行注册 消息转换器 

会对请求的mini类型进行匹配 如果无法匹配 不会进行消息转换


配置springMvc.xml:

<mvc:annotation-driven>
	<mvc:message-converters>
		<bean id="myConvetor" class="cn.et.springmvc.lesson02.message.MyMessageConvertor">
			<property name="supportedMediaTypes">
				<list>
					<!-- 设置响应支持的类型 -->
					<value>text/html;charset=UTF-8</value>
					<!-- 设置请求body支持的类型 -->
					<value>application/x-www-form-urlencoded</value>
				</list>
			</property>
		</bean>
	</mvc:message-converters>
</mvc:annotation-driven>





定义一个消息转换器处理类,该类必须继承AbstractHttpMessageConverter<?>

消息转换器处理类:
package cn.et.springmvc.lesson02.message;

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
	 */
	@Override
	protected Phone readInternal(Class<? extends Phone> arg0,
			HttpInputMessage msg) throws IOException,
			HttpMessageNotReadableException {
		InputStream is = msg.getBody();
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String param = br.readLine();
		Phone phone = new Phone();
		param = param.split("=")[1];
		phone.setQno(param.split("-")[0]);
		phone.setNumber(param.split("-")[1]);
		return phone;
	}

	/**
	 * 当前的转换器支持转换的类
	 */
	@Override
	protected boolean supports(Class<?> arg0) {
		if (arg0 == Phone.class) {
			return true;
		}
		return false;
	}

	/**
	 * 用于将返回对象转换成字符串显示网页上 该方法可以实现Action的返回值不一定为String
	 */
	@Override
	protected void writeInternal(Phone phone, HttpOutputMessage arg1)
			throws IOException, HttpMessageNotWritableException {
		String p = phone.getQno() + "-" + phone.getNumber();
		arg1.getBody().write(p.getBytes("utf-8"));
	}

}




Action类的需要用到@RequestBody和@ResponseBody注解关联到MyMessageConvertor

Action类:
package cn.et.springmvc.lesson02.message;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 消息转换器 将请求头的数据转换成 action方法的参数 将方法的返回值的内容转换成响应头
 * 
 * @author Administrator
 * 
 */
@Controller
public class MessageAction {

	@RequestMapping(value = "/user")
	public @ResponseBody
	Phone user(@RequestBody Phone phone) {
		System.out.println(phone.getQno() + phone.getNumber());
		return phone;
	}
}




编写jsp文件跳转的动作只能是form表单,并且添加属性enctype="application/x-www-form-urlencoded"
jsp入口:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'index.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
	</head>

	<body>
		<form action="${public String _elExpressionpageContext.getRequest().getContextPath()}/user" method="post"
			enctype="application/x-www-form-urlencoded">
			<input type="hidden" name="phone" value="0755-123456789" />
			<input type="submit" />
		</form>
	</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值