如何控制REST Web Service返回JSON的字段名首字母大写?
Problem Description
I'm developing a REST Web Service with JAX-RS, and I've defined the AccountSyncResponse entity class and corresponding POST endpoint as follows:
AccountSyncResponse Entity:
@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 Endpoint:
@POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public AccountSyncResponse excute(AccountSyncRequest ASReq) { AccountSyncResponse ASRes = new AccountSyncResponse(); return ASRes; }
Currently, the returned JSON uses lowercase-first field names: {"result":"Create","value":"123456"}, but I need the first letter of each field name to be uppercase, like {"Result":"Create","Value":"123456"}. How can I control the format of the generated JSON field names?
Solutions
Depending on which JSON serializer your JAX-RS implementation uses (most commonly Jackson or MOXy), here are the most straightforward approaches:
1. Use Jackson's @JsonProperty Annotation
If your project uses Jackson as the JSON processor (the default for many JAX-RS implementations like Jersey 2.x with Jackson support), you can explicitly specify the desired field name using the @JsonProperty annotation on either the field itself or its getter method:
Option 1: Annotate Getter Methods
@XmlRootElement public class AccountSyncResponse { private String Result; private String Value; public AccountSyncResponse() {} @JsonProperty("Result") public String getResult() {return Result;} public void setResult(String Result) {this.Result = Result;} @JsonProperty("Value") public String getValue() {return Value;} public void setValue(String Value) {this.Value = Value;} }
Option 2: Annotate Fields Directly
@XmlRootElement public class AccountSyncResponse { @JsonProperty("Result") private String Result; @JsonProperty("Value") private String Value; public AccountSyncResponse() {} // Getter and setter methods remain unchanged 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;} }
2. Use JAXB's @XmlElement Annotation
If your JAX-RS implementation uses MOXy (the default JSON processor for some Jersey versions or EE environments) and you're already using JAXB annotations like @XmlRootElement, you can use @XmlElement to define the JSON field name:
@XmlRootElement public class AccountSyncResponse { private String Result; private String Value; public AccountSyncResponse() {} @XmlElement(name = "Result") public String getResult() {return Result;} public void setResult(String Result) {this.Result = Result;} @XmlElement(name = "Value") public String getValue() {return Value;} public void setValue(String Value) {this.Value = Value;} }
MOXy will respect the name attribute in @XmlElement when serializing to JSON, ensuring the field names use uppercase first letters.
3. Global Configuration (Avoid Per-Field Annotations)
If you want to apply the uppercase-first rule to all entities without annotating each field, you can configure the JSON serializer globally:
For Jackson:
Create a custom ObjectMapper with the UPPER_CAMEL_CASE naming strategy, then register it with your JAX-RS provider:
ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE); // Register with Jersey (example) JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider(); provider.setMapper(objectMapper);
This will automatically convert all field names to uppercase-first format for every serialized entity.
内容的提问来源于stack exchange,提问作者asabd

