Spring Boot有哪几种接收参数的方式呢?
下文笔者讲述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;
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


