SoFunction
Updated on 2025-03-08

About SpringBoot's Demo case for receiving json format

SpringBoot receives a demo in json format

When developing API interfaces, we often encounter docking interface data, and the data is generally in json format.

Here is a record of how to use SpringBoot to receive json format data

@RequestBody annotation using SpringBoot

Receive json data using string

Then convert it to fastjson object()

package ;

import ;
import ;
import ;
import ;
import ;

import ;
import ;

/**
  * Ant dance
  */
@RestController
@RequestMapping(value="/boot")
public class BootJsonStrController1 {
	
	@PostMapping(value="/demo1")
	public Object jsonStr1(@RequestBody String str) {
		// Use fastjson JSONObject		JSONObject jsonData = (str);
		(());

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

Can also be used.

package ;


import .;
import ;
import ;
import ;
import ;

import ;
import ;

/**
  * Ant dance
  */
@RestController
@RequestMapping(value="/boot")
public class BootJsonStrController2 {
	
	@PostMapping(value="/demo2")
	public Object jsonStr2(@RequestBody String str) {
		// Use fastjson2 JSONObject		JSONObject jsonData = (str);
		(());

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

fastjson's maven package

<dependency>
    <groupId></groupId>
    <artifactId>fastjson</artifactId>
    <version>2.0.25</version>
    <scope>compile</scope>
</dependency>

You can also use ()

maven package

<dependency>
    <groupId></groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>
package ;


import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;

/**
  * Ant dance
  */
@RestController
@RequestMapping(value="/boot")
public class BootJsonStrController3 {
    
    @PostMapping(value="/demo3")
    public Object jsonStr3(@RequestBody String str) {
        Gson gson = new Gson();
        JsonObject json = (str, );
        (());

        Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();
        ("state", true);
        ("code", 200);
        ("timeStamp", ()/1000);
        return map;
    }
}

Directly use fastjson's JSONObject object

package ;

import ;
//import .;
import ;
import ;
import ;
import ;

import ;
import ;

/**
  * Ant dance
  */
@RestController
@RequestMapping(value="/boot")
public class BootJsonStrController4 {
    
    @PostMapping(value="/demo4")
    public Object jsonStr4(@RequestBody JSONObject jsonObject) {
        (());

        Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();
        ("state", true);
        ("code", 200);
        ("timeStamp", ()/1000);
        return map;
    }
}

Can objects be used to receive?

Can't be used directly! ! !

(There are other ways to be available, so I won’t study this situation)

import 

// It's not OK to use it directly@PostMapping(value="/demoxxx")
public void jsonStr5(@RequestBody JsonObject json) {
    (());
}

Simple json data can also be received in Java specific objects

This method is quite troublesome for more complex json data processing

@PostMapping(value="/demoxxx")
public void jsonStr6(@RequestBody Object object) {
    (());
}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.