現(xiàn)在正在做的項(xiàng)目要將舊系統(tǒng)實(shí)現(xiàn)微服務(wù),用 springboot 來做,有時(shí)候同一個(gè) request 就要同時(shí)接收來自 ajax 的 json 數(shù)據(jù)和 restful 的數(shù)據(jù),如果里面還包含 map 怎么辦呢? 最近就只想出了這種辦法,僅供參考。如有錯(cuò)誤請(qǐng)指正,謝謝。
代碼
json 數(shù)據(jù)
1
2
3
4
5
6
7
8
|
{ "fieldmap" : { "middlename" : "1" , "mailingaddress" : "2" , "mobilenumber" : "3" } } |
restful url
1
2
|
//注意要讓 @modelattribute requestdto 自動(dòng)封裝成 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 數(shù)據(jù), consumes = "application/json" 來區(qū)分同一個(gè)請(qǐng)求是用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 數(shù)據(jù), @modelattribute 將param配對(duì)成 requestdto @requestmapping (method = { requestmethod.post }, value = { "/hello" }) public final void restfulrequest( final httpservletrequest httprequest, final httpservletresponse httpresponse, @modelattribute final requestdto requestdto ){ ... } |
以上這篇解決springmvc同時(shí)接收json和restful時(shí)request里有map的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/lwdlouis/article/details/66975252