Spring Boot有哪几种接收参数的方式呢?

戚薇 SpringBoot 发布时间:2023-06-08 17:25:11 阅读数:101 1
下文笔者讲述SpringBoot接收参数的方法分享,如下所示

SpringBoot接收参数的方法

方式1:
   直接接收对象

方式2:
   通过变量名接收

方式3:
   使用注解的方式接收参数
例:SpringBoot接收参数的示例
@RestController
public class MyController {
 
    @GetMapping("/test1")
    public MyResponse test1(MyRequest myRequest){
 
        MyResponse myResponse = new MyResponse();
        myResponse.setCode(myRequest.getCode());
        myResponse.setMsg(myRequest.getMsg());
        return myResponse;
    }
 
    @GetMapping("/test2")
    public MyResponse test2(Integer code, String msg){
 
        MyResponse myResponse = new MyResponse();
        myResponse.setCode(code);
        myResponse.setMsg(msg);
        return myResponse;
    }
 
    @GetMapping("/test3")
    public MyResponse test3(@RequestParam("code") Integer code, @RequestParam("msg") String msg){
 
        MyResponse myResponse = new MyResponse();
        myResponse.setCode(code);
        myResponse.setMsg(msg);
        return myResponse;
    }
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/SpringBoot/202306/6753.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者