Jersey JSON switching from camel case to underscores (snake case)
This is something that needs to be configured with the Jackson ObjectMapper
. You can do this in a ContextResolver
. Basically, you need something like
@Provider
public class MapperProvider implements ContextResolver<ObjectMapper> {
final ObjectMapper mapper;
public MapperProvider() {
mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(
PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
}
@Override
public ObjectMapper getContext(Class<?> cls) {
return mapper;
}
}
Then register with your client
client.register(MapperProvider.class);
If you need this support on the server also, then you will need to register it on the server too.
Multiple ObjectMappers for different parts of API
If both parts of the API
are not dealing with the same domain objects, you'll be able to do it by providing your own implementation of MappingJackson2HttpMessageConverter
that overwrite the canRead
and canWrite
methods. Here an example using the base package of the object which must be serialized :
Custom converter :
public class CustomConverter extends MappingJackson2HttpMessageConverter {
private String basePackage;
public CustomConverter(String basePackage) {
this.basePackage = basePackage;
}
@Override
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
if (contextClass.getName().startsWith(basePackage)) {
return super.canRead(type, contextClass, mediaType);
}
return false;
}
@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
if (clazz.getName().startsWith(basePackage)) {
return super.canWrite(clazz, mediaType);
}
return false;
}
}
Configuration
@Configuration
public class Configuration {
@Bean
public MappingJackson2HttpMessageConverter camelCaseConverter() {
MappingJackson2HttpMessageConverter converter = new CustomConverter("package.camel");
converter.setObjectMapper(camelCaseObjectMapper());
return converter;
}
@Bean
public MappingJackson2HttpMessageConverter snakeCaseConverter() {
MappingJackson2HttpMessageConverter converter = new CustomConverter("package.snake");
converter.setObjectMapper(snakeCaseObjectMapper());
return converter;
}
@Bean
public ObjectMapper camelCaseObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);
return mapper;
}
@Bean
public ObjectMapper snakeCaseObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
return mapper;
}
}
Unable to unmarshal array of elements with inner text
You can use @XmlValue
for the property mapping to the inner text.
@XmlAccessorType(XmlAccessType.FIELD)
public class Item {
@XmlAttribute
private String atr;
@XmlValue
private String value;
}
For More Information
- http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html
Related Topics
Autosize Column Width Using Poi
Jdk Tools.Jar as Maven Dependency
Default Value in Lombok. How to Init Default With Both Constructor and Builder
How to Truncate a Double to Only Two Decimal Places in Java
How to Parallelize a Foreach Loop in Java
Hibernate Foreign Key Issue:Error Executing Ddl "Alter Table..."
In Java How to Extract Exact Domain Name from Url
How to Convert Xml to Java.Util.Map and Vice Versa
Java Ssl: How to Disable Hostname Verification
How to Find Out My MySQL Url, Host, Port and Username
Rounded Swing Jbutton Using Java
How to Unpackage and Repackage a War File
How to Exclude Classes from Test Coverage Using Java Annotations in Spring Boot
How to Test Class Which Implements Runnable With Junit
How to Store a Large (10 Digits) Integer
Get the Nearest Date to Specific from the List of Dates
Handling Static Variables in Multithreaded Java Program
Java Simpledateformatter With 10 Decimals After the Seconds, Cannot Convert to Date