一、接收 Form 表单数据

1,基本的接收方法

(1)下面样例Controller接收form-data格式的POST数据:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @PostMapping("/postHello1")
    public String postHello1(@RequestParam("name") String name,
                        @RequestParam("age") Integer age) {
        return "name:"   name   "\nage:"   age;
    }
}

(2)下面是一个简单的测试样例:

2,参数没有传递的情况

(1)如果没有传递参数Controller将会报错,这个同样有如下两种解决办法:

  • 使用required = false标注参数是非必须的。
  • 使用defaultValue给参数指定个默认值。
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @PostMapping("/postHello2")
    public String postHello2(@RequestParam(name = "name", defaultValue = "xxx") String name,
                        @RequestParam(name = "age", required = false) Integer age) {
        return "name:"   name   "\nage:"   age;
    }
}

3,使用 map 来接收参数

(1)Controller还可以直接使用map来接收所有的请求参数:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @PostMapping("/postHello2")
    public String postHello2(@RequestParam(name = "name", defaultValue = "xxx") String name,
                        @RequestParam(name = "age", required = false) Integer age) {
        return "name:"   name   "\nage:"   age;
    }
}

(2)下面是一个简单的测试样例:

4,接收一个数组

(1)表单中有多个同名参数,Controller这边可以定义一个数据进行接收:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.Map;
 
@RestController
public class HelloController {
    @PostMapping("/postHello4")
    public String postHello4(@RequestParam("name") String[] names) {
        String result = "";
        for(String name:names){
            result  = name   "\n";
        }
        return result;
    }
}

(2)下面是一个简单的测试样例:

5,使用对象来接收参数

1)如果一个post请求的参数太多,我们构造一个对象来简化参数的接收方式:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @PostMapping("/postHello5")
    public String postHello5(User user) {
        return "name:"   user.getName()   "\nage:"   user.getAge();
    }
}

(2)User类的定义如下,到时可以直接将多个参数通过getter、setter方法注入到对象中去:

public class User {
    private String name;
    private Integer age;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Integer getAge() {
        return age;
    }
 
    public void setAge(Integer age) {
        this.age = age;
    }
}

(3)下面是一个简单的测试样例:

(4)如果传递的参数有前缀,且前缀与接收实体类的名称相同,那么参数也是可以正常传递的:

(5)如果一个 post 请求的参数分属不同的对象,也可以使用多个对象来接收参数:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @PostMapping("/postHello5-1")
    public String hello(User user, Phone phone) {
        return "name:"   user.getName()   "\nage:"   user.getAge()
                  "\nnumber:"   phone.getNumber();
    }
}

6,使用对象接收时指定参数前缀

(1)如果传递的参数有前缀,且前缀与接收实体类的名称不同相,那么参数无法正常传递:

(2)我们可以结合@InitBinder解决这个问题,通过参数预处理来指定使用的前缀为 u.

除了在 Controller 里单独定义预处理方法外,我们还可以通过@ControllerAdvice结合@InitBinder来定义全局的参数预处理方法,方便各个Controller使用。具体做法参考我之前的文章:

SpringBoot - @ControllerAdvice的使用详解3(请求参数预处理 @InitBinder)

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
 
@RestController
public class HelloController {
    @PostMapping("/postHello6")
    public String postHello6(@ModelAttribute("u") User user) {
        return "name:"   user.getName()   "\nage:"   user.getAge();
    }
 
    @InitBinder("u")
    private void initBinder(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("u.");
    }
}

(3)重启程序再次发送请求,可以看到参数已经成功接收了:

二、接收字符串文本数据

(1)如果传递过来的是Text文本,我们可以通过HttpServletRequest获取输入流从而读取文本内容。

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
 
@RestController
public class HelloController {
    @PostMapping("/helloText")
    public String hello(HttpServletRequest request) {
        ServletInputStream is = null;
        try {
            is = request.getInputStream();
            StringBuilder sb = new StringBuilder();
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = is.read(buf)) != -1) {
                sb.append(new String(buf, 0, len));
            }
            System.out.println(sb.toString());
            return "获取到的文本内容为:"   sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

三、接收 JSON 数据

1,使用 Map 来接收数据

(1)如果把json作为参数传递,我们可以使用@requestbody接收参数,将数据转换Map:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @PostMapping("/helloBean")
    public String hello(@RequestBody User user){
        return user.getName()   " "   user.getAge();
    }
}

(2)下面是一个简单的测试样例:

2,使用 Bean 对象来接收数据

(1)如果把json作为参数传递,我们可以使用@requestbody接收参数,将数据直接转换成对象:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @PostMapping("/helloBean")
    public String hello(@RequestBody User user){
        return user.getName()   " "   user.getAge();
    }
}

(2)下面是一个简单的测试样例:

(4)如果传递的JOSN数据是一个数组也是可以的,Controller做如下修改:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
 
@RestController
public class HelloController {
    @PostMapping("/helloList")
    public String helloList(@RequestBody List<User> users){
        String result = "";
        for(User user:users){
            result  = user.getName()   " "   user.getAge()   "\n";
        }
        return result;
    }
}

到此这篇关于Springboot接收 Form 表单数据的文章就介绍到这了,更多相关Springboot接收表单数据内容请搜索Devmax以前的文章或继续浏览下面的相关文章希望大家以后多多支持Devmax!

