Spring Boot接收从前端传过来的数据常用方式以及处理的技巧

作者 : admin 本文共2918个字,预计阅读时间需要8分钟 发布时间: 2024-06-17 共1人阅读

Spring Boot接收从前端传过来的数据常用方式以及处理的技巧

  • 一、params 传参
      • restful风格的请求
  • 二、Body中的form-data 传参
  • 三、Body中的raw的json格式 传参

一、params 传参

参数是会拼接到url后面的请求

场景规范:url后面的key值<=3个参数的时候,使用params 传参

支持的请求方式:get(正规的是get方式)、post 都行

例如:
http://localhost:8080/simpleParam?name=Tom&age=10

在postman里面的体现为
Spring Boot接收从前端传过来的数据常用方式以及处理的技巧插图

后端接收的接口写法如下
普遍都是使用第一种和第二种去接收

   //(1)直接接收,只要key值相同
    @RequestMapping("/simpleParam")
    public String simpleParam(String name,Integer age){
        System.out.println("name= "+name);
        System.out.println("age= "+ age);
        return "success";
    }
    //(2)直接接收,值不同可以使用@RequestParam("name");取别名
    @RequestMapping("/simpleParam")
    public String simpleParam(@RequestParam("name")String username ,Integer age){
        System.out.println("username = "+username );
        System.out.println("age= "+ age);
        return "success";
    }
    //(3)实体类接收,注意接收的实体类里面的属性值要和请求url中的key值一样哦
   @RequestMapping("/simpleParam")
    public String simpleParam(User user){
        System.out.println(user);
        return "success";
    }
  //(4)最牛皮的,HttpServletRequest来接受
   @RequestMapping("/simpleParam")
    public String simpleParam(HttpServletRequest request){
        String name= request.getParameter("name");
        String age= request.getParameter("age");
        return "success";

restful风格的请求

get请求:localhost:8080/users/add3?tom/123456

后端接收的接口写法如下

//(1)使用 @PathVariable
@RequestMapping("add3/{name}/{password}")
    public String add3(@PathVariable("name") String username, @PathVariable String password){
        System.out.println(username+" / "+password);
        return "success";
    }

二、Body中的form-data 传参

form-data

当需要发送表单数据或上传文件

场景规范:发送表单数据或上传文件

支持的请求方式
只是表单数据的话,get、post (正规的是post方式)都行;
如果存在文件数据,必须是post请求

(1)场景一:只是表单数据(那就和params 传参的后端接收法一样,就不重复写了)

在postman里面的体现为
Spring Boot接收从前端传过来的数据常用方式以及处理的技巧插图(1)

(2)场景二:存在文件数据

在postman里面的体现为
Spring Boot接收从前端传过来的数据常用方式以及处理的技巧插图(2)

后端接收的接口写法如下

   //(1)使用 HttpServletRequest
    @RequestMapping("/simpleParam")
    public String simpleParam(HttpServletRequest request) throws IOException {  
        if (request instanceof MultipartHttpServletRequest) {  
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;  
  
            // 获取文件  
            MultipartFile file = multipartRequest.getFile("file");  
            String fileName = file.getOriginalFilename();  
            byte[] fileBytes = file.getBytes();  
  
            // 处理文件...  
  
            // 获取其他字段  
            String username = multipartRequest.getParameter("name");  
  
            return "File uploaded: " + fileName + ", User: " + username ;  
        } else {  
            return "Error: Form must have enctype=multipart/form-data.";  
        }  
    }  
    //(2)使用 @RequestPart
    @RequestMapping("/simpleParam")
    public String simpleParam(@RequestPart("file") MultipartFile file,  
            @RequestPart("name") String username) throws IOException {  
        String fileName = file.getOriginalFilename();  
        // 处理文件...  
  
        return "File uploaded: " + fileName + ", User: " + username;  
    }  

三、Body中的raw的json格式 传参

支持的请求方式:post (最常见post方式)、PUT和PATCH

Spring Boot接收从前端传过来的数据常用方式以及处理的技巧插图(3)
后端接收的接口写法如下

   //(1)使用@RequestBody注解接收JSON对象
    @RequestMapping("/simpleParam")
    public String simpleParam(@RequestBody User user) {  
        // 使用User对象中的值  
        return "Received JSON: " + user.toString();  
    }  

实体类为:

 public static class User{  
        private String name;  
        private Integer age;
        //单个实体类 
        private Cat cat;
        //List实体类
        private List<Course> courseList;
 
    @Override
    public String toString() {
        return "User{" +
                "name='" + name+ '\'' +
                ", age='" + age+ '\'' +
                ", cat=" + cat +
                ", courseList=" + courseList +
                '}';
    }
}

参考文章
【1】Spring Boot接收从前端传过来的数据常用方式以及处理的技巧
https://blog.csdn.net/aiwokache/article/details/129037252

本站无任何商业行为
个人在线分享 » Spring Boot接收从前端传过来的数据常用方式以及处理的技巧
E-->