爱生活,爱分享


Springboot中controller接参几种形式

haiten 2020-12-14 543浏览 0条评论
首页/正文
分享到: / / / /

一、请求路径参数

1.@PathVairable

获取路径参数,url{id} 形式。

2.@RequestParam

获取查询参数,url?name= 形式。

实例:

GET
http://localhost:8282/helloworld/2019?name=Naland

对应java代码:

// @RequestMapping(value = "/helloworld/{id}", method = RequestMethod.GET) //亦可
@GetMapping("/helloworld/{id}")
public void demo(@PathVariable(name = "id") String id, @RequestParam(name = "name") String name) {
    System.out.println("id="+id);
    System.out.println("name="+name);
}
// -------------------------------------------------------------------------------
// 输出结果:
// id=2019
// name=Naland
// -------------------------------------------------------------------------------

二、Body参数

POST 请求,结合 postman 截图和代码说明

Peroson 类:

public class Person {
 
    private long id;
    private String name;
    private int age;
    private String hobby;
 
    @Override
    public String toString(){
        return "name:"+name+";age="+age+";hobby:"+hobby;
    }
 
    // getters and setters
}

1.@RequestBody

@RequestBody 主要用来接收前端传递给后端的 json 字符串中的数据的(请求体中的数据的);

GET 方式无请求体,所以使用 @RequestBody,前端要用 POST 方式进行提交;

一个请求,只有一个RequestBody;一个请求,可以有多个RequestParam;

对应 java 代码:

@PostMapping(path = "/demo1")
public void demo1(@RequestBody Person person) {
    System.out.println(person.toString());
}
// -------------------------------------------------------------------------------
// 输出结果:
// name:suki_rong;age=18;hobby:programing
// -------------------------------------------------------------------------------

或者:

@PostMapping(path = "/demo1")
public void demo1(@RequestBody Map<String, String> person) {
    System.out.println(person.get("name"));
}
// -------------------------------------------------------------------------------
// 输出结果:
// suki_rong
// -------------------------------------------------------------------------------

2.无注解

对应代码:

@PostMapping(path = "/demo2")
public void demo2(Person person) {
    System.out.println(person.toString());
}
// -------------------------------------------------------------------------------
// 输出结果:
// name:suki_rong;age=18;hobby:programing
// -------------------------------------------------------------------------------

三、请求头参数及cookie

1.@RequestHeader

2.@CookieValue

例子:

java代码:

@GetMapping("/demo3")
public void demo3(@RequestHeader(name = "myHeader") String myHeader,
        @CookieValue(name = "myCookie") String myCookie) {
    System.out.println("myHeader=" + myHeader);
    System.out.println("myCookie=" + myCookie);
}

或者:

@GetMapping("/demo3")
public void demo3(HttpServletRequest request) {
    System.out.println(request.getHeader("myHeader"));
    for (Cookie cookie : request.getCookies()) {
        if ("myCookie".equals(cookie.getName())) {
            System.out.println(cookie.getValue());
        }
    }
}

原文链接:Springboot中controller接参几种形式

转载仅为方便学习查看,一切权利属于原作者,本人只是做了整理和排版,如果带来不便请联系我删除。

最后修改:2020-12-14 12:52:19 © 著作权归作者所有
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付

上一篇

发表评论

说点什么吧~

评论列表

还没有人评论哦~赶快抢占沙发吧~