[英]How to get JSON String with Subdomain in Java Map at Spring MVC Controller
I'm trying to receive a JSON String with subdomain to a Java Map at Spring MVC Controller. 我正在尝试在Spring MVC Controller上接收带有Java Map子域的JSON字符串。
Let's say I have this JSON at the JavaScript part: 假设我在JavaScript部分具有此JSON:
var contract = {dateFrom:someDate,
dateTo:someDate,
season: {seasonCode:someString}}
The JSON is sent as GET Ajax call. JSON作为GET Ajax调用发送。
The Controller looks like this: 控制器看起来像这样:
@RequestMapping(value = "/", method = RequestMethod.GET, headers = "Accept=application/json")
public ResponseEntity<String> getVertagFromSearch(@RequestParam Map<String, Object> allRequestParams, ModelMap model){
The output of the Map looks like this: Map的输出如下所示:
{dateFrom=02-07-2014, dateTo=02-07-2014, season[seasonCode]=SO03}
The GET Request looks like this: GET请求如下所示:
http://localhost:8080/contracts/?dateFrom=02-07-2014&dateTo=02-07-2014&season%5BseasonCode%5D=SO03
I want to parse this Map into my contract domain object. 我想将此Map解析为我的合同域对象。 But with this structure it doesnt work.
但是,使用这种结构是行不通的。 Without a subdomain(season) it worked.
没有子域(季节)就可以了。
Thanks in advance! 提前致谢!
Update 1 更新1
Sending as an object it looks like this: (output Browser) 作为对象发送,如下所示:(输出浏览器)
Object { dateFrom: "09-07-2014", dateTo: "08-07-2014", season: Object }
Sending after JSON.stringify it looks like this: 在JSON.stringify之后发送,如下所示:
"{"fromDate":"09-07-2014","dateTo":"08-07-2014","season":{"saiCode":"SO03"}}"
In this case I think the probem are the double quotes at the beginning and at the end. 在这种情况下,我认为该探询是开头和结尾处的双引号。
I think the best two options are: 我认为最好的两个选择是:
1) Modify your JS object to have only simple objects . 1)修改JS对象,使其仅包含简单对象 。 If you look at your request URL before your update, you had:
如果在更新之前查看您的请求URL,则您有:
{your IP:port}/contracts/?dateFrom=02-07-2014&dateTo=02-07-2014&season%5BseasonCode%5D=SO03
{您的IP:端口} / contracts /?dateFrom = 02-07-2014&dateTo = 02-07-2014&season%5BseasonCode%5D = SO03
That is not JSON at all, is a simple request with three parameters: 那根本不是JSON,它是带有三个参数的简单请求:
dateFrom = 02-07-2014
dateTo = 02-07-2014
season%5BseasonCode%5D= SO03 // %5B and %5D are '[' and ']' escaped
So your javascript is transforming a JS object (not JSON) in plain parameters. 因此,您的JavaScript会使用简单的参数转换JS对象(而非JSON)。
2) Send a parameter, a string, with your JSON structure and then use some JSON library to parse it: 2)使用您的JSON结构发送参数(字符串),然后使用一些JSON库对其进行解析:
http://localhost:8080/contracts/?myJson=<JSON_String>
and then modify your controller as: 然后将您的控制器修改为:
@RequestMapping(value="/", method=RequestMethod.GET, headers="Accept=text/plain")
public ResponseEntity<String> getVertagFromSearch(@RequestParam String myJson, ModelMap model){
JSONObject myJSON= new JSONObject (myJson);
...
}
Usually sending a JSON is easier with a POST (just adding it to the body), but as your request seems to ask for data, a POST request is not a good idea. 通常,通过POST(将其添加到正文中)来发送JSON更为容易,但是由于您的请求似乎要求数据,因此POST请求并不是一个好主意。 Most of the rest APIs use the first approach, JSON tends to be part of a response more than part of a GET request.
其余大多数API使用第一种方法,JSON往往是响应的一部分,而不是GET请求的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.