Jackson ObjectMapper upper/lower case issues
I've had the same problem.
This is my solution:
public class MyNamingStrategy extends PropertyNamingStrategy {
@Override
public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
return field.getName();
}
@Override
public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return convert(method, defaultName);
}
@Override
public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return convert(method, defaultName);
}
private String convert(AnnotatedMethod method, String defaultName) {
Class<?> clazz = method.getDeclaringClass();
List<Field> flds = FieldUtils.getAllFieldsList(clazz);
for (Field fld : flds) {
if (fld.getName().equalsIgnoreCase(defaultName)) {
return fld.getName();
}
}
return defaultName;
}
}
In this case you will get the exact name of the property, and will not have to depend on the correct names of the methods.
I think that this is the solution (using custom PropertyNamingStrategy):
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
public class MyPropertyNamingStrategy extends PropertyNamingStrategy {
@Override
public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
return convert(field.getName());
}
@Override
public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return convert(method.getName().toString());
}
@Override
public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return convert(method.getName().toString());
}
private String convert(String input) {
return input.substring(3);
}
}
Test
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.IOException;
import java.io.StringWriter;
public class MyPropertyNamingStrategyTest {
public static void main(String[] args) {
PrivatePerson privatePerson = new PrivatePerson();
privatePerson.setFirstName("John");
privatePerson.setLastName("Smith");
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(new MyPropertyNamingStrategy());
mapper.enable(SerializationFeature.INDENT_OUTPUT);
StringWriter sw = new StringWriter();
try {
mapper.writeValue(sw, privatePerson);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(sw.toString());
}
}
PrivatePerson
public class PrivatePerson {
private String firstName;
private String lastName;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
}
Even though @JsonProperty
doesn't work, I was able to use @JsonSetter
and @JsonGetter
to map capitalized json field names.
@JsonSetter("ABC")
public void setABC(String ABC) {
this.ABC= ABC;
}
Spring will now serialize the object field as "ABC" and not "abc".
You can configure Jackson to be case sensitivity tolerant:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
Cudos to https://stackoverflow.com/a/32842962/1639556