如何在 Spring MVC 控制器的 Java Map 中获取带有子域的 JSON 字符串 [英] How to get JSON String with Subdomain in Java Map at Spring MVC Controller
问题描述
我正在尝试在 Spring MVC 控制器接收带有子域的 JSON 字符串到 Java 映射.
I'm trying to receive a JSON String with subdomain to a Java Map at Spring MVC Controller.
假设我在 JavaScript 部分有这个 JSON:
Let's say I have this JSON at the JavaScript part:
var contract = {dateFrom:someDate,
dateTo:someDate,
season: {seasonCode:someString}}
JSON 作为 GET Ajax 调用发送.
The JSON is sent as GET Ajax call.
控制器看起来像这样:
@RequestMapping(value = "/", method = RequestMethod.GET, headers = "Accept=application/json")
public ResponseEntity<String> getVertagFromSearch(@RequestParam Map<String, Object> allRequestParams, ModelMap model){
Map 的输出如下所示:
The output of the Map looks like this:
{dateFrom=02-07-2014, dateTo=02-07-2014, season[seasonCode]=SO03}
GET 请求如下所示:
The GET Request looks like this:
http://localhost:8080/contracts/?dateFrom=02-07-2014&dateTo=02-07-2014&season%5BseasonCode%5D=SO03
我想将此 Map 解析为我的合同域对象.但是使用这种结构它不起作用.没有子域(季节)它工作.
I want to parse this Map into my contract domain object. But with this structure it doesnt work. Without a subdomain(season) it worked.
提前致谢!
更新 1
作为一个对象发送,它看起来像这样:(输出浏览器)
Sending as an object it looks like this: (output Browser)
Object { dateFrom: "09-07-2014", dateTo: "08-07-2014", season: Object }
在 JSON.stringify 之后发送它看起来像这样:
Sending after JSON.stringify it looks like this:
"{"fromDate":"09-07-2014","dateTo":"08-07-2014","season":{"saiCode":"SO03"}}"
在这种情况下,我认为probem是开头和结尾的双引号.
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) 将您的 JS 对象修改为只有简单的对象.如果您在更新前查看您的请求网址,您会发现:
1) Modify your JS object to have only simple objects. If you look at your request URL before your update, you had:
{您的 IP:port}/contracts/?dateFrom=02-07-2014&dateTo=02-07-2014&season%5BseasonCode%5D=SO03
{your IP:port}/contracts/?dateFrom=02-07-2014&dateTo=02-07-2014&season%5BseasonCode%5D=SO03
那根本不是 JSON,是一个带有三个参数的简单请求:
That is not JSON at all, is a simple request with three parameters:
dateFrom = 02-07-2014
dateTo = 02-07-2014
season%5BseasonCode%5D= SO03 // %5B and %5D are '[' and ']' escaped
所以你的 javascript 正在以普通参数转换一个 JS 对象(不是 JSON).
So your javascript is transforming a JS object (not JSON) in plain parameters.
2) 使用您的 JSON 结构发送一个参数,一个字符串,然后使用一些 JSON 库来解析它:
2) Send a parameter, a string, with your JSON structure and then use some JSON library to parse it:
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);
...
}
通常使用 POST 发送 JSON 更容易(只需将其添加到正文中),但由于您的请求似乎需要数据,因此 POST 请求不是一个好主意.大多数其他 API 使用第一种方法,JSON 往往是响应的一部分,而不是 GET 请求的一部分.
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. Most of the rest APIs use the first approach, JSON tends to be part of a response more than part of a GET request.
这篇关于如何在 Spring MVC 控制器的 Java Map 中获取带有子域的 JSON 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!