springmvc @RequestBody String類型參數
通過如下配置:
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.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.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/> <!-- JSON轉換器 --> </list> </property> </bean>
在spring 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 this.objectMapper.readValue(inputMessage.getBody(), javaType); } catch (IOException ex) { throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex); } }
為了讓@RequestBody支持String參數(目前只支持接收單個參數)
重寫org.springframework.http.converter.json.MappingJackson2HttpMessageConverter類
package com.test.converter.json import org.springframework.http.HttpInputMessage; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import java.io.IOException; import java.lang.reflect.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 = super.readInternal(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 = super.read(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.get(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="com.test.converter.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) { 。。。 }
springmvc用Map接收請求參數分析
第一種情況,什么也不設置,無參數傳遞
注解為 @Controller @RequestMapping
可以看到傳遞的為SpringMVC的BindingAwareModelMap類型,SpringMVC中的隱含模型就是這個類型,其作用域等價于 request 域,當添加Model、ModelMap參數時,SpringMVC實際傳入的就是這個隱含模型;向這個隱含模型種設置值后,在返回的頁面中就能通過request域取值。
第二種情況,加個參數試試 => .../testmap?test1=2342
結果類型還是一樣,且參數不會被傳入,當然使用request肯定能取出來。
第三種情況,給Map參數添加@RequestParam注解
1、Get請求 =>http://localhost:8080/ssm/v2/testmap?test1=234234
成功傳入了參數,注意這個Map類型為LinkedHashMap,而不是隱含模型了
再添加個Model參數看看,隱含模型中依然沒有值
所以添加@RequestParam注解后,SpringMVC會將 Get 請求中封裝進對應的參數中,如果參數是Map就封裝稱LinkedHashMap而不再傳入隱含模型
2、Post請求, 再測試測試Post請求
與Get的結果一致:參數無@RequestParam注解時,Map接收隱含模型;添加@RequestParam注解時,Map接收LinkedHashMap;隱含模型中無值。
第四種情況,給Map參數添加@RequestBody注解,且請求方式為Post
出乎意料的也成功傳入了,與@RequestParam注解結果類似,也是LinkedHashMap
復雜點的Json數據也能解析接收成功
小結一下吧
SpringMVC處理請求用Map類型接收參數時,如果參數無注解,則會傳入BindingAwareModelMap類型,等價于Model、ModelMap參數;
參數添加@RequestParam注解時,會將參數包裝稱LinkedHashMap對象,參數的key為Map的key,參數值為Map的key,支持Get、Post方法(應該支持Put、Delete,沒有測,倆方法與Post類似);
添加@RequestBody注解時,接收Json類型數據,也會包裝成LinkedHashMap對象,該注解不支持Get請求,Get請求沒有請求體不能傳Json。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/yangxingzou/article/details/54092578