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

springboot接收json數(shù)據(jù)時(shí),接收到空值問(wèn)題

 更新時(shí)間:2024年05月17日 10:06:38   作者:妄想...  
這篇文章主要介紹了springboot接收json數(shù)據(jù)時(shí),接收到空值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

springboot接收json數(shù)據(jù)時(shí),接收到空值

問(wèn)題:springboot接收json數(shù)據(jù)收到空值

原因:springboot處理json數(shù)據(jù)時(shí)默認(rèn)采用小駝峰映射

案例

實(shí)體類:

@Data
public class NewTask {
    private String TaskNo;
    private String Priority;
    private String VehicleNo;
    private String VehicleType;
    private String FinishAction;
    private String TaskType;
    private String Location;
}

Controller:

@PostMapping("/addTask")
public String addTask(@RequestBody NewTask task){
    System.out.println(task.toString());
    return "ss";
}

postmam:

springboot收到空值:

這就是因?yàn)閟pringboot處理json數(shù)據(jù)時(shí)默認(rèn)采用小駝峰映射

解決方法

方法一:

將實(shí)體類屬性名改成小駝峰命名;

方法二:

在實(shí)體類上面添加注解

@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
//設(shè)置springboot序列化對(duì)象時(shí)使用大駝峰命名,springboot默認(rèn)使用小駝峰命名

springboot接收json格式的Demo案例

面向API接口開(kāi)發(fā)的時(shí)候,經(jīng)常遇到對(duì)接接口數(shù)據(jù),而數(shù)據(jù)一般是json格式的,在這里記錄一下使用SpringBoot接收json格式數(shù)據(jù)的方式

使用SpringBoot的@RequestBody注解

將json數(shù)據(jù)用字符串去接收,然后轉(zhuǎn)成fastjson的對(duì)象(com.alibaba.fastjson.JSONObject)

package boot.example.json.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 *  螞蟻舞
 */
@RestController
@RequestMapping(value="/boot")
public class BootJsonStrController1 {
    
    @PostMapping(value="/demo1")
    public Object jsonStr1(@RequestBody String str) {
        // 使用fastjson JSONObject
        JSONObject jsonData = JSONObject.parseObject(str);
        System.out.println(jsonData.toJSONString());

        Map<String, Object> map = new HashMap<>();
        map.put("state", true);
        map.put("code", 200);
        map.put("timeStamp", System.currentTimeMillis()/1000);
        return map;
    }
}

也可以用com.alibaba.fastjson2.JSONObject

package boot.example.json.controller;


import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 *  螞蟻舞
 */
@RestController
@RequestMapping(value="/boot")
public class BootJsonStrController2 {
    
    @PostMapping(value="/demo2")
    public Object jsonStr2(@RequestBody String str) {
        // 使用fastjson2 JSONObject
        JSONObject jsonData = JSONObject.parseObject(str);
        System.out.println(jsonData.toJSONString());

        Map<String, Object> map = new HashMap<>();
        map.put("state", true);
        map.put("code", 200);
        map.put("timeStamp", System.currentTimeMillis()/1000);
        return map;
    }
}

fastjson的maven包

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>2.0.25</version>
    <scope>compile</scope>
</dependency>

還可以使用(com.google.gson.JsonObject)

maven包

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>
package boot.example.json.controller;


import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 *  螞蟻舞
 */
@RestController
@RequestMapping(value="/boot")
public class BootJsonStrController3 {
    
    @PostMapping(value="/demo3")
    public Object jsonStr3(@RequestBody String str) {
        Gson gson = new Gson();
        JsonObject json = gson.fromJson(str, JsonObject.class);
        System.out.println(json.toString());

        Map<String, Object> map = new HashMap<>();
        map.put("state", true);
        map.put("code", 200);
        map.put("timeStamp", System.currentTimeMillis()/1000);
        return map;
    }
}

直接使用fastjson的JSONObject對(duì)象

package boot.example.json.controller;

import com.alibaba.fastjson.JSONObject;
//import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 *  螞蟻舞
 */
@RestController
@RequestMapping(value="/boot")
public class BootJsonStrController4 {
    
    @PostMapping(value="/demo4")
    public Object jsonStr4(@RequestBody JSONObject jsonObject) {
        System.out.println(jsonObject.toString());

        Map<String, Object> map = new HashMap<>();
        map.put("state", true);
        map.put("code", 200);
        map.put("timeStamp", System.currentTimeMillis()/1000);
        return map;
    }
}

