java JAX-RS, Map<String,String> 到 JSON 没有开销?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1900132/
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
JAX-RS, Map<String,String> to JSON without the overhead?
提问by
I'm using JAX-RS to create restful webservices in Java. I am getting to much overhead in the produced JSON.
我正在使用 JAX-RS 在 Java 中创建宁静的 Web 服务。我在生成的 JSON 中获得了很多开销。
Data class:
数据类:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Test {
private Map<String,String> data;
Test() {}
public Test(Map<String,String> data) {
this.data = data;
}
public Map<String, String> getData() {
return data;
}
}
Service:
服务:
@GET
@Path("/test")
@Produces("application/json; charset=UTF-8;")
public Test test() {
Map<String,String> map = new HashMap<String,String>();
map.put("foo", "bar");
map.put("bingo", "bongo");
return new Test(map);
}
Produces:
产生:
{"data":{"entry":[{"key":"foo","value":"bar"},{"key":"bingo","value":"bongo"}]}}
I would like it to produce:
我希望它产生:
{"data":{"foo":"bar","bingo":"bongo"}}
What is the simplest way to achive this? I am free to redifine my data class but I can't know in advance the keys or size of the map.
实现这一目标的最简单方法是什么?我可以自由地重新定义我的数据类,但我无法提前知道地图的键或大小。
回答by BalusC
Simplest way would be using List<Pair>instead where Pairis just a Javabean with two properties.
最简单的方法是使用List<Pair>wherePair只是一个具有两个属性的 Javabean。
回答by user258793
"JAXB spec defines a special handling for Map when it's used as a property of a bean."
“当 Map 用作 bean 的属性时,JAXB 规范定义了对 Map 的特殊处理。”
In light of that, I don't think there is much you can do (currently) except, perhaps, to write your own MessageBodyWriter (https://jsr311.dev.java.net/nonav/javadoc/javax/ws/rs/ext/MessageBodyWriter.html)
有鉴于此,我认为(目前)除了编写自己的 MessageBodyWriter(https://jsr311.dev.java.net/nonav/javadoc/javax/ws/rs /ext/MessageBodyWriter.html)
回答by wds
If you don't use JAXB annotated objects, but simple POJO's, you can get the correct behaviour by simply enabling the POJO mapping feature, together with the Hymanson JSON library.
如果您不使用带 JAXB 注释的对象,而是使用简单的 POJO,则只需启用 POJO 映射功能以及 Hymanson JSON 库即可获得正确的行为。
So in web.xml, if you're using the filter (similar for servlet), your configuration should be:
因此web.xml,如果您使用过滤器(类似于 servlet),您的配置应该是:
<filter>
<filter-name>Jersey</filter-name>
<filter-class>...</filter-class>
<init-param>
? <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
? <param-value>true</param-value>
</init-param>
</filter>
And just depend on the jersey-json dependency in your maven config, or download it from the website and put Hymanson in your classpath. See also this answerand this answer, and this blog post. And why on earth this is not standard behaviour I do not know.
并且只依赖于您的 maven 配置中的 jersey-json 依赖项,或者从网站下载它并将 Hymanson 放在您的类路径中。另请参阅此答案和此答案以及此博客文章。为什么这不是我不知道的标准行为。

