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

Joh*_*ohn 8 java json jax-rs jaxb jersey

我正在使用作为Jersey JAX-RS一部分的JAXB.当我为输出类型请求JSON时,我的所有属性名称都以这样的星号开头,

这个对象;

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 ="";
}
Run Code Online (Sandbox Code Playgroud)

成为这个,

   {"ResponseDetails":{"@transactionDate":"07-12-2010",  
             "@statusMessage":"Successful","@returnCode":"0"}
Run Code Online (Sandbox Code Playgroud)

那么,为什么名字中有@?

bdo*_*han 9

使用@XmlAttribute映射的任何属性都将在JSON中以"@"为前缀.如果要删除它,只需使用@XmlElement注释您的属性.

大概这是为了避免潜在的名称冲突:

@XmlAttribute(name="foo") public String prop1;  // maps to @foo in JSON
@XmlElement(name="foo") public String prop2;  // maps to foo in JSON
Run Code Online (Sandbox Code Playgroud)