最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

一、參考文獻(xiàn)

原生Ajax與JQuery Ajax

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

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

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

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

二、勇敢嘗試

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

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

后端改 @RequestParam 接受參數(shù)。前端報(bào) 404,后端報(bào) 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ù)。前端報(bào) 415,后端報(bào) 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”。前端報(bào) 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í)候,我想在后端使用對(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;

  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字符串, 后端 使用對(duì)象 或者 Map

前端代碼

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

總結(jié)

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

相關(guān)文章

  • Java中spring boot validation自定義注解使用方式

    Java中spring boot validation自定義注解使用方式

    這篇文章主要介紹了Java中spring boot validation自定義注解使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java使用Ehcache緩存框架的技術(shù)指南

    Java使用Ehcache緩存框架的技術(shù)指南

    Ehcache 是 Java 平臺(tái)下一個(gè)開(kāi)源、高性能的分布式緩存框架,常用于提高系統(tǒng)性能和可擴(kuò)展性,它能夠幫助開(kāi)發(fā)者緩存頻繁訪(fǎng)問(wèn)的數(shù)據(jù),從而減少對(duì)數(shù)據(jù)庫(kù)和其他持久化存儲(chǔ)的訪(fǎng)問(wèn)壓力,本文給大家介紹了Java使用Ehcache緩存框架的技術(shù)指南,需要的朋友可以參考下
    2025-03-03
  • Spring之Scope注解使用詳解

    Spring之Scope注解使用詳解

    spring的bean管理中,每個(gè)bean都有對(duì)應(yīng)的scope。在BeanDefinition中就已經(jīng)指定scope,默認(rèn)的RootBeanDefinition的scope是prototype類(lèi)型,使用@ComponentScan掃描出的BeanDefinition會(huì)指定是singleton,最常使用的也是singleton
    2023-02-02
  • 解決spring-integration-mqtt頻繁報(bào)Lost connection錯(cuò)誤問(wèn)題

    解決spring-integration-mqtt頻繁報(bào)Lost connection錯(cuò)誤問(wèn)題

    這篇文章主要介紹了解決spring-integration-mqtt頻繁報(bào)Lost connection錯(cuò)誤問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 在springboot項(xiàng)目中同時(shí)接收文件和多個(gè)參數(shù)的方法總結(jié)

    在springboot項(xiàng)目中同時(shí)接收文件和多個(gè)參數(shù)的方法總結(jié)

    在開(kāi)發(fā)接口中,遇到了需要同時(shí)接收文件和多個(gè)參數(shù)的情況,可以有多種方式實(shí)現(xiàn)文件和參數(shù)的同時(shí)接收,文中給大家介紹了兩種實(shí)現(xiàn)方法,感興趣的同學(xué)跟著小編一起來(lái)看看吧
    2023-08-08
  • Java對(duì)象的內(nèi)存布局詳細(xì)介紹

    Java對(duì)象的內(nèi)存布局詳細(xì)介紹

    這篇文章主要介紹了Java對(duì)象的內(nèi)存布局,我們知道在Java中基本數(shù)據(jù)類(lèi)型的大小,例如int類(lèi)型占4個(gè)字節(jié)、long類(lèi)型占8個(gè)字節(jié),那么Integer對(duì)象和Long對(duì)象會(huì)占用多少內(nèi)存呢?本文介紹一下Java對(duì)象在堆中的內(nèi)存結(jié)構(gòu)以及對(duì)象大小的計(jì)算
    2023-02-02
  • 華為技術(shù)專(zhuān)家講解JVM內(nèi)存模型(收藏)

    華為技術(shù)專(zhuān)家講解JVM內(nèi)存模型(收藏)

    這篇文章主要介紹了華為技術(shù)專(zhuān)家講解JVM內(nèi)存模型(收藏)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),具有一定的收藏借鑒價(jià)值,需要的朋友可以參考下
    2021-05-05
  • 關(guān)于Spring框架中異常處理情況淺析

    關(guān)于Spring框架中異常處理情況淺析

    最近學(xué)習(xí)Spring時(shí),認(rèn)識(shí)到Spring異常處理的強(qiáng)大,這篇文章主要給大家介紹了關(guān)于Spring框架中異常處理情況的相關(guān)資料,通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-08-08
  • Spring需要三個(gè)級(jí)別緩存解決循環(huán)依賴(lài)原理解析

    Spring需要三個(gè)級(jí)別緩存解決循環(huán)依賴(lài)原理解析

    這篇文章主要為大家介紹了Spring需要三個(gè)級(jí)別緩存解決循環(huán)依賴(lài)原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • SpringBoot使用Captcha生成驗(yàn)證碼

    SpringBoot使用Captcha生成驗(yàn)證碼

    這篇文章主要介紹了SpringBoot如何使用Captcha生成驗(yàn)證碼,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot,感興趣的朋友可以了解下
    2021-04-04

最新評(píng)論

曲麻莱县| 丽江市| 乌审旗| 东方市| 江山市| 岐山县| 吴川市| 涞水县| 中卫市| 平湖市| 屏边| 琼结县| 修水县| 平武县| 芜湖市| 潮州市| 碌曲县| 延庆县| 鞍山市| 洛阳市| 松滋市| 宜宾市| 紫阳县| 长阳| 和林格尔县| 康保县| 泗水县| 湛江市| 兴义市| 绵竹市| 渝中区| 南部县| 商城县| 南靖县| 广丰县| 濉溪县| 平陆县| 恭城| 大英县| 舒城县| 玉田县|