Java 在 Spring MVC 中重命名返回到 ResponseBody 的 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19022659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Renaming JSON objects returned to ResponseBody in Spring MVC
提问by user2219247
Lets say I have a list of objects like this: LinkedList<JsonAssessment> jsonAssessments...
.
可以说我有这样的对象列表:LinkedList<JsonAssessment> jsonAssessments...
。
It is returned to this kind of method:
它返回到这种方法:
@RequestMapping(value = "mapping", method = RequestMethod.POST)
public
@ResponseBody
List<JsonAssessment> doSomething(....) {
.....
}
I am making AJAX call to this controller everything is working correctly as expected but I don't like the naming my JSON is returned. In firebug I am seeing:
我正在对这个控制器进行 AJAX 调用,一切都按预期正常工作,但我不喜欢返回我的 JSON 的命名。在萤火虫中,我看到:
{"LinkedList":[{"assessmentName":"........
The question is how can I rename that root element LinkedList
? Is there any config I have to set?
问题是如何重命名该根元素LinkedList
?有什么我必须设置的配置吗?
EDIT
编辑
I do not want to use any wrapper objects.
我不想使用任何包装对象。
EDIT
编辑
My ObjectMapper:
我的对象映射器:
public class JsonObjectMapper extends ObjectMapper {
public JsonObjectMapper() {
super();
this.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
this.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
this.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
}
Spring: 3.2.4.RELEASE Hymanson: 2.1.2
春季:3.2.4.RELEASE Hyman逊:2.1.2
EDIT
编辑
This object mapper is declared in MVC message converters:
这个对象映射器在 MVC 消息转换器中声明:
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter">
<property name="objectMapper" ref="jsonObjectMapper"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
And that is what I've tried with naming strategy:
这就是我尝试的命名策略:
public class JsonPropertyNamingStrategy extends PropertyNamingStrategy {
public static final MyNamingStrategy MY_NAMING_STRATEGY = new MyNamingStrategy();
private static final LinkedHashMap<String, String> PROPERTIES = new LinkedHashMap<String, String>() {{
put("LinkedList", "resultList");
}};
public static class MyNamingStrategy extends PropertyNamingStrategyBase {
@Override
public String translate(String propertyName) {
if (propertyName == null) {
return null;
}
if (PROPERTIES.containsKey(propertyName)) {
return PROPERTIES.get(propertyName);
}
return propertyName;
}
}
}
I was debugging it and when it comes to translate method property names comes all except the root element. I have everything that LinkedList
contains, but LinkedList
is not coming to this method.
我正在调试它,当涉及到翻译方法属性名称时,除了根元素之外的所有内容都出现了。我拥有LinkedList
包含的所有内容,但LinkedList
不会使用此方法。
回答by NimChimpsky
put the list in a wrapper class is a a commonly implemented strategy
将列表放在包装类中是一种常用的实现策略
回答by schomax
If you are using Hymanson Mapper you can use Annotations to define names of properties in classes by
如果您使用的是 Hymanson Mapper,则可以使用 Annotations 来定义类中的属性名称
@JsonProperty("foo")
or set the order by
或设置顺序
@JsonPropertyOrder({"foo", "bar"})
and so on.
等等。
See further Hymanson Annotations
查看更多Hymanson 注释
edit: Sorry, just saw the wrapper-comment. The only solution i saw is using a wrapper like this: How to rename root key in JSON serialization with Hymanson
编辑:对不起,刚看到包装评论。我看到的唯一解决方案是使用这样的包装器:How to rename root key in JSON serialization with Hymanson
回答by M. Deinum
Instead of directly returning a the List, wrap it inside a ResponseEntity
that will give you a response without a root element
不是直接返回一个 List,而是将它包装在 a 中ResponseEntity
,它会给你一个没有根元素的响应
@RequestMapping(value = "mapping", method = RequestMethod.POST)
public
@ResponseBody ResponseEntity<List<JsonAssessment>> doSomething(....) {
.....
return new ResponseEntity(yourList);
}
That way you don't have a root element. If you still want a root element you could add it to a Map
. Results in the following JSON "[{"assessmentName":"........]
这样你就没有根元素。如果你仍然想要一个根元素,你可以将它添加到Map
. 结果在以下 JSON"[{"assessmentName":"........]
@RequestMapping(value = "mapping", method = RequestMethod.POST)
public
@ResponseBody ResponseEntity<Map<String, List<JsonAssessment>>> doSomething(....) {
.....
Map results = new HashMap();
result.put("assesments", yourlist);
return new ResponseEntity(results);
}
Should output {"assesments":[{"assessmentName":"........
应该输出 {"assesments":[{"assessmentName":"........
Although you are stilling wrapping the objects here, it is in objects that are freely available, you don't have to add your own custom classes.
尽管您仍在此处包装对象,但它位于免费提供的对象中,您不必添加自己的自定义类。
This is what we are using in a couple of our @Controller
s, we are using Spring 3.2 and Hymanson 2.2.
这是我们在几个@Controller
s 中使用的,我们使用的是 Spring 3.2 和 Hymanson 2.2。