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

關(guān)于SpringBoot接收json格式的Demo案例

 更新時間:2024年05月17日 10:19:19   作者:螞蟻舞  
這篇文章主要介紹了關(guān)于SpringBoot接收json格式的Demo案例,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

SpringBoot接收json格式的Demo

面向API接口開發(fā)的時候,經(jīng)常遇到對接接口數(shù)據(jù),而數(shù)據(jù)一般是json格式的

在這里記錄一下使用SpringBoot接收json格式數(shù)據(jù)的方式

使用SpringBoot的@RequestBody注解

將json數(shù)據(jù)用字符串去接收

然后轉(zhuǎn)成fastjson的對象(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對象

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對象去接收?

不能直接用?。?!

(有其他方式可用,就不去研究這種情況了)

import com.google.gson.JsonObject

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

簡單的json數(shù)據(jù)還可以用java具體的對象的方式去接收

這種方式對于較復雜的json數(shù)據(jù)處理起來挺麻煩的

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

總結(jié)

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

相關(guān)文章

  • Spring Cloud應(yīng)用實現(xiàn)配置自動刷新過程詳解

    Spring Cloud應(yīng)用實現(xiàn)配置自動刷新過程詳解

    這篇文章主要介紹了Spring Cloud應(yīng)用實現(xiàn)配置自動刷新過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • Spring中實現(xiàn)策略模式的幾種方式小結(jié)

    Spring中實現(xiàn)策略模式的幾種方式小結(jié)

    在寫業(yè)務(wù)代碼的時候,難免會遇到很多if-else,這個時候如果if-else不是很多可以用if-else,如果此時場景過多,太多的if-else會導致代碼比較臃腫,這個時候策略模式就出現(xiàn)了,本文主要闡述工作中常用的實現(xiàn)策略模式的幾種方式,需要的朋友可以參考下
    2024-05-05
  • Java類獲取Spring中bean的5種方式

    Java類獲取Spring中bean的5種方式

    這篇文章主要為大家詳細介紹了Java類獲取Spring中bean的5種方式,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • java學生信息管理系統(tǒng)設(shè)計(2)

    java學生信息管理系統(tǒng)設(shè)計(2)

    這篇文章主要為大家詳細介紹了java學生信息管理系統(tǒng)設(shè)計,學生信息添加進入數(shù)據(jù)庫的事務(wù),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Java ThreadLocal原理解析以及應(yīng)用場景分析案例詳解

    Java ThreadLocal原理解析以及應(yīng)用場景分析案例詳解

    這篇文章主要介紹了Java ThreadLocal原理解析以及應(yīng)用場景分析案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • 詳解spring自動掃描包

    詳解spring自動掃描包

    這篇文章主要介紹了spring自動掃描包的相關(guān)知識,本文通過實例相結(jié)合的形式給大家介紹的非常詳細,感興趣的朋友跟隨腳本之家小編一起看看吧
    2018-06-06
  • 淺析Java中XPath和JsonPath以及SpEL的用法與對比

    淺析Java中XPath和JsonPath以及SpEL的用法與對比

    XPath,即XML路徑語言,是一種用于在XML文檔中查找信息的語言,JsonPath是從XPath中發(fā)展而來的,專門用于JSON數(shù)據(jù)格式,本文主要來講講他們的用法與區(qū)別,需要的可以參考下
    2023-11-11
  • Java8中常見函數(shù)式接口的使用示例詳解

    Java8中常見函數(shù)式接口的使用示例詳解

    在 Java 8 中,函數(shù)式接口是一個關(guān)鍵的特性,它們允許將方法作為參數(shù)傳遞或返回類型,本文為大家整理了一些常見的函數(shù)式接口的使用,希望對大家有所幫助
    2023-12-12
  • Java泛型的使用限制實例分析

    Java泛型的使用限制實例分析

    這篇文章主要介紹了Java泛型的使用限制,結(jié)合實例形式分析了不能使用java泛型的情況以及泛型使用的相關(guān)注意事項,需要的朋友可以參考下
    2019-08-08
  • springBoot配置國產(chǎn)達夢數(shù)據(jù)庫的示例詳解

    springBoot配置國產(chǎn)達夢數(shù)據(jù)庫的示例詳解

    本文向大家介紹springBoot?配置國產(chǎn)達夢數(shù)據(jù)庫的相關(guān)知識,文章結(jié)合示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04

最新評論