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

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

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

SpringBoot接收json格式的Demo

面向API接口開發(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ù)處理起來挺麻煩的

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

總結(jié)

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

相關(guān)文章

  • IDEA修改idea.vmoptions后,IDEA無法打開的解決方案

    IDEA修改idea.vmoptions后,IDEA無法打開的解決方案

    文章介紹了在IDEA中因錯(cuò)誤修改啟動(dòng)參數(shù)導(dǎo)致無法啟動(dòng)的問題,指出正確的修改文件位置應(yīng)在破解插件目錄下的idea.vmoptions,并分享了個(gè)人經(jīng)驗(yàn)供參考
    2025-10-10
  • eclipse/IDEA配置javafx項(xiàng)目步驟(圖文教程)

    eclipse/IDEA配置javafx項(xiàng)目步驟(圖文教程)

    這篇文章主要介紹了eclipse/IDEA配置javafx項(xiàng)目步驟(圖文教程),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • 在mybatis中如何將Map作為參數(shù)

    在mybatis中如何將Map作為參數(shù)

    這篇文章主要介紹了在mybatis中如何將Map作為參數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • SpringBoot整合MongoDB的完整操作指南

    SpringBoot整合MongoDB的完整操作指南

    在實(shí)際項(xiàng)目開發(fā)中,合理封裝MongoDB的操作工具類,可以大幅提升代碼復(fù)用性和維護(hù)性,本文將帶你從零開始實(shí)現(xiàn)一個(gè)功能完善的MongoDB工具類,涵蓋基礎(chǔ)CRUD和高級(jí)查詢功能,需要的朋友可以參考下
    2026-02-02
  • Java基礎(chǔ)知識(shí)精通循環(huán)結(jié)構(gòu)與break及continue

    Java基礎(chǔ)知識(shí)精通循環(huán)結(jié)構(gòu)與break及continue

    循環(huán)結(jié)構(gòu)是指在程序中需要反復(fù)執(zhí)行某個(gè)功能而設(shè)置的一種程序結(jié)構(gòu)。它由循環(huán)體中的條件,判斷繼續(xù)執(zhí)行某個(gè)功能還是退出循環(huán),選擇結(jié)構(gòu)用于判斷給定的條件,根據(jù)判斷的結(jié)果判斷某些條件,根據(jù)判斷的結(jié)果來控制程序的流程
    2022-04-04
  • SpringBoot整合mybatis的方法詳解

    SpringBoot整合mybatis的方法詳解

    這篇文章主要為大家詳細(xì)介紹了SpringBoot整合mybatis的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Spring Boot實(shí)現(xiàn)郵件注冊(cè)功能示例代碼

    Spring Boot實(shí)現(xiàn)郵件注冊(cè)功能示例代碼

    本篇文章主要介紹了Spring Boot實(shí)現(xiàn)郵件注冊(cè)功能示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • Java 實(shí)現(xiàn)分布式服務(wù)的調(diào)用鏈跟蹤

    Java 實(shí)現(xiàn)分布式服務(wù)的調(diào)用鏈跟蹤

    分布式服務(wù)中完成某一個(gè)業(yè)務(wù)動(dòng)作,需要服務(wù)之間的相互協(xié)作才能完成,在這一次動(dòng)作引起的多服務(wù)的聯(lián)動(dòng)我們需要用1個(gè)唯一標(biāo)識(shí)關(guān)聯(lián)起來,關(guān)聯(lián)起來就是調(diào)用鏈的跟蹤。本文介紹了Java 實(shí)現(xiàn)分布式服務(wù)的調(diào)用鏈跟蹤的步驟
    2021-06-06
  • 解決FastJson中

    解決FastJson中"$ref重復(fù)引用"的問題方法

    這篇文章主要介紹了解決FastJson中"$ref重復(fù)引用"的問題方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-11-11
  • 淺談Java反射與代理

    淺談Java反射與代理

    下面小編就為大家?guī)硪黄獪\談Java反射與代理。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-07-07

最新評(píng)論

仪陇县| 陆川县| 宜丰县| 织金县| 无为县| 乌什县| 民权县| 乌拉特中旗| 陈巴尔虎旗| 宜黄县| 锡林浩特市| 湟中县| 花垣县| 越西县| 噶尔县| 朝阳市| 凤山县| 曲阳县| 昭觉县| 宣汉县| 敖汉旗| 承德县| 曲阜市| 天峻县| 和林格尔县| 江西省| 莒南县| 平原县| 灯塔市| 丰宁| 湘乡市| 外汇| 年辖:市辖区| 革吉县| 兴仁县| 泸州市| 霞浦县| 淮北市| 紫金县| 杭州市| 涡阳县|