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