spring boot前后端参数传递方式

使用spring boot2X做后端,postman做前端测试

1.获取json字符串

复制代码
@RestController
public class Demo {
    @RequestMapping("test")
    public Result test(@RequestBody JSONObject obj) {

        return  Result.success(200,obj);
    }

}
复制代码

测试

2.获取出传入的参数值

复制代码
@RestController
public class Demo {
    @RequestMapping("test")
    public Result test(@RequestParam(value = "name",required = false) String name,@RequestParam(value = "id") Integer id) {

        MyData my = new MyData();
        my.setId(id);
        my.setName(name);
        return  Result.success(200,my);
    }

}
复制代码

测试

3.获取路径中的参数值

(1)通过PathVariable注解来绑定请求路径的参数

复制代码
@RestController
public class Demo {
    @RequestMapping(value = "/test/{id}/{name}")
    public Result test(@PathVariable(value = "id") Integer id, @PathVariable(value = "name") String name) {

        MyData my = new MyData();
        my.setId(id);
        my.setName(name);
        return  Result.success(200,my);
    }

}
复制代码

测试

(2)指定前端url请求参数名称与方法名一致

复制代码
@RestController
public class Demo {
    @RequestMapping(value = "test")
    public Result test(Integer id,String name) {

        MyData my = new MyData();
        my.setId(id);
        my.setName(name);
        return  Result.success(200,my);
    }

}
复制代码

 (3)通过HttpServletRequest来获取前端页面参数

复制代码
@RestController
public class Demo {
    @RequestMapping(value = "test")
    public Result test(HttpServletRequest request) {

        MyData my = new MyData();
        my.setId(Integer.valueOf(request.getParameter("id")));
        my.setName(request.getParameter("name"));
        return  Result.success(200,my);
    }
}
复制代码

 

 (4)通过RequestParam注解来获取

复制代码
@RestController
public class Demo {
    @RequestMapping(value = "test")
    public Result test(@RequestParam(defaultValue = "0") Integer id,@RequestParam(defaultValue = "0")String name) {

        MyData my = new MyData();
        my.setId(id);
        my.setName(name);
        return  Result.success(200,my);
    }
}
复制代码

4.获取请求的报文头

@RestController
public class Demo {
    @RequestMapping(value = "test")
    public Result test(@RequestHeader Map<String, String> map) {

        return  Result.success(200,map);
    }
}

5.请求中的cookie

@RestController
public class Demo {
    @RequestMapping(value = "test")
    public Result test(@CookieValue(value = "JSESSIONID", required = true) String jsessionId,@CookieValue(value = "name", required = true) String name) {

        return  Result.success(200,"JSESSIONID="+jsessionId+",name="+name);
    }
}

 

 

5.获取矩阵变量绑定的参数

矩阵变量

矩阵变量可以出现在任何路径片段中,每一个矩阵变量都用分号(;)隔开,多个值可以用逗号隔开

Spring4.0已经全面支持Matrix Variable,该注解似的开发人员能够将请求中的矩阵变量绑定到处理器的方法参数中

spring  boot 默认是无法使用矩阵变量绑定参数的。需要覆盖WebMvcConfigurer中的configurePathMatch方法

复制代码
package com.example.demo.core;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.util.UrlPathHelper;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper=new UrlPathHelper();
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }

}
复制代码

使用矩阵变量

复制代码
@RestController
public class Demo {
    @RequestMapping(value="test/{param}")
    public Object test(@MatrixVariable(pathVar="param",value="color")String[] tt){
        for (String s : tt) {
            System.out.println(s);
        }
        return Result.success(200,tt);
    }
}
复制代码

调用时也可以写成http://127.0.0.1:8080/test/color=red;color=green;color=blue,结果是一样的

复制代码
@RestController
public class Demo {
    @RequestMapping(value="test/{x1}/p/{x2}")
    public Object test2(@MatrixVariable(pathVar = "x1") Map<String, String[]> x1,
                        @MatrixVariable(pathVar = "x2") Map<String, String[]> x2){

        System.out.println(x1);
        System.out.println(x2);
        return x1+"-----"+x2;
    }
}
复制代码

附:

Result.java

复制代码
package com.example.demo.core;

import com.alibaba.fastjson.JSON;

/**
 * Created by Beibei on 19/02/22
 * API响应结果
 */
public class Result<T> {
    private int code;
    private String message;
    private T data;

    public Result setCode(Integer code) {
        this.code = code;
        return this;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public Result setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result setData(T data) {
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public static <T>  Result<T> fail(Integer code,T data) {
        Result<T> ret = new Result<T>();
        ret.setCode(code);
        ret.setData(data);
        return ret;
    }

    public static <T>  Result<T> failMessage(Integer code,String msg) {
        Result<T> ret = new Result<T>();
        ret.setCode(code);
        ret.setMessage(msg);
        return ret;
    }
    public static <T>  Result<T> successMessage(Integer code,String msg) {
        Result<T> ret = new Result<T>();
        ret.setCode(code);
        ret.setMessage(msg);
        return ret;
    }

    public static <T> Result<T> success(Integer code,T data) {
        Result<T> ret = new Result<T>();
        ret.setCode(code);
        ret.setData(data);
        return ret;
    }

}
View Code
复制代码

MyData.java

复制代码
package com.example.demo.domain;

public class MyData {
    private int id;
    private String name;
    private String other;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getOther() {
        return other;
    }

    public void setOther(String other) {
        this.other = other;
    }

}
View Code
复制代码

 

posted @   慕尘  阅读(2156)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示