Springboot接收 Form 表单数据的示例详解的更多相关文章

  1. HTML5新增form控件和表单属性实例代码详解

    这篇文章主要介绍了HTML5新增form控件和表单属性实例代码详解,需要的朋友可以参考下

  2. HTML5表单验证特性(知识点小结)

    这篇文章主要介绍了HTML5表单验证特性的一些知识点,本文通过实例代码截图的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  3. amazeui页面分析之登录页面的示例代码

    这篇文章主要介绍了amazeui页面分析之登录页面的示例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  4. ios – Swift Eureka Form中的循环

    我正在构建一个Eureka表单,并希望在表单中放置一个循环来构建基于数组的步进器列表.我试图使用的代码是:但是,当我这样做时,我在StepperRow行上出现了一个错误:所以看起来Swift不再认为它在形式之内并且正在关注

  5. swift 上传图片和参数 upload image with params

    Alamofire.upload(urlRequest.0,urlRequest.1).progress{(bytesWritten,totalBytesWritten,totalBytesExpectedToWrite)inprintln("\(totalBytesWritten)/\(totalBytesExpectedToWrite)")}}

  6. swift – 使用PostgreSQL在Vapor 3中上传图片

    我正在关注这些家伙MartinLasek教程,现在我正在“图片上传”.似乎没有人能回答“如何上传iVapor3图像”的问题Db连接正常,所有其他值都保存.这是我的创建方法:和型号:}和叶子模板:我知道需要一种管理文件的方法和原始图像字节,但我怎么去那里?这使用多部分表单的自动解码:upload.leaf文件是:使用File类型可以访问上载文件的本地文件名以及文件数据.如果将其余的Question字段添加到ExampleUpload结构中,则可以使用该路径捕获整个表单的字段.

  7. SpringBoot本地磁盘映射问题

    这篇文章主要介绍了SpringBoot本地磁盘映射问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  8. java SpringBoot 分布式事务的解决方案(JTA+Atomic+多数据源)

    这篇文章主要介绍了java SpringBoot 分布式事务的解决方案(JTA+Atomic+多数据源),文章围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下

  9. SpringBoot整合Javamail实现邮件发送的详细过程

    日常开发过程中,我们经常需要使用到邮件发送任务,比方说验证码的发送、日常信息的通知等,下面这篇文章主要给大家介绍了关于SpringBoot整合Javamail实现邮件发送的详细过程,需要的朋友可以参考下

  10. SpringBoot详细讲解视图整合引擎thymeleaf

    这篇文章主要分享了Spring Boot整合使用Thymeleaf,Thymeleaf是新一代的Java模板引擎,类似于Velocity、FreeMarker等传统引擎,关于其更多相关内容,需要的小伙伴可以参考一下

随机推荐

  1. 基于EJB技术的商务预订系统的开发

    用EJB结构开发的应用程序是可伸缩的、事务型的、多用户安全的。总的来说,EJB是一个组件事务监控的标准服务器端的组件模型。基于EJB技术的系统结构模型EJB结构是一个服务端组件结构,是一个层次性结构,其结构模型如图1所示。图2:商务预订系统的构架EntityBean是为了现实世界的对象建造的模型,这些对象通常是数据库的一些持久记录。

  2. Java利用POI实现导入导出Excel表格

    这篇文章主要为大家详细介绍了Java利用POI实现导入导出Excel表格,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  3. Mybatis分页插件PageHelper手写实现示例

    这篇文章主要为大家介绍了Mybatis分页插件PageHelper手写实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  4. (jsp/html)网页上嵌入播放器(常用播放器代码整理)

    网页上嵌入播放器,只要在HTML上添加以上代码就OK了,下面整理了一些常用的播放器代码,总有一款适合你,感兴趣的朋友可以参考下哈,希望对你有所帮助

  5. Java 阻塞队列BlockingQueue详解

    本文详细介绍了BlockingQueue家庭中的所有成员,包括他们各自的功能以及常见使用场景,通过实例代码介绍了Java 阻塞队列BlockingQueue的相关知识,需要的朋友可以参考下

  6. Java异常Exception详细讲解

    异常就是不正常,比如当我们身体出现了异常我们会根据身体情况选择喝开水、吃药、看病、等 异常处理方法。 java异常处理机制是我们java语言使用异常处理机制为程序提供了错误处理的能力,程序出现的错误,程序可以安全的退出,以保证程序正常的运行等

  7. Java Bean 作用域及它的几种类型介绍

    这篇文章主要介绍了Java Bean作用域及它的几种类型介绍,Spring框架作为一个管理Bean的IoC容器,那么Bean自然是Spring中的重要资源了,那Bean的作用域又是什么,接下来我们一起进入文章详细学习吧

  8. 面试突击之跨域问题的解决方案详解

    跨域问题本质是浏览器的一种保护机制,它的初衷是为了保证用户的安全,防止恶意网站窃取数据。那怎么解决这个问题呢?接下来我们一起来看

  9. Mybatis-Plus接口BaseMapper与Services使用详解

    这篇文章主要为大家介绍了Mybatis-Plus接口BaseMapper与Services使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  10. mybatis-plus雪花算法增强idworker的实现

    今天聊聊在mybatis-plus中引入分布式ID生成框架idworker,进一步增强实现生成分布式唯一ID,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部