Spring MVC各种提交形式以及参数接收(form表单提交以及Json提交)

简介: form表单参数接收的三种方式

form表单参数接收的三种方式:

1.HttpServletRequest

   @RequestMapping(value = "/getParmByReq.do", method = RequestMethod.POST)

@RequestMapping(value = "/getParmByReq.do", method = RequestMethod.POST)
    @ResponseBody
    public String getParmByReq(HttpServletRequest request) {
        String name = request.getParameter("name");
        int age = Integer.parseInt(request.getParameter("age"));
        System.out.println("接收到的参数,name=" + name + ";age=" + age);
        return "接收到的参数,name=" + name + ";age=" + age;
    }

用curl 测试结果: (-d 表示post请求)

curl   -d "name=admin&age=34"   http://localhost:8080/ssm-req/form/getParmByReq.do

133b95d52499942d7c8479f3424417fd_2018113022143855.png

2. 一个个参数接收,单个参数按照名称接收:

@RequestMapping(value = "/requestParam.do", method = RequestMethod.POST)
    @ResponseBody
    public String getBarBySimplePathWithRequestParam(
            @RequestParam(value = "name") String name,
            @RequestParam(value = "age") Integer age
    ) {
        System.out.println("接收到的参数,name=" + name + ";age=" + age);
        return "接收到的参数,name=" + name + ";age=" + age;
    }

用curl 测试结果:

curl   -d "name=ceshi&age=22"   http://localhost:8080/ssm-req/form/requestParam.do

adbcbd84aa262719c5e55e2bdd8472d3_20181130221905702.png

3. 自定义实体类接收,使用注解:@ModelAttribute

@RequestMapping(value = "modelAttribute.do", method = RequestMethod.POST)
    @ResponseBody
    public String getModelAttribute(@ModelAttribute() User user) {
        System.out.println("接收到的参数,name=" + user.getName() + ";age=" + user.getAge());
        return "接收到的参数,name=" + user.getName() + ";age=" + user.getAge();
    }

用curl 测试结果:

curl   -d "name=jaywei&age=33"   http://localhost:8080/ssm-req/form/modelAttribute.do

15490d948385017ce19490c0dbf60ff9_20181130222209300.png

4. 不用流的形式接收

@RequestMapping(value = "/getNotStream.do")
    @ResponseBody
    public String getNotStream(String order) {
        JSONObject jsonObject = JSONObject.parseObject(order);
        System.out.println("接收到的参数,name=" + jsonObject.getString("name") + ";age=" + jsonObject.getString("age"));
        return "接收到的参数,name=" + jsonObject.getString("name") + ";age=" + jsonObject.getString("age");
    }

用curl 测试结果

curl   -d "order={'name':'张三','age':12}"   http://localhost:8080/ssm-req/form//getNotStream.do

fa8cb045fe86e568b4f5e48ea5e4ebde_20181130223718787.png

小结:

推荐使用:@ModelAttribute 接收参数的方式。

PS: 解决中文乱码问题,在web.xml文件中加入字符过滤器:

<filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


JSON提交参数接收

使用@RequestBody注解,该注解是将参数放在请求体中

@RequestMapping(value = "getJson2.do",produces = "application/json")
    public String getJson2(@RequestBody String userStr) {
        User user = JSON.parseObject(userStr, User.class);
        System.out.println("接收到的参数,name=" + user.getName() + ";age=" + user.getAge());
        return JSON.toJSONString("接收到的参数,name=" + user.getName() + ";age=" + user.getAge());
    }

用curl测试结果

curl   -H "Content-Type:application/json" -X POST -d "{'name':'sange','age':12}"   http://localhost:8080/ssm-req/json/getJson2.do

8639bac2df980cc99cd516b1ecf46a9b_20181130232840134.png

源码地址:

https://github.com/XWxiaowei/JavaWeb/tree/master/ssm-request-demo


