欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot實(shí)現(xiàn)前后端、json數(shù)據(jù)交互以及Controller接收參數(shù)的幾種常用方式

 更新時(shí)間:2022年03月14日 12:01:59   作者:月半花開(kāi)  
這篇文章主要給大家介紹了關(guān)于SpringBoot實(shí)現(xiàn)前后端、json數(shù)據(jù)交互以及Controller接收參數(shù)的幾種常用方式,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

前言

現(xiàn)在大多數(shù)互聯(lián)網(wǎng)項(xiàng)目都是采用前后端分離的方式開(kāi)發(fā),前端人員負(fù)責(zé)頁(yè)面展示和數(shù)據(jù)獲取,后端負(fù)責(zé)業(yè)務(wù)邏輯處理和接口封裝。當(dāng)與前端交互的過(guò)程當(dāng)中,常用json數(shù)據(jù)與前端進(jìn)行交互,這樣想取出前端傳送過(guò)來(lái)的json數(shù)據(jù)的時(shí)候,就需要用到@RequestBody這個(gè)注解。@RequestBody注解用于讀取http請(qǐng)求的內(nèi)容(字符串),通過(guò)springmvc提供的HttpMessageConverter接口將讀到的內(nèi)容轉(zhuǎn)換為json、xml等格式的數(shù)據(jù)并綁定到controller方法的參數(shù)上。 

提交方式為 POST 時(shí),

JQuery Ajax 以 application/x-www-form-urlencoded 上傳 JSON對(duì)象 ,

后端用 @RequestParam 或者Servlet 獲取參數(shù)。

JQuery Ajax 以 application/json 上傳 JSON字符串,

后端用 @RquestBody 獲取參數(shù)。

獲取參數(shù)的幾種常用注解

@PathVariable:一般我們使用URI template樣式映射使用,即url/{param}這種形式,也就是一般我們使用的GET,DELETE,PUT方法會(huì)使用到的,我們可以獲取URL后所跟的參數(shù)。

@RequestParam:一般我們使用該注解來(lái)獲取多個(gè)參數(shù),在()內(nèi)寫(xiě)入需要獲取參數(shù)的參數(shù)名即可,一般在PUT,POST中比較常用。

@RequestBody:該注解和@RequestParam殊途同歸,我們使用該注解將所有參數(shù)轉(zhuǎn)換,在代碼部分在一個(gè)個(gè)取出來(lái),也是目前我使用到最多的注解來(lái)獲取參數(shù)

還有@RequestHeader來(lái)獲取頭信息里的值,@CookieValue來(lái)獲取Cookie值等等。在這,我也僅僅說(shuō)明一些較常用的取值方法而已。

一.請(qǐng)求路徑參數(shù) get請(qǐng)求

一般用于查詢(xún)數(shù)據(jù),采用明文進(jìn)行傳輸,一般用來(lái)獲取一些無(wú)關(guān)用戶(hù)信息的數(shù)據(jù),

@GetMapping 組合注解,是 @RequestMapping(method = RequestMethod.GET) 的縮寫(xiě)

1.get請(qǐng)求,url路徑傳參

get請(qǐng)求一般通過(guò)url傳參,如:

http://localhost:4001/api/unit?code=111

后端要獲取code參數(shù),可以使用@RequestParam注解

@RestController
public class HelloController {
    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String sayHello(@RequestParam Integer id){
        return "id:"+id;
    }
}

2.get請(qǐng)求,url路徑參數(shù)

如:http://localhost:4001/api/unit/1

后端使用@PathVariable可以接收路徑參數(shù)1。

@RestController
public class HelloController {
    @RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET)
    public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name){
        return "id:"+id+" name:"+name;
    }
}

小結(jié):當(dāng)請(qǐng)求為get請(qǐng)求時(shí),使用@PathVariable或者@RequestParam獲取參數(shù)值,獲取路徑參數(shù)。@PathVariable一般用于獲取獲取url/{id}這種形式的參數(shù);@RequestParam獲取查詢(xún)參數(shù)。即url?name=這種形式

二、Body參數(shù) POST請(qǐng)求

1、post請(qǐng)求,Body傳值

較推薦使用json格式傳值,postman設(shè)置如圖:

后端接受這種數(shù)據(jù)應(yīng)該采用@RequestBody或者@requestparam

//map接收
@PostMapping(path = "/demo1")
public void demo1(@RequestBody Map<String, String> person) {
    System.out.println(person.get("name"));
}
 
