springmvc@RequestBodyString类型参数
通过如下配置:
<bean id="mappingJacksonHttpMessageConverter"
class="org.verter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.hod.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.hod.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter"/>
<!-- JSON转换器 -->
</list>
</property>
</bean>
在 mvc的Controller层使⽤@RequestBody接收Content-Type为application/json的数据时,默认⽀持Map⽅式和对象⽅式参数
@RequestMapping(value = "/{code}/saveUser", method = RequestMethod.POST)
@ResponseBody
public JsonResult saveUser(@PathVariable("code") Integer code, @RequestBody Map<String, Object> datas,@RequestBody User user) {。。。
}
如果是⼀个参数时也需要⽤个Map或者对象处理,使⽤String会报解析错误,具体看:AbstractJackson2HttpMessageConverter的⽅法read(Type type, Class
@Override
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
JavaType javaType = getJavaType(type, contextClass);
return readJavaType(javaType, inputMessage);
}
private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
try {
return Body(), javaType);
mvc的controller}
catch (IOException ex) {
throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
}
}
为了让@RequestBody⽀持String参数(⽬前只⽀持接收单个参数)
重写org.verter.json.MappingJackson2HttpMessageConverter类
st.converter.json
import org.springframework.http.HttpInputMessage;
import org.verter.HttpMessageNotReadableException;
import org.verter.json.MappingJackson2HttpMessageConverter;
import java.io.IOException;
import flect.Type;
import java.util.LinkedHashMap;
/**
* 处理@RequestBody注解为String的情况,只⽀持接收单个参数的情况
* Created by test
* Date:2017/1/4
* Time:17:33
*/
public class CustomerMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
@Override
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { Class<?> deseriClazz = getClazz(clazz);
Object param = adInternal(deseriClazz, inputMessage);
return getTrueObject(clazz, param);
}
@Override
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
Type deseriType = getType(type);
Object param = ad(deseriType, contextClass, inputMessage);
return getTrueObject(type, param);
}
/**
* 通过返回参数类型决定是否处理参数,如果是String类型的参数,将解析后的HashMap⾥的值返回(只⽀持单个参数)
*
* @param type 返回参数类型
* @param param 参数值
* @return 实际参数值
*/
private Object getTrueObject(Type type, Object param) {
if (type == String.class) {
Object backParam = null;
if (param != null && param instanceof LinkedHashMap) {
LinkedHashMap paramMap = (LinkedHashMap) param;
if (paramMap.size() == 1) {
backParam = (paramMap.keySet().iterator().next());
}
}
param = backParam;
}
return param;
}
/**
* 获取解析参数⽤的Type
*
* @param type 参数类型
* @return
*/
private Type getType(Type type) {
Type deseriClazz;
if (type == String.class) {
//jackson不⽀持String默认⽤LinkedHashMap
deseriClazz = LinkedHashMap.class;
} else {
deseriClazz = type;
}
return deseriClazz;
}
}
/
**
* 获取解析参数⽤的Type
* @param clazz 参数类型
* @return
*/
private Class<?> getClazz(Class<?> clazz) {
Class<?> deseriClazz;
if (clazz == String.class) {
//jackson不⽀持String默认⽤LinkedHashMap
deseriClazz = LinkedHashMap.class;
} else {
deseriClazz = clazz;
}
return deseriClazz;
}
}
spring mvc xml配置⽂件修改:
<bean id="mappingJacksonHttpMessageConverter"
class="verter.json.CustomerMappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
Controller层:
@RequestMapping(value = "/delUser", method = RequestMethod.POST)
@ResponseBody
public JsonResult delUser(@RequestBody String id) {
。。。
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论