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

SpringBoot前后端json數(shù)據(jù)交互的全過程記錄

 更新時間:2022年03月14日 10:49:16   作者:北冥友余  
現(xiàn)在大多數(shù)互聯(lián)網(wǎng)項(xiàng)目都是采用前后端分離的方式開發(fā),下面這篇文章主要給大家介紹了關(guān)于SpringBoot前后端json數(shù)據(jù)交互的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

一、參考文獻(xiàn)

原生Ajax與JQuery Ajax

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

  • 提交方式為 POST 時,
  • JQuery Ajax 以 application/x-www-form-urlencoded 上傳 JSON對象 ,
  • 后端用 @RequestParam 或者Servlet 獲取參數(shù)。
  • JQuery Ajax 以 application/json 上傳 JSON字符串,
  • 后端用 @RquestBody 獲取參數(shù)。
  • 總結(jié)成表

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

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

二、勇敢嘗試

前端js發(fā)送ajax請求( application/x-www-form-urlencoded )

       var jsonObj = {"openid":"xxx","username":"Ed sheeran","password":"123"};
          /*
              Jquery默認(rèn)Content-Type為application/x-www-form-urlencoded類型
           */
          $.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ù)。前端報 200,后端報 返回值都是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"));
}

后端改 @RequestParam 接受參數(shù)。前端報 404,后端報 Required String parameter ‘username’ is not present

@Controller
public class LoginController {
  @PostMapping("/login")
  public void login(@RequestParam("username") String username,
                    @RequestParam("password") String password,
                    @RequestParam("openid") String openid){
      System.err.println(username);
      System.err.println(password);
      System.err.println(openid);
  }

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

Http status 415 Unsupported Media Type

What is 415 ?

HTTP 415 Unsupported Media Type

The client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format.

Let’s look at the broswer ?

How to resolve 405 problem when using ajax post @ResponseBody return json data ?

spring-webmvc.x.x.jar

jackson-databind.jar

jackson-core.jar

jackson-annotations.jar

  • In Spring Configuration file,add following code
<bean class="org.springframework.web.servlet.mvc.
annotation.AnnotationMethodHandlerAdapter">  
    <property name="messageConverters">  
        <list>  
            <ref bean="jsonHttpMessageConverter" />  
        </list>  
    </property>  
</bean>  

<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.
MappingJackson2HttpMessageConverter">  
    <property name="supportedMediaTypes">  
        <list>  
          <value>application/json;charset=UTF-8</value>  
        </list>  
    </property>  
</bean>
  • In ajax , set contentType : ‘application/json;charse=UTF-8’
    var data = {"name":"jsutin","age":18};
    $.ajax({  
        url : "/ASW/login.html",  
        type : "POST",  
        data : JSON.stringify(data),  
        dataType: 'json',  
        contentType:'application/json;charset=UTF-8',      
        success : function(result) {  
            console.log(result);  
        }  
    }); 

In server ,using @RequestBody anotation receive json data,and using @ResponseBody anotation response json to jsp page

@RequestMapping(value = "/login",method = RequestMethod.POST)
public @ResponseBody User login(@RequestBody User user){
    system.out.println(user.getName);
    system.out.println(user.getAge);
}

 

@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”。前端報 200,后端 能接受到參數(shù)

    $.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"));
}
}

有時候,我想在后端使用對象來獲取參數(shù)。前端報 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;

  public String getOpenid() {
      return openid;
  }

  public void setOpenid(String openid) {
      this.openid = openid;
  }

  public String getUsername() {
      return username;
  }

  public void setUsername(String username) {
      this.username = username;
  }

  public String getPassword() {
      return password;
  }

  public void setPassword(String password) {
      this.password = password;
  }

  @Override
  public String toString() {
      return "Form{" +
              "openid='" + openid + '\'' +
              ", username='" + username + '\'' +
              ", password='" + password + '\'' +
              '}';
  }
}

三、最終選擇交互方式

前端 application/json,上傳 josn字符串, 后端 使用對象 或者 Map

前端代碼

       var jsonObj = {"openid":"xxx","username":"Ed sheeran","password":"123"};
          /*
              Jquery默認(rèn)Content-Type為application/x-www-form-urlencoded類型
           */
          $.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"));
}
}

總結(jié)

到此這篇關(guān)于SpringBoot前后端json數(shù)據(jù)交互的文章就介紹到這了,更多相關(guān)SpringBoot前后端json數(shù)據(jù)交互內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java的三種隨機(jī)數(shù)生成方式的實(shí)現(xiàn)方法

    java的三種隨機(jī)數(shù)生成方式的實(shí)現(xiàn)方法

    這篇文章主要介紹了java的三種隨機(jī)數(shù)生成方式的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java常用類之字符串相關(guān)類使用詳解

    Java常用類之字符串相關(guān)類使用詳解

    String、StringBuilder、StringBuffer類是Java中常用的三個字符串相關(guān)類。本文將通過示例為大家講解一下他們的用法,需要的可以參考一下
    2022-08-08
  • SpringBoot集成mybatis實(shí)例

    SpringBoot集成mybatis實(shí)例

    本篇文章主要介紹了SpringBoot集成mybatis實(shí)例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Java8中Optional類型和Kotlin中可空類型的使用對比

    Java8中Optional類型和Kotlin中可空類型的使用對比

    這篇文章主要給大家介紹了關(guān)于Java8中Optional類型和Kotlin中可空類型的使用對比,文中通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09
  • Java程序開發(fā)中abstract 和 interface的區(qū)別詳解

    Java程序開發(fā)中abstract 和 interface的區(qū)別詳解

    abstract class和interface在Java語言中都是用來進(jìn)行抽象類。但是兩者有什么區(qū)別呢,接下來小編給大家?guī)砹薬bstract 和 interface的區(qū)別詳解,感興趣的朋友一起學(xué)習(xí)吧
    2016-06-06
  • 關(guān)于Redis的緩存穿透問題

    關(guān)于Redis的緩存穿透問題

    這篇文章主要介紹了關(guān)于Redis的緩存穿透問題,緩存穿透是指客戶端請求的數(shù)據(jù)在緩存中和數(shù)據(jù)庫中都不存在,這樣緩存永遠(yuǎn)不會生效,這些請求都會打到數(shù)據(jù)庫,需要的朋友可以參考下
    2023-08-08
  • mybatis自定義參數(shù)類型轉(zhuǎn)換器數(shù)據(jù)庫字段加密脫敏

    mybatis自定義參數(shù)類型轉(zhuǎn)換器數(shù)據(jù)庫字段加密脫敏

    這篇文章主要為大家介紹了mybatis自定義參數(shù)類型轉(zhuǎn)換器數(shù)據(jù)庫字段加密脫敏,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Java常見的四種負(fù)載均衡算法

    Java常見的四種負(fù)載均衡算法

    本文主要介紹了Java常見的四種負(fù)載均衡算法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Java觀察者設(shè)計模式詳解

    Java觀察者設(shè)計模式詳解

    這篇文章主要為大家詳細(xì)介紹了Java觀察者設(shè)計模式,對觀察者設(shè)計模式進(jìn)行分析理解,感興趣的小伙伴們可以參考一下
    2016-02-02
  • 如何在Java中使用正則表達(dá)式API

    如何在Java中使用正則表達(dá)式API

    這篇文章主要介紹了如何在Java中使用正則表達(dá)式API,我們將討論java正則表達(dá)式API,以及如何在Java編程語言中使用正則表達(dá)式。具體詳細(xì)介紹,需要的小伙伴可以參考下面文章內(nèi)容
    2022-06-06

最新評論