现在正在做的项目要将旧系统实现微服务,用 springboot 来做,有时候同一个 request 就要同时接收来自 ajax 的 json 数据和 restful 的数据,如果里面还包含 map 怎么办呢? 最近就只想出了这种办法,仅供参考。如有错误请指正,谢谢。
代码
json 数据
1 2 3 4 5 6 7 8 |
{ "fieldmap" : { "middlename" : "1" , "mailingaddress" : "2" , "mobilenumber" : "3" } } |
restful url
1 2 |
//注意要让 @modelattribute requestdto 自动封装成 map 的话要像下面的format。 http: //localhost:8080/hello?fieldmap[middlename]=1&fieldmap[mailingaddress]=2&fieldmap[mobilenumber]=3 |
request dto
1 2 3 4 5 6 7 8 9 10 11 |
public class requestdto {
private hashmap<string, string> fieldmap;
public hashmap<string, string> getfieldmap() { return fieldmap; } public void setfieldmap(hashmap<string, string> fieldmap) { this .fieldmap = fieldmap; } } |
spring mvc 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
//接收 json 数据, consumes = "application/json" 来区分同一个请求是用json还是其他 @requestmapping (method = { requestmethod.post }, value = { "/hello" }, consumes = "application/json" ) public final void requestbyjson( final httpservletrequest httprequest, final httpservletresponse httpresponse, @requestbody final requestdto requestdto) {
... }
//接收 restful 数据, @modelattribute 将param配对成 requestdto @requestmapping (method = { requestmethod.post }, value = { "/hello" }) public final void restfulrequest( final httpservletrequest httprequest, final httpservletresponse httpresponse, @modelattribute final requestdto requestdto ){
...
} |
以上这篇解决springmvc同时接收json和restful时request里有map的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
原文链接:https://blog.csdn.net/lwdlouis/article/details/66975252
查看更多关于解决SpringMVC同时接收Json和Restful时Request里有Map的问题的详细内容...