SpringBoot接收json格式的Demo案例

面向API接口开发的时候,经常遇到对接接口数据,而数据一般是json格式的,在这里记录一下使用SpringBoot接收json格式数据的方式

使用SpringBoot的@RequestBody注解

将json数据用字符串去接收,然后转成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数据还可以用java具体的对象的方式去接收,这种方式对于较复杂的json数据处理起来挺麻烦的

@PostMapping(value="/demoxxx")
public void jsonStr6(@RequestBody Object object) {
	System.out.println(object.toString());
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值