post json 对象数组到spring mvc 处理方式

想法和行动 发布于 2013/04/16 14:22
阅读 22K+
收藏 2

1.客户端post 的json格式

{
    "apList": [
        {
            "bssid": "1234444",
            "name": "fasfafaf",
            "signalStrength ": "80",
            "scanTime": "UTC 2013-03-12 10:12:12",
            "status": "true"
        }
    ]
}

服务端spring mvc处理


@RequestMapping(value = "test/uppost", method = RequestMethod.POST)
    @ResponseBody
    public void uploadTest(@RequestBody ApList apList) {}
ApList类如下



public class ApList {
	private List<ApBean> apList;

	public List<ApBean> getApList() {
		return apList;
	}

	public void setApList(List<ApBean> apList) {
		this.apList = apList;
	}

}

ApBean类如下

public class ApBean {
	private String bssid;
	private String name;
	private int signalStrength;
	private String scanTime;
	private boolean status;
 //get set ......
}

如何能够做到在post接受方法uploadTest方法中自动bean注入 ?

或者说:@RequestBody ApList aplist,应该写成何种形式 ?



加载中
0
倪闯
倪闯

3     public void uploadTest(@RequestBody ApList apList) {}

把@ResponseBody换成@RequestParam试试


倪闯
倪闯
回复 @nginx困惑 : 我不知道你这种复杂的对象支不支持,你试一下只传入ApBean对象,应该是可以的
想法和行动
想法和行动
@RequestBody是可以的,我将ApList的属性改成private List<String> apList; 是可以获取String类型的,我想要的是Spring mvc能不能将Post的list类型里面的json直接封装成一个对象里面,不需要自己再通过JSONArray去转换或者什么的? 谢谢你的评论
0
tony1987
tony1987
@RequestBody ApBean[] array
0
行水
行水

兄台,你这个问题后来是怎么解决的啊,我今天也遇见了一样的问题。json数组不能够自动转换为list<对象>..

想法和行动
想法和行动
这个问题已经解决,spring mvc天然支持list方式,我的错误是输入失误,一时没看出来。"signalStrength ": "80", 字符串里面包含了空格
0
王静0716
王静0716
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported同楼主,遇到这个错误

想法和行动
想法和行动
你这个错误是contentType不支持json的原因吧
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部