Springboot的POST、GET、PUT、DELETE请求实例

Spring的请求

前文我们的访问我们的项目直接就是用的http://localhost:8080/,这里对于具体的项目我们可以加上项目路径并且设置端口号。在application.properties文件中,

server.port=9090
server.context-path=/retrofitclientserver

此时访问我们的项目就要使用http://localhost:9090/retrofitclientserver,端口号可以不指定,则会使用默认的端口号8080,为http://localhost:8080/retrofitclientserver

请求地址映射注解

请求地址映射注解可以加在类上,也可以加在方法上,加在类上表示这个类中所有响应请求的方法都是以该地址作为父路径。

@RequestMapping的属性

这是一个通用的注解,支持所有的HTTP请求。这里大致讲一下常用的属性。

@AliasFor("path")
String[] value() default {};
@AliasFor("value")
String[] path() default {};
RequestMethod[] method() default {};

value和path

value和path是都是指请求地址。@RequestMapping(“/login”)等价于@RequestMapping(path=”/login”)。

占位符

然后这两个属性还支持带占位符的URL,比如:

@RequestMapping(path = "/{account}", method = RequestMethod.GET)
public String getUser(@PathVariable String account)

这是参数名跟占位符名字一致的情况,不一致的话就要这样写:

@RequestMapping(path = "/{account}/{name}", method = RequestMethod.GET)
public String getUser(@PathVariable("account") String phoneNumber,@PathVariable("name") String userName)

这样就把占位符绑定到参数phoneNumber上了。

@PathVariable

这里出现了@PathVariable,@Pathvariable注解可以绑定占位符传过来的值到方法的参数上。

method
method属性是指请求的方式。

组合注解(RequestMapping的变形)

@GetMapping = @RequestMapping(method = RequestMethod.GET)
@PostMapping = @RequestMapping(method = RequestMethod.POST)
@PutMapping = @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)

组合注解是方法级别的,只能用在方法上,我们的实例基本都用组合注解。

@RequestParam

用法如下:

@PostMapping(value = "login")
public void login(@RequestParam String name, @RequestParam String password) {
    System.out.println(name + ":" + password);
}

@RequestParam注解可以用来提取名为“name”的String类型的参数,并将之作为输入参数传入,这就是SpringMVC的提取和解析请求参数的能力。
我们甚至可以不用这个注解,也能只要传入参数名和方法的参数名一致,也能匹配:

@PostMapping(value = "login")
public void login(String name, @RequestParam String password) {
    System.out.println(name + ":" + password);
}

这里的name参数没有加这个注解,实际上也匹配到了。
如果传入参数名字和方法参数名字不一致,可以给@RequestParam的属性赋值:

@PostMapping(value = "login")
public void login(@RequestParam("account") String name, @RequestParam String password) {
    System.out.println(name + ":" + password);
}

@RequestBody

用法如下:

@PostMapping(path = "register")
public String registerUser(@RequestBody User user) {
    return user.toString();
}
public class User {
    private String name;
    private String password;

    省略getset、toString...
}

@RequestBody可以用来解析json字符串(还可以解析xml),并将字符串映射到对应的实体中,实体的字段名和json中的键名要对应。

注意提交请求的时候要在请求头指定content-type为application/json charset=utf-8。

@RequestHeader

@RequestHeader注解用来将请求头的内容绑定到方法参数上。
用法如下:

@PostMapping(value = "login")
public void login(@RequestHeader("access_token") String accessToken,@RequestParam String name) {
    System.out.println("accessToken:" + accessToken);
}

原文地址https://blog.csdn.net/u013005791/article/details/73348206
项目demo github地址:https://github.com/Jadyli/RetrofitClientServer

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值