相关文章
|
4月前
|
JSON Java 数据格式
微服务——SpringBoot使用归纳——Spring Boot返回Json数据及数据封装——封装统一返回的数据结构
本文介绍了在Spring Boot中封装统一返回的数据结构的方法。通过定义一个泛型类`JsonResult&lt;T&gt;`,包含数据、状态码和提示信息三个属性,满足不同场景下的JSON返回需求。例如,无数据返回时可设置默认状态码&quot;0&quot;和消息&quot;操作成功!&quot;,有数据返回时也可自定义状态码和消息。同时,文章展示了如何在Controller中使用该结构,通过具体示例(如用户信息、列表和Map)说明其灵活性与便捷性。最后总结了Spring Boot中JSON数据返回的配置与实际项目中的应用技巧。
220 0
|
4月前
|
JSON Java fastjson
微服务——SpringBoot使用归纳——Spring Boot返回Json数据及数据封装——使用 fastJson 处理 null
本文介绍如何使用 fastJson 处理 null 值。与 Jackson 不同,fastJson 需要通过继承 `WebMvcConfigurationSupport` 类并覆盖 `configureMessageConverters` 方法来配置 null 值的处理方式。例如,可将 String 类型的 null 转为 &quot;&quot;,Number 类型的 null 转为 0,避免循环引用等。代码示例展示了具体实现步骤,包括引入相关依赖、设置序列化特性及解决中文乱码问题。
109 0
|
4月前
|
JSON Java fastjson
微服务——SpringBoot使用归纳——Spring Boot返回Json数据及数据封装——Spring Boot 默认对Json的处理
本文介绍了在Spring Boot中返回Json数据的方法及数据封装技巧。通过使用`@RestController`注解,可以轻松实现接口返回Json格式的数据,默认使用的Json解析框架是Jackson。文章详细讲解了如何处理不同数据类型(如类对象、List、Map)的Json转换,并提供了自定义配置以应对null值问题。此外,还对比了Jackson与阿里巴巴FastJson的特点,以及如何在项目中引入和配置FastJson,解决null值转换和中文乱码等问题。
319 0
|
4月前
|
JSON Java 数据格式
微服务——SpringBoot使用归纳——Spring Boot中的全局异常处理——定义返回的统一 json 结构
本课主要讲解Spring Boot中的全局异常处理方法。在项目开发中,各层操作难免会遇到各种异常,若逐一处理将导致代码耦合度高、维护困难。因此,需将异常处理从业务逻辑中分离,实现统一管理与友好反馈。本文通过定义一个简化的JsonResult类(含状态码code和消息msg),结合全局异常拦截器,展示如何封装并返回标准化的JSON响应,从而提升代码质量和用户体验。
79 0
|
5月前
|
消息中间件 Java Kafka
【Azure Kafka】使用Spring Cloud Stream Binder Kafka 发送并接收 Event Hub 消息及解决并发报错
reactor.core.publisher.Sinks$EmissionException: Spec. Rule 1.3 - onSubscribe, onNext, onError and onComplete signaled to a Subscriber MUST be signaled serially.
|
9月前
|
JSON 前端开发 Java
Spring MVC——获取参数和响应
本文介绍了如何在Spring框架中通过不同的注解和方法获取URL参数、上传文件、处理cookie和session、以及响应不同类型的数据。具体内容包括使用`@PathVariable`获取URL中的参数,使用`MultipartFile`上传文件,通过`HttpServletRequest`和`@CookieValue`获取cookie,通过`HttpSession`和`@SessionAttribute`获取session,以及如何返回静态页面、HTML代码片段、JSON数据,并设置HTTP状态码和响应头。
154 1
Spring MVC——获取参数和响应
|
9月前
|
JSON 前端开发 Java
【Spring】“请求“ 之传递 JSON 数据
【Spring】“请求“ 之传递 JSON 数据
146 2
|
9月前
|
前端开发 Java Spring
【Spring】“请求“ 之传递单个参数、传递多个参数和传递对象
【Spring】“请求“ 之传递单个参数、传递多个参数和传递对象
233 2
|
10月前
|
JSON API 数据格式
requests库中json参数与data参数使用方法的深入解析
选择 `data`或 `json`取决于你的具体需求,以及服务器端期望接收的数据格式。
537 2
|
11月前
|
JSON C语言 数据格式
Python导出隐马尔科夫模型参数到JSON文件C语言读取
Python导出隐马尔科夫模型参数到JSON文件C语言读取
78 1