能不能使用com.google.gson.JsonObject對(duì)象去接收?不能直接用?。。。ㄓ衅渌绞娇捎?,就不去研究這種情況了)

import com.google.gson.JsonObject

// 直接用是不行的
@PostMapping(value="/demoxxx")
public void jsonStr5(@RequestBody JsonObject json) {
    System.out.println(json.toString());
}

簡(jiǎn)單的json數(shù)據(jù)還可以用java具體的對(duì)象的方式去接收,這種方式對(duì)于較復(fù)雜的json數(shù)據(jù)處理起來(lái)挺麻煩的

@PostMapping(value="/demoxxx")
public void jsonStr6(@RequestBody Object object) {
    System.out.println(object.toString());
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中冒泡排序的原生實(shí)現(xiàn)方法(正序與逆序)

    Java中冒泡排序的原生實(shí)現(xiàn)方法(正序與逆序)

    這篇文章主要給大家介紹了關(guān)于Java中冒泡排序的原生實(shí)現(xiàn)方法(正序與逆序)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • SpringMVC和Spring的配置文件掃描包詳解

    SpringMVC和Spring的配置文件掃描包詳解

    這篇文章主要介紹了SpringMVC和Spring的配置文件掃描包,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-05-05
  • Java Web項(xiàng)目中編寫(xiě)定時(shí)任務(wù)的實(shí)現(xiàn)

    Java Web項(xiàng)目中編寫(xiě)定時(shí)任務(wù)的實(shí)現(xiàn)

    本篇文章主要介紹了Java Web項(xiàng)目中編寫(xiě)定時(shí)任務(wù)的實(shí)現(xiàn),具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-01-01
  • SpringBoot調(diào)用WebService接口的實(shí)現(xiàn)示例

    SpringBoot調(diào)用WebService接口的實(shí)現(xiàn)示例

    本文主要介紹了SpringBoot調(diào)用WebService接口的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-03-03
  • Redis集群與SSM整合使用方法

    Redis集群與SSM整合使用方法

    這篇文章主要介紹了Redis集群與SSM整合使用方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-12-12
  • JAVA微信掃碼支付模式二線上支付功能實(shí)現(xiàn)以及回調(diào)

    JAVA微信掃碼支付模式二線上支付功能實(shí)現(xiàn)以及回調(diào)

    本篇文章主要介紹了JAVA微信掃碼支付模式二線上支付功能實(shí)現(xiàn)以及回調(diào),這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。
    2016-11-11
  • Java生成及校驗(yàn)token的實(shí)踐

    Java生成及校驗(yàn)token的實(shí)踐

    Token 的生成和校驗(yàn)機(jī)制為應(yīng)用程序提供了一種安全的身份驗(yàn)證和授權(quán)方式,可以用于用戶認(rèn)證、API 訪問(wèn)控制等場(chǎng)景,本文主要介紹了Java生成及校驗(yàn)token的實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-04-04
  • java直接插入排序示例

    java直接插入排序示例

    這篇文章主要介紹了java直接插入排序示例,插入排序的比較次數(shù)仍然是n的平方,但在一般情況下,它要比冒泡排序快一倍,比選擇排序還要快一點(diǎn)。它常常被用在復(fù)雜排序算法的最后階段,比如快速排序。
    2014-05-05
  • 哈希表在算法題目中的實(shí)際應(yīng)用詳解(Java)

    哈希表在算法題目中的實(shí)際應(yīng)用詳解(Java)

    散列表(Hash?table,也叫哈希表)是根據(jù)關(guān)鍵碼值(Key?value)而直接進(jìn)行訪問(wèn)的數(shù)據(jù)結(jié)構(gòu),下面這篇文章主要給大家介紹了關(guān)于哈希表在算法題目中的實(shí)際應(yīng)用,文中介紹的方法是Java,需要的朋友可以參考下
    2024-03-03
  • Spring Boot使用GridFS實(shí)現(xiàn)文件的上傳和下載方式

    Spring Boot使用GridFS實(shí)現(xiàn)文件的上傳和下載方式

    這篇文章主要介紹了Spring Boot使用GridFS實(shí)現(xiàn)文件的上傳和下載方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評(píng)論