Spring MVC中接受接口传过来的JSON数组对象

MVC的@Controller 层中
@RequestMapping("/api/v1/gis") 拦截
@RequestMapping(value = “/handle”,method = RequestMethod.POST)
//找到拦截的 POST 请求后
public GisResponseDTO gisHandle(@RequestBody GisRequestDTO gisRequestDTO){
@RequestBody 后就是JSON格式要转化成什么什么,这里转换成 GisRequestDTO 对象
}

JSON 请求过来的数据
{
	"head": {
		"token":"aaa",
		"msgidt":"bbb",
		"userName":"ccc"
	},
	"data":[{
	"one":"first",
	"two":"second",
	"three":"third"
	},
	{
	"color1":"yellow",
	"color2":"blue",
	"color3":"red"
	}]
}

在GisResponseDTO 中获取

public class GisRequestDTO  {
    /**
     * 消息头
     */
    private Map head;
	/**
     * 消息体,JSON数组中可以有多个对象
     */
    //private Map data;	如果不用数组对象
    private List> data;		//有数组对象

//访问消息头
	public String getToken(){
        return (String) head.get("token");
    }
   //遍历消息体
   List> cityEditList = gisRequestDTO.getData();
    	for(Map cityEdit : cityEditList){
    	Systen.out.print(cityEdit.get("cityId"));
}

注意:发送过来的JSON格式的Post请求,请求头(信息头)必须是:content-type : application/json

https://www.cnblogs.com/wangqiao170/p/9121374.html


另一种方式是在@RequesBody 后面接( Object[] obj )对象数组

ajax发送Java Bean对象接收遍历JSON
https://blog.csdn.net/rentian1/article/details/80848882
https://greatpwx.iteye.com/blog/1974150 不用List?
详细介绍:https://blog.csdn.net/qq_33981088/article/details/79525741

你可能感兴趣的:(Spring MVC中接受接口传过来的JSON数组对象)