为什么使用 Jersey 在 JSON 中返回带有 @ 的名称

发布于 2024-09-10 06:14:30 字数 631 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余生共白头 2024-09-17 06:14:30

在 JSON 中,使用 @XmlAttribute 映射的任何属性都将以“@”为前缀。如果您想删除它,只需使用 @XmlElement 注释您的属性即可。

据推测,这是为了避免潜在的名称冲突:

@XmlAttribute(name="foo") public String prop1;  // maps to @foo in JSON
@XmlElement(name="foo") public String prop2;  // maps to foo in JSON

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:

@XmlAttribute(name="foo") public String prop1;  // maps to @foo in JSON
@XmlElement(name="foo") public String prop2;  // maps to foo in JSON
奢望 2024-09-17 06:14:30

如果您要编组到 XML 和 JSON,并且不需要将其作为 XML 版本中的属性,那么建议使用 @XmlElement 是最好的方法。

但是,如果它需要是 XML 版本中的属性(而不是元素),那么您确实有一个相当简单的替代方案。

您可以轻松设置一个 JSONConfiguration 来关闭“@”的插入。

它看起来像这样:

@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
private JAXBContext context;

public JAXBContextResolver() throws Exception {
    this.context=   new JSONJAXBContext(
        JSONConfiguration
            .mapped()
            .attributeAsElement("StatusMessage",...)
            .build(), 
            ResponseDetails.class); 
}

@Override
public JAXBContext getContext(Class<?> objectType) {
    return context;
}
}

这里还有一些其他替代文档:

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:

@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
private JAXBContext context;

public JAXBContextResolver() throws Exception {
    this.context=   new JSONJAXBContext(
        JSONConfiguration
            .mapped()
            .attributeAsElement("StatusMessage",...)
            .build(), 
            ResponseDetails.class); 
}

@Override
public JAXBContext getContext(Class<?> objectType) {
    return context;
}
}

There are also some other alternatives document here:

http://jersey.java.net/nonav/documentation/latest/json.html

飘过的浮云 2024-09-17 06:14:30

您必须将 JAXBContext 配置中的 JSON_ATTRIBUTE_PREFIX 设置为 "",默认情况下为 "@"

properties.put(JAXBContextProperties.JSON_ATTRIBUTE_PREFIX, ""); 

You have to set JSON_ATTRIBUTE_PREFIX in your JAXBContext configuration to "" which by default is "@":

properties.put(JAXBContextProperties.JSON_ATTRIBUTE_PREFIX, ""); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文