Springboot PostMapping無法獲取數據問題及解決
PostMapping無法獲取數據問題
在使用SpringBoot的PostMapping注解的時候,發(fā)現(xiàn)無法獲取數據(get方法可行),經過一番查證,發(fā)現(xiàn)需要添加新的注解
舉例如下
? ? //接受單個參數,使用RequestParam,并且添加上name屬性,保證前后端的參數名稱一致
?
? ? @PostMapping(value = "/users")
? ? public RestfulResponse postUser(@RequestParam("id") Integer id, ? ? ? ? @RequestParam("username") String username, @RequestParam("password") String password) {
? ? ? ? User user = new User(id, username, password);
? ? ? ? //User user = new User(1,"tom","123123");
? ? ? ? System.out.println(id + "----" + username);
? ? ? ? ? ? restfulResponse = new RestfulResponse(true,200,"查詢成功", null);
? ? ? ? return restfulResponse;
? ? }
? ? //接受一個實體類,要使用RequestBody 注解
? ? @PostMapping(value = "/getuser")
? ? public RestfulResponse postUser1(@RequestBody User user) {
? ? ? ? restfulResponse = new RestfulResponse(true,200,"查詢成功", user);
? ? ? ? return restfulResponse;
? ? }Springboot之PostMapping
@PostMapping
映射一個POST請求
Spring MVC新特性
提供了對Restful風格的支持
@PostMapping(value = "/user/login") //等價于 @RequestMapping(value = "/user/login",method = RequestMethod.POST)
擴展
@GetMapping,處理get請求@PostMapping,處理post請求@PutMapping,處理put請求@DeleteMapping,處理delete請求
@RequestMapping
RequestMapping是一個用來處理請求地址映射的注解,可用于類或方法上。用于類上,表示類中的所有響應請求的方法都是以該地址作為父路徑。
屬性
value:指定請求的實際地址method:指定方法類型,get、post、put、delete等consumes:指定處理請求的提交內容類型,如application/json, text/html;produces: 指定返回的內容類型,僅當request請求頭中的(Accept)類型中包含該指定類型才返回;params: 指定request中必須包含某些參數值是,才讓該方法處理。headers: 指定request中必須包含某些指定的header值,才能讓該方法處理請求。
value/method示例
@Controller
@RequestMapping("/appointments")
public class AppointmentsController {
? ? private AppointmentBook appointmentBook;
? ??
? ? @Autowired
? ? public AppointmentsController(AppointmentBook appointmentBook) {
? ? ? ? this.appointmentBook = appointmentBook;
? ? }
? ? @RequestMapping(method = RequestMethod.GET)
? ? public Map<String, Appointment> get() {
? ? ? ? return appointmentBook.getAppointmentsForToday();
? ? }
? ? @RequestMapping(value="/{day}", method = RequestMethod.GET)
? ? public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) {
? ? ? ? return appointmentBook.getAppointmentsForDay(day);
? ? }
? ? @RequestMapping(value="/new", method = RequestMethod.GET)
? ? public AppointmentForm getNewForm() {
? ? ? ? return new AppointmentForm();
? ? }
? ? @RequestMapping(method = RequestMethod.POST)
? ? public String add(@Valid AppointmentForm appointment, BindingResult result) {
? ? ? ? if (result.hasErrors()) {
? ? ? ? ? ? return "appointments/new";
? ? ? ? }
? ? ? ? appointmentBook.addAppointment(appointment);
? ? ? ? return "redirect:/appointments";
? ? }
}value的uri值為以下三類:
A) 可以指定為普通的具體值;
B) 可以指定為含有某變量的一類值(URI Template Patterns with Path Variables);
C) 可以指定為含正則表達式的一類值( URI Template Patterns with Regular Expressions);
@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model model) {
? Owner owner = ownerService.findOwner(ownerId); ?
? model.addAttribute("owner", owner); ?
? return "displayOwner";?
}@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}")
? public void handle(@PathVariable String version, @PathVariable String extension) { ? ?
? ? // ...
? }
}
cousumes的樣例:
@Controller
@RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json")
public void addPet(@RequestBody Pet pet, Model model) { ? ?
? ? // implementation omitted
}
params的樣例:
@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController {
? @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, params="myParam=myValue")
? public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) { ? ?
? ? // implementation omitted
? }
}headers的樣例:
@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController {
@RequestMapping(value = "/pets", method = RequestMethod.GET, headers="Referer=http://www.ifeng.com/")
? public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) { ? ?
? ? // implementation omitted
? }
}
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Mybatis查詢返回Map<String,Object>類型實例詳解
這篇文章主要給大家介紹了關于Mybatis查詢返回Map<String,Object>類型的相關資料,平時沒太注意怎么用,今天又遇到了總結記錄一下,方便以后處理此類問題,需要的朋友可以參考下2022-07-07
SpringBoot3.1.2 引入Swagger報錯Type javax.servlet.http
這篇文章主要介紹了SpringBoot3.1.2 引入Swagger報錯Type javax.servlet.http.HttpServletRequest not present解決辦法,文中通過代碼示例給大家介紹的非常詳細,需要的朋友可以參考下2024-03-03
vscode開發(fā)maven的javaweb項目并部署到tomcat及配置指南
這篇文章主要給大家介紹了關于vscode開發(fā)maven的javaweb項目并部署到tomcat及配置的相關資料,在vscode中創(chuàng)建maven項目,需要逐一操作下面的環(huán)節(jié),文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-12-12

