[英]Converting object to json control fields name upper case first letter
Object: 宾语:
@XmlRootElement
public class AccountSyncResponse
{
private String Result;
private String Value;
public AccountSyncResponse() {}
public String getResult() {return Result;}
public void setResult(String Result) {this.Result = Result;}
public String getValue() {return Value;}
public void setValue(String Value) {this.Value = Value;}
}
Rest Web Service: 其余Web服务:
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public AccountSyncResponse excute(AccountSyncRequest ASReq)
{
AccountSyncResponse ASRes = new AccountSyncResponse();
return ASRes;
}
The result is {"result":"Create","value":"123456"}
结果是
{"result":"Create","value":"123456"}
I need it the first letter of fields name to be in upper case {"Result":"Create","Value":"123456"}
我需要字段名称的首字母大写
{"Result":"Create","Value":"123456"}
How can I control fields name in the resulting json string ? 如何控制结果json字符串中的字段名称?
You can use @XmlElement
as show below: 您可以使用
@XmlElement
,如下所示:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class AccountSyncResponse {
@XmlElement(name = "Result")
private String result;
@XmlElement(name = "Value")
private String value;
// Default constructor, getters and setters
}
Alternatively you can annotate the getters with @XmlElement
(then the @XmlAccessorType
annotation is not required). 或者,您可以使用
@XmlElement
注释吸气剂(然后@XmlAccessorType
注释)。
Alternatively to JAXB annotation, you might want to consider Jackson. 除了JAXB注释,您可能还需要考虑Jackson。 It's a popular JSON parser for Java that can be used with Jersey .
这是可用于Jersey的流行的Java JSON解析器。 Then you can use
@JsonProperty
instead (however Jackson can also work with JAXB annotations). 然后,您可以改用
@JsonProperty
(但是Jackson也可以使用JAXB注释)。
With Jackson, depending on your needs, you can use a PropertyNamingStrategy
such as PropertyNamingStrategy.UpperCamelCaseStrategy
. 对于Jackson,根据您的需求,可以使用
PropertyNamingStrategy
例如PropertyNamingStrategy.UpperCamelCaseStrategy
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.