- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章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.jsonimport 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) { 。。。 }
。
注解为 @Controller @RequestMapping 。
可以看到传递的为SpringMVC的BindingAwareModelMap类型,SpringMVC中的隐含模型就是这个类型,其作用域等价于 request 域,当添加Model、ModelMap参数时,SpringMVC实际传入的就是这个隐含模型;向这个隐含模型种设置值后,在返回的页面中就能通过request域取值.
结果类型还是一样,且参数不会被传入,当然使用request肯定能取出来.
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 。
最后此篇关于springmvc @RequestBody String类型参数的使用的文章就讲到这里了,如果你想了解更多关于springmvc @RequestBody String类型参数的使用的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
当我定义 requestBody 时,它不会显示在 swagger 文档中。我想为 swagger 中的 gpx 文件创建一组图像和单个文件上传。如何才能实现 requestBody 像参数属性一样显
我正在使用 Spring 4.x 做 Spring Rest Api 项目 这有效: Controller.java @PostMapping("newTransaction") Transactio
我正在向服务器发送一个 JSON 对象,该服务器将填充我的域对象报告。 public class CustomReport {String name; String name; String emai
我有一个非常琐碎的问题,它占用了我很多时间。 我有一个 Spring Rest 服务,它接受 @RequestBody 中的模型对象。我在模型对象中传递的是格式为 yyyy-MM-dd'T'HH:mm
我一直在开发 Spring Boot Rest api 服务,这是我的 Rest Controller 方法之一: @RequestMapping(value = "manager", method
我使用 Spring Boot 2.0.8.RELEASE。 我有一个 @RestController 方法,如下所示: @PostMapping("/message") public
类(class): class Person { private String name; private List addr; } c
我有以下类作为我的@RequestBody,但我无法发送我的请求,我尝试以不同的格式准备 JSON,但它们都不起作用。 处理程序 @RequestMapping("/grades") @Respons
@PatchMapping("/update") HttpEntity updateOnlyIfFieldIsPresent(@RequestBody Person person) { if(
我正在尝试使用 apache 集合中的 MultiValueMap(MultiMap 的实现)。我正在使用 Spring MVC 的 @RequestBody 注释。但是,我不断收到 HTTPMedi
Requestbody 没有映射到我在这里使用的对象。 PaymentRequest 中的所有字段都为空。我看到注释和映射一切似乎都是正确的。 @RestController @RequestMapp
假设以下 JSON: { "attr_A": "val_A", "array_A": [{ "attr_B": "val_B" }] } 以及以下两个类: public class C
JavaScript 代码: function deleteMarks(list){ $http.post('/api/marks/delete/all',list).then( fu
我是 Java/Spring 的新手,正在尝试在现有项目中设置 API 端点。我基本上已经复制了一些当前正在工作的其他端点,但我的端点在被击中时没有验证,这似乎是因为 @RequestBody 没有填
我正在使用 postman 来测试我的请求。所以我从 postman 那里选择了显示代码 fragment 的选项并选择了 OkHttp fragment 是: OkHttpClient client
我使用 Spring-boot 2.0.1 和 WebFlux 作为 Rest 服务器。 在我的 RestController 中,我想自动反序列化一个对象(产品)。但我收到 Jackson 错误,就
我创建了一个简单的 REST 服务 (POST)。但是当我从 postman 那里调用这个服务时,@RequestBody 没有收到任何值。 import org.springframework.ht
PostMan传参给@RequestBody(接受前端参数) 今天新接手一个项目框架,需要改造,但后台写好方法,准备用postman 测试时候,发现用以前传参方式不行,需要需要将json字符串转成
springmvc @RequestBody String类型参数 通过如下配置: <bean id="mappingJacksonHttpMessageConverter&qu
目录 SpringMVC @RequestBody为null 关于inputsteam的一些理解 @RequestBody 自动映射原理的简单介
我是一名优秀的程序员,十分优秀!