Springboot PostMapping無法獲取數(shù)據(jù)問題及解決
PostMapping無法獲取數(shù)據(jù)問題
在使用SpringBoot的PostMapping注解的時候,發(fā)現(xiàn)無法獲取數(shù)據(jù)(get方法可行),經(jīng)過一番查證,發(fā)現(xiàn)需要添加新的注解
舉例如下
? ? //接受單個參數(shù),使用RequestParam,并且添加上name屬性,保證前后端的參數(shù)名稱一致
?
? ? @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風(fēng)格的支持
@PostMapping(value = "/user/login") //等價于 @RequestMapping(value = "/user/login",method = RequestMethod.POST)
擴(kuò)展
@GetMapping,處理get請求@PostMapping,處理post請求@PutMapping,處理put請求@DeleteMapping,處理delete請求
@RequestMapping
RequestMapping是一個用來處理請求地址映射的注解,可用于類或方法上。用于類上,表示類中的所有響應(yīng)請求的方法都是以該地址作為父路徑。
屬性
value:指定請求的實際地址method:指定方法類型,get、post、put、delete等consumes:指定處理請求的提交內(nèi)容類型,如application/json, text/html;produces: 指定返回的內(nèi)容類型,僅當(dāng)request請求頭中的(Accept)類型中包含該指定類型才返回;params: 指定request中必須包含某些參數(shù)值是,才讓該方法處理。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) 可以指定為含正則表達(dá)式的一類值( 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
? }
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決MyBatis中模糊搜索使用like匹配帶%字符時失效問題
Mybatis是我們?nèi)粘m椖恐薪?jīng)常使用的框架,在項目中我們一般會使用like查詢作為模糊匹配字符進(jìn)行搜索匹配,下面的Mapper.xml是我們使用like在項目中進(jìn)行模糊匹配的常用方式,感興趣的朋友跟隨小編一起看看吧2021-09-09
詳解MyBatis Generator自動創(chuàng)建代碼(dao,mapping,poji)
這篇文章主要介紹了詳解MyBatis Generator自動創(chuàng)建代碼(dao,mapping,poji)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-10-10
springboot+thymeleaf+mybatis實現(xiàn)甘特圖的詳細(xì)過程
這篇文章主要介紹了springboot+thymeleaf+mybatis實現(xiàn)甘特圖的詳細(xì)過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07
使用spring aop 統(tǒng)一捕獲異常和寫日志的示例demo
本文通過一個小demo給大家介紹spring AOP 實現(xiàn)的異常捕獲和日志的方法技巧,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-08-08
SpringBoot中@ConfigurationProperties注解的使用與源碼詳解
這篇文章主要介紹了SpringBoot中@ConfigurationProperties注解的使用與源碼詳解,@ConfigurationProperties注解用于自動配置綁定,可以將application.properties配置中的值注入到bean對象上,需要的朋友可以參考下2023-11-11
springboot基于IDEA環(huán)境熱加載與熱部署教程
這篇文章主要為大家介紹了springboot在IDEA環(huán)境下的熱加載與熱部署教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03

