亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Springboot PostMapping無法獲取數(shù)據(jù)問題及解決

 更新時間:2022年05月06日 15:12:32   作者:小喬美噠噠  
這篇文章主要介紹了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;
? ? }
? ? //接受一個實(shí)體類,要使用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:指定請求的實(shí)際地址
  • 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中模糊搜索使用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)

    這篇文章主要介紹了詳解MyBatis Generator自動創(chuàng)建代碼(dao,mapping,poji)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-10-10
  • Java的JDBC和橋接模式詳解

    Java的JDBC和橋接模式詳解

    下面小編就為大家?guī)硪黄狫ava的JDBC和橋接模式(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-09-09
  • springboot+thymeleaf+mybatis實(shí)現(xiàn)甘特圖的詳細(xì)過程

    springboot+thymeleaf+mybatis實(shí)現(xiàn)甘特圖的詳細(xì)過程

    這篇文章主要介紹了springboot+thymeleaf+mybatis實(shí)現(xiàn)甘特圖的詳細(xì)過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-07-07
  • 一文帶你深入了解Java?String的不可變性

    一文帶你深入了解Java?String的不可變性

    這篇文章主要來和大家一起深入探討一下Java?String中的不可變性,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-06-06
  • 使用spring aop 統(tǒng)一捕獲異常和寫日志的示例demo

    使用spring aop 統(tǒng)一捕獲異常和寫日志的示例demo

    本文通過一個小demo給大家介紹spring AOP 實(shí)現(xiàn)的異常捕獲和日志的方法技巧,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-08-08
  • Springboot異常錯誤處理解決方案詳解

    Springboot異常錯誤處理解決方案詳解

    這篇文章主要介紹了Springboot異常錯誤處理解決方案詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • SpringBoot中@ConfigurationProperties注解的使用與源碼詳解

    SpringBoot中@ConfigurationProperties注解的使用與源碼詳解

    這篇文章主要介紹了SpringBoot中@ConfigurationProperties注解的使用與源碼詳解,@ConfigurationProperties注解用于自動配置綁定,可以將application.properties配置中的值注入到bean對象上,需要的朋友可以參考下
    2023-11-11
  • JavaWeb框架MVC設(shè)計思想詳解

    JavaWeb框架MVC設(shè)計思想詳解

    這篇文章主要介紹了JavaWeb框架MVC設(shè)計思想詳解的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-07-07
  • springboot基于IDEA環(huán)境熱加載與熱部署教程

    springboot基于IDEA環(huán)境熱加載與熱部署教程

    這篇文章主要為大家介紹了springboot在IDEA環(huán)境下的熱加載與熱部署教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03

最新評論