在REST请求中添加地图的正确方法是什么

时间:2022-08-16 23:07:51

I'm using Google Endpoint and for one of my entities I want to create a POST request that adds a map of properties. What is the right way to do it? I know Google Endpoint can receive a Collection as a parameter, but I want to add a map (unknown key values). Should I pass a JSON as a parameter or just add the JSON in the body of the request and extract it from the HttpServletRequest object?

我正在使用Google Endpoint,对于我的某个实体,我想创建一个添加属性映射的POST请求。这样做的正确方法是什么?我知道Google Endpoint可以接收Collection作为参数,但我想添加一个地图(未知的键值)。我应该将JSON作为参数传递,还是仅在请求正文中添加JSON并从HttpServletRequest对象中提取它?

2 个解决方案

#1


0  

Every JSON object is a map, so it looks like the most obvious choice. GSON makes it easy, but you can use other parsers too.

每个JSON对象都是一个地图,因此它看起来是最明显的选择。 GSON让它变得简单,但您也可以使用其他解析器。

Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> myMap = gson.fromJson("{'k1':'apple','k2':'orange'}", type);

#2


1  

I would avoid passing it as a parameter. You can send it in the body of the request and then use json library to get a python object.

我会避免将其作为参数传递。您可以在请求的正文中发送它,然后使用json库来获取python对象。

https://docs.python.org/3/library/json.html

https://docs.python.org/3/library/json.html

#1


0  

Every JSON object is a map, so it looks like the most obvious choice. GSON makes it easy, but you can use other parsers too.

每个JSON对象都是一个地图,因此它看起来是最明显的选择。 GSON让它变得简单,但您也可以使用其他解析器。

Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> myMap = gson.fromJson("{'k1':'apple','k2':'orange'}", type);

#2


1  

I would avoid passing it as a parameter. You can send it in the body of the request and then use json library to get a python object.

我会避免将其作为参数传递。您可以在请求的正文中发送它,然后使用json库来获取python对象。

https://docs.python.org/3/library/json.html

https://docs.python.org/3/library/json.html