- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章SpringMVC @RequestBody的使用解析由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
Spring mvc是一个非常轻量的mvc框架,注解可以大大减少配置,让请求的拦截变得比较简单。这次记录下@RequestBody 注解接收参数尤其是数组参数的用法.
关于容器的配置不再多说,这里写出spring-servlet.xml的sechme
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"> <!-- 扫描包注解 --><context:component-scan base-package="xxxx"></context:component-scan><!-- mvc注解功能启动 --><mvc:annotation-driven /></beans>
只要对应包名下面的添加注解即可扫描到对应的控制器,一般采用@Controller 。
RequestBody接收基本类型 。
@Controllerpublic class TestController {// url请求拦截@RequestMapping("test/test.do")@ResponseBody // 返回参数为JSONpublic void test(@RequestBody String name) {System.out.println("getParams : " + name);}![这里写图片描述](https://img-blog.csdn.net/20161114115809292)}
@RequestBody只要接收POST请求Body里的数据.
这样发送请求,即可在java控制台中打印:
getParams : {"name":"micro"} 。
@RequestBody接收基本数组 。
然后我们接收基本类型数组:
@RequestMapping("test/test.do") @ResponseBody public void test(@RequestBody List<String> nameList) { System.out.println("getParams : " + nameList); }
这样即可获取到参数,不要body里写成了{“nameList”:[“name1”,“name2”]}这样会抛出异常.
@RequestBody是对应的POST请求的body,body即是获取的参数,如果想通过参数去获取,则要使用@RequestParams 注解:
@RequestMapping("test/test.do") @ResponseBody public void test(@RequestParam("name") String name) { System.out.println("getParams : " + name); }
注意是GET请求,参数直接放到URL后面,这样就可以使用@RequestParams获取到对应参数名的参数值。如果是复杂的对象.
@RequestBody的使用.
定义model:
class Person { private Long id; private String name; // setter getter}
@RequestBody接收复杂对象 。
接收参数的方式 。
@RequestMapping("test/test.do") @ResponseBody public void test(@RequestBody Person person) { System.out.println("getParams : " + person.getId() + " ," + person.getName()); }
即可获取到参数,body里的参数会自动匹配到person的属性并赋值.
注意名字要与对象的属性变量名一致。否则获取不到参数,例如这里就不能在body里写成{“i”:1,“name”:“micro”},这样获取到person的id为null.
@RequestBody接收复杂对象数组 。
如果是复杂对象数组:
@RequestMapping("test/test.do") @ResponseBody public void test(@RequestBody List<Person> personList) { for (Person p : personList) { System.out.println(p.getId() + " ," + p.getName()); } }
请求方式如下,注意body里的格式是[]数组.
控制台打印:
1 ,micro 。
2 ,micro2 。
即完成了@RequestBody接收各种类型的参数.
。
众所周知,springmvc中@RequestBody的注解是一个很实用的功能,它能帮我们解析客户端(移动设备、浏览器等)发送过来的json数据,并封装到实体类中.
但我今天要说的不是它的原理,而是记录一些工作中使用@RequestBody注解遇到的一些问题,也提醒广大java开发者避免类似的问题.
最近有个需求,接收客户的设备发送过来的json数据,客户的设备里面只能修改ip,然后通过http协议的post方式发送数据过来。我很自然地想到在登录页那里处理,在toLogin方法中增加@RequestBody Kehu kehu参数,用户解析并封装json数据.
废话不多说,上代码,如下所示:
@RequestMapping(value = "/toLogin") public ModelAndView toLogin(HttpServletRequest request, @RequestBody Kehu kehu) throws Exception { // 接收客户设备发送过来的json数据 if (kehu != null && !StringUtil.isEmpty(kehu.cmd)) { uploadData(kehu); } ModelAndView mv = new ModelAndView(); PageData pageData = this.getPageData(request); pageData.put("SYSNAME", Tools.readTxtFile(Const.SYSNAME)); // 读取系统名称 mv.setViewName("base/login"); mv.addObject("pd", pageData); return mv; }
一切看似很完美,在浏览器上测试一下,输入localhost(我的项目已经设置为了缺省项目,端口号也改为了80) 。
我傻眼了,报了400错误。如下图所示:
The request sent by the client was syntactically incorrect. 。
翻译过来就是:客户端发送的请求在语法上是不正确的.
没加@RequestBody Kehu kehu之前是正常的,问题肯定出在了这里。我一看RequestBody的源码:
package org.springframework.web.bind.annotation; import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target; import org.springframework.http.converter.HttpMessageConverter;/*** Annotation indicating a method parameter should be bound to the body of the web request.* The body of the request is passed through an {@link HttpMessageConverter} to resolve the* method argument depending on the content type of the request. Optionally, automatic* validation can be applied by annotating the argument with {@code @Valid}.** <p>Supported for annotated handler methods in Servlet environments.** @author Arjen Poutsma* @since 3.0* @see RequestHeader* @see ResponseBody* @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter* @see org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter*/@Target(ElementType.PARAMETER)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface RequestBody { /** * Whether body content is required. * <p>Default is {@code true}, leading to an exception thrown in case * there is no body content. Switch this to {@code false} if you prefer * {@code null} to be passed when the body content is {@code null}. * @since 3.2 */ boolean required() default true;}
required方法默认返回值是true.
这样问题就明朗了,我请求localhost的时候,没有传json过去,所以就会报400错误,因为客户端发送的请求在语法上是不正确的.
解决方法:在@RequestBody后面加上(required=false)就可以了。表示kehu对象可以不传入.
/** * 访问登录页 * @RequestBody(required=false) 表示kehu对象可以不传入。 * 一定要加上required=false,否则登录的时候会报400错误。错误代码: * The request sent by the client was syntactically incorrect. * @return * @throws Exception */ @RequestMapping(value = "/toLogin") public ModelAndView toLogin(HttpServletRequest request, @RequestBody(required=false) Kehu kehu) throws Exception { // 接收硬币机发送过来的json数据 if (kehu != null && !StringUtil.isEmpty(kehu.cmd)) { uploadData(kehu); } ModelAndView mv = new ModelAndView(); PageData pageData = this.getPageData(request); pageData.put("SYSNAME", Tools.readTxtFile(Const.SYSNAME)); // 读取系统名称 mv.setViewName("base/login"); mv.addObject("pd", pageData); return mv; }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我.
原文链接:https://blog.csdn.net/micro_hz/article/details/53157738 。
最后此篇关于SpringMVC @RequestBody的使用解析的文章就讲到这里了,如果你想了解更多关于SpringMVC @RequestBody的使用解析的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
基于SpringBoot版本如下: org.springframework.boot spring-boot-starter-parent 2.5.2
@RestController public class TestController { @GetMapping("/download") public ResponseEntit
概述 记得之前跟前端同事联调接口的时候,后端SpringMVC需要接收数组类型的参数,然后跟前端说需要传数组类型过来。后来前端童鞋传了数组,但是后端接收不成功,联调失败。那时候由于时间关系没有仔细研究
web.xml 片段: contextConfigLocation /WEB-INF/applicationContext-security.xml a
目录 相关准备 功能清单 具体功能:访问首页 ①配置view-controller ②创建页面
Spring mvc是一个非常轻量的mvc框架,注解可以大大减少配置,让请求的拦截变得比较简单。这次记录下@RequestBody 注解接收参数尤其是数组参数的用法。 关于容器的配置不再多说,这里
目录 SpringMVC默认处理的几种异常 @ResponseStatus 异常处理的顺序 自定义异常类(SpringMVC的异常处理)
目录 SpringMVC 接收前端传递的参数四种方式 @RequestParam 获取注解 @PathVariable获取注解 Sp
目录 @PathVariable的用法解析 问题描述 解析过程 动态参数使用@PathVariable
目录 SpringMVC @NotNull校验不生效 加了两个依赖问题解决 @NotNull注解失效原因之一 Lo
springmvc―handlermapping三种映射 handlermapping负责映射中央处理器转发给controller的映射策略,简单说就是控制中央处理器的请求触发哪一个control
目录 使用ModelAndView向request域对象共享数据 使用Model向request域对象共享数据 使用map向request域对象共享数据
整合SSM 环境要求 环境: IDEA MySQL5.7.19 Tomcat9 Maven3.6 要求: 需要熟练掌握MySQL数据库,Spring,Ja
目录 1、SpringMVC简介 2、工作流程与介绍 3、代码截图 以下组件通常使用框架提供实现: 1、Di
简介 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。开发者可以自己定义一些拦截器来实现特定的功能。 过滤器
背景 举个例子,出现中文乱码的例子:提交表单的时候。 表单 ?
请求进入DispatcherServlet的doDispatch后,获取HandlerMethod。然后根据HandlerMethod来确认HandlerApapter,确认后执行HandlerAd
实现需求: 1.用户未登录,跳转到登录页,登录完成后会跳到初始访问页。 2.用户自定义处理(如需要激活),跳转到激活页面,激活完成后会跳到初始访问页。 使用到的框架 springmvc 的拦
为了实现用户登录拦截你是否写过如下代码呢? 1. 基于Filter ?
springmvc dao层和service层的区别 首先解释面上意思,service是业务层,dao是数据访问层 这个问题我曾经也有过,记得以前刚学编程的时候,都是在service里直接调用d
我是一名优秀的程序员,十分优秀!