//或者是實(shí)體對(duì)象接收
@PostMapping(path = "/demo1")
public void demo1(@RequestBody Person person) {
    System.out.println(person.toString());
}

 注意;@RequestBody,它是用來(lái)處理前臺(tái)定義發(fā)來(lái)的數(shù)據(jù)Content-Type: 而不是application/x-www-form-urlencoded編碼的內(nèi)容,例如application/json, application/xml等;使用@RequestBody注解接收參數(shù)的時(shí)候,從名稱(chēng)上來(lái)看也就是說(shuō)要讀取的數(shù)據(jù)在請(qǐng)求體里,前臺(tái)的Content-Type必須要改為application/json,所以要發(fā)post請(qǐng)求,因?yàn)锳jax使用的POST,并且發(fā)送的是JSON對(duì)象。前端必須指定請(qǐng)求json數(shù)據(jù)的contentType為:application/json,否則會(huì)報(bào)類(lèi)型不支持的異常錯(cuò)誤“org.springframework.web.HttpMediaTypeNotSupportedException”

當(dāng)Ajax以application/x-www-form-urlencoded格式上傳即使用JSON對(duì)象,后臺(tái)只能使用@RequestParam 或者Servlet獲取參數(shù)。 當(dāng)Ajax以application/json格式上傳即使用JSON字符串,后臺(tái)可以使用@RquestBody或者@RequestParam獲取。

如何定義后臺(tái)接收參數(shù)“能映射上去”呢?若是json中的key在實(shí)體中都能找到對(duì)應(yīng)的field,自動(dòng)映射,也就是說(shuō):前臺(tái)傳入的json中的key在實(shí)體中必須要存在,不然就會(huì)報(bào)錯(cuò) 

第三類(lèi):請(qǐng)求頭參數(shù)以及Cookie

post請(qǐng)求,Headers、cookie傳值

在這里我們把Content-Type設(shè)置為了json格式。

我們還可以在headers里面加入別的參數(shù),比如Token。

后端可以通過(guò)HttpServletRequest 獲取請(qǐng)求頭的內(nèi)容,如:

request.getHeader(string name)方法:String 
request.getHeaders(String name)方法:Enumeration 
request.getHeaderNames()方法
 
@GetMapping("/demo3")
public void demo3(HttpServletRequest request) {
    System.out.println(request.getHeader("myHeader"));
    for (Cookie cookie : request.getCookies()) {
        if ("myCookie".equals(cookie.getName())) {
            System.out.println(cookie.getValue());
        }
    }
}

四、HttpServletRequest 

前端js發(fā)送ajax請(qǐng)求,Content-Type發(fā)送信息至服務(wù)器時(shí)內(nèi)容編碼類(lèi)型,默認(rèn)是( application/x-www-form-urlencoded 這種格式的特點(diǎn)就是,name/value 成為一組,每組之間用 & 聯(lián)接,這種形式是沒(méi)有辦法將復(fù)雜的 JSON 組織成鍵值對(duì)形式),

data_type設(shè)置你收到服務(wù)器數(shù)據(jù)的格式,不指定自動(dòng)判斷

  var jsonObj = {"openid":"xxx","username":"Ed sheeran","password":"123"};
          /*
              Jquery默認(rèn)Content-Type為application/x-www-form-urlencoded類(lèi)型
           */
          $.ajax({
              type: 'POST',
              url: "/login",
              dataType: "json",
              data: JSON.stringify(jsonObj),
              success: function(data) {
                  console.log(data)
              },
              error: function() {
                  console.log("fucking error")
              }
          });

?后端Servlet接受參數(shù)。前端報(bào) 200,后端報(bào) 返回值都是null

@Controller
public class LoginController {
  @PostMapping("/login")
  public void login(HttpServletRequest request){
      System.err.println(request.getParameter("openid"));
      System.err.println(request.getParameter("username"));
      System.err.println(request.getParameter("password"));
}

后端改 @RequestBody 接受參數(shù)。前端報(bào) 415,后端報(bào) Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported

Http status 415 Unsupported Media Type

@Controller
public class LoginController {
  @PostMapping("/login")
  public void login(@RequestBody Map<String,Object> map){
      System.err.println(map.get("username"));
      System.err.println(map.get("password"));
      System.err.println(map.get("openid"));
  }

前端加 contentType : “application/json”。前端報(bào) 200,后端能接受到參數(shù)

var jsonObj = {"openid":"xxx","username":"Ed sheeran","password":"123"};
$.ajax({
              type: 'POST',
              url: "/login",
              dataType: "json",
              data: JSON.stringify(jsonObj),
              contentType : "application/json",
              success: function(data) {
                  console.log(data)
              },
              error: function() {
                  console.log("fucking error")
              }
          });
@Controller
public class LoginController {
  @PostMapping("/login")
  public void login(@RequestBody Map<String,Object> map){
      System.err.println(map.get("username"));
      System.err.println(map.get("password"));
      System.err.println(map.get("openid"));
}
}

后端使用對(duì)象來(lái)獲取參數(shù)。前端報(bào) 200,后端 也ok@Controller

public class LoginController {
  @PostMapping("/login")
  public void login(@RequestBody Form form){
      System.err.println(form);
}
}
public class Form {
  private String openid;
  private String username;
  private String password;
 
  // get set
 
  @Override
  public String toString() {
      return "Form{" +
              "openid='" + openid + '\'' +
              ", username='" + username + '\'' +
              ", password='" + password + '\'' +
              '}';
  }
}

五、參數(shù)校檢

1.后端接收到前端的數(shù)據(jù),如果想對(duì)前端的數(shù)據(jù)進(jìn)行校驗(yàn),可以加入springboot的Validate 功能依賴(lài)包

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-validator</artifactId>
</dependency>

 使用只需要在接收數(shù)據(jù)的實(shí)體上加上@Valid注解,BindingResult接收錯(cuò)誤的不合法的提示信息

接收參數(shù)的實(shí)體的屬性上還需要加校驗(yàn)的注解@NotEmpty(message="密碼不能為空")

還可以使用正則表達(dá)式對(duì)屬性進(jìn)行校驗(yàn)。只需要加入以下注解即可:
@Pattern(
        regexp =  正則表達(dá)式,
        message = "輸入格式不合法"
)

2、當(dāng)后端接收完前端的數(shù)據(jù),響應(yīng)一般也是返回json數(shù)據(jù)給前端,此時(shí)只需要在后端控制器Contoller類(lèi)加上@ResponseBody即可。該注解用于將Controller的方法返回的對(duì)象,通過(guò)HttpMessageConverter接口轉(zhuǎn)換為指定格式的數(shù)據(jù)如:json,xml等,通過(guò)Response響應(yīng)給客戶(hù)端。@Controller 與 @ResponseBody 結(jié)合使用返回json數(shù)據(jù)給前端,我們還可以使用@RestController替換他們,從而使代碼更加的精簡(jiǎn)

注意: 

接收到的參數(shù)默認(rèn)都是字符串類(lèi)型的 

有的注解只能用在String類(lèi)型的屬性上

@JsonProperty可以實(shí)現(xiàn)前端的屬性名和后臺(tái)實(shí)體類(lèi)的屬性名不一致問(wèn)題

校驗(yàn)方式: 

使用@RequestBody @Valid 對(duì)JSON參數(shù)進(jìn)行獲取和校驗(yàn)

最終選擇交互方式

前端 application/json,post請(qǐng)求,上傳 josn字符串, 后端結(jié)合@RequestBody 使用自定義對(duì)象 或者 Map接收參數(shù),這是最常用的方法

前端代碼

    var jsonObj = {"openid":"xxx","username":"Ed sheeran","password":"123"};
          /*
              Jquery默認(rèn)Content-Type為application/x-www-form-urlencoded類(lèi)型
           */
          $.ajax({
              type: 'POST',
              url: "/login",
              dataType: "json",
              data: JSON.stringify(jsonObj),
              contentType : "application/json",
              success: function(data) {
                  console.log(data)
              },
              error: function() {
                  console.log("fucking error")
              }
          });

后端代碼1

@Controller
public class LoginController {
  @PostMapping("/login")
  public void login(@RequestBody Form form){
      System.err.println(form);
}
}

后端代碼2

@Controller
public class LoginController {
  @PostMapping("/login")
  public void login(@RequestBody Map<String,Object> map){
      System.err.println(map.get("username"));
      System.err.println(map.get("password"));
      System.err.println(map.get("openid"));
}
}

如果是get請(qǐng)求,結(jié)合 @RequestParam 或者@PathVariable獲取路徑參數(shù)

如果是通過(guò)header傳值,使用HttpServletRequest獲取

如果是post請(qǐng)求body傳參,對(duì)于參數(shù)較多的,可以使用對(duì)象或者map結(jié)合@RequestBody使用接收參數(shù),如果是少量參數(shù),可以使用@RequestParam單個(gè)映射接收。

參考文獻(xiàn)

SpringBoot 前后端json數(shù)據(jù)交互

原生Ajax與JQuery Ajax

SpringMVC接受JSON參數(shù)詳解及常見(jiàn)錯(cuò)誤總結(jié)

Controller接收參數(shù)以及參數(shù)校驗(yàn)

AJAX POST請(qǐng)求中參數(shù)以form data和request payload形式在servlet中的獲取方式

SpringBoot實(shí)戰(zhàn) 之 數(shù)據(jù)交互篇

SpringBoot Controller接收參數(shù)的幾種常用方式

spring boot常見(jiàn)get 、post請(qǐng)求參數(shù)處理、參數(shù)注解校驗(yàn)、參數(shù)自定義注解校驗(yàn)

總結(jié)

到此這篇關(guān)于SpringBoot實(shí)現(xiàn)前后端、json數(shù)據(jù)交互以及Controller接收參數(shù)的幾種常用方式的文章就介紹到這了,更多相關(guān)SpringBoot數(shù)據(jù)交互及Controller接收參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論