为什么使用 Jersey 在 JSON 中返回带有 @ 的名称
我正在使用 JAXB,它是 Jersey JAX-RS 的一部分。当我为我的输出类型请求 JSON 时,我的所有属性名称都以星号开头,如下所示,
This object;
package com.ups.crd.data.objects;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
@XmlType
public class ResponseDetails {
@XmlAttribute public String ReturnCode = "";
@XmlAttribute public String StatusMessage = "";
@XmlAttribute public String TransactionDate ="";
}
变成这样了,
{"ResponseDetails":{"@transactionDate":"07-12-2010",
"@statusMessage":"Successful","@returnCode":"0"}
那么,为什么名字里有@呢?
I am using the JAXB that is part of the Jersey JAX-RS. When I request JSON for my output type, all my attribute names start with an asterisk like this,
This object;
package com.ups.crd.data.objects;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
@XmlType
public class ResponseDetails {
@XmlAttribute public String ReturnCode = "";
@XmlAttribute public String StatusMessage = "";
@XmlAttribute public String TransactionDate ="";
}
becomes this,
{"ResponseDetails":{"@transactionDate":"07-12-2010",
"@statusMessage":"Successful","@returnCode":"0"}
So, why are there @ in the name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 JSON 中,使用 @XmlAttribute 映射的任何属性都将以“@”为前缀。如果您想删除它,只需使用 @XmlElement 注释您的属性即可。
据推测,这是为了避免潜在的名称冲突:
Any properties mapped with @XmlAttribute will be prefixed with '@' in JSON. If you want to remove it simply annotated your property with @XmlElement.
Presumably this is to avoid potential name conflicts:
如果您要编组到 XML 和 JSON,并且不需要将其作为 XML 版本中的属性,那么建议使用 @XmlElement 是最好的方法。
但是,如果它需要是 XML 版本中的属性(而不是元素),那么您确实有一个相当简单的替代方案。
您可以轻松设置一个
JSONConfiguration
来关闭“@”的插入。它看起来像这样:
这里还有一些其他替代文档:
http:// /jersey.java.net/nonav/documentation/latest/json.html
If you are marshalling to both XML and JSON, and you don't need it as an attribute in the XML version then suggestion to use @XmlElement is the best way to go.
However, if it needs to be an attribute (rather than an element) in the XML version, you do have a fairly easy alternative.
You can easily setup a
JSONConfiguration
that turns off the insertion of the "@".It would look something like this:
There are also some other alternatives document here:
http://jersey.java.net/nonav/documentation/latest/json.html
您必须将
JAXBContext
配置中的JSON_ATTRIBUTE_PREFIX
设置为""
,默认情况下为"@"
:You have to set
JSON_ATTRIBUTE_PREFIX
in yourJAXBContext
configuration to""
which by default is"@"
: