本篇文章给大家分享的是有关SpringBoot项目中如何使用@PathVaribale、@RequestBody和@RequestParam,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
实例User
package com.iflytek.odeon.shipper.model.rx;
import io.swagger.annotations.ApiModelProperty;
public class Student {
@ApiModelProperty(value = "名称", example = "zhangsan", required = true)
private String name;
private Integer call;
public Student() {
}
public Student(String name, Integer call) {
this.name = name;
this.call = call;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCall() {
return call;
}
public void setCall(Integer call) {
this.call = call;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", call=" + call +
'}';
}
}
实例Controller
package com.iflytek.odeon.shipper.controller;
import com.iflytek.odeon.shipper.model.rx.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/**
* 测试注解及调试功能API
*/
@RestController
@RequestMapping("/v1")
public class SampleController {
@PostMapping("/hi")
public Student hi(@RequestBody() Student student) {
return new Student(student.getName(), student.getCall());
}
@PostMapping("/hello")
public Student hello(@RequestParam(value = "name") String name, @RequestParam(value = "call") Integer call) {
Student stuResponse = new Student();
stuResponse.setName(name + "call");
stuResponse.setCall(call);
return stuResponse;
}
@GetMapping("/hello/{id}")
public Integer getUrl(@PathVariable(value = "id") Integer id) {
return id;
}
}
效果
body
parme key value
pathvar
/{id}
以上就是SpringBoot项目中如何使用@PathVaribale、@RequestBody和@RequestParam,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。