I have a project with spring mvc and i wanna invoke method "setIgnorableProperties" from MapDeserializer globally, but I dont know how get this class from ObjectMapper, can you help me? Thx for advice.
I see it, like that:
#Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
mapDeserializer.getContentType();
converters.forEach(httpMessageConverter -> {
if (httpMessageConverter instanceof MappingJackson2HttpMessageConverter) {
MappingJackson2HttpMessageConverter converter = (MappingJackson2HttpMessageConverter) httpMessageConverter;
ObjectMapper mapper = converter.getObjectMapper();
MapDeserializer mapDes = mapper.(What I have to invoke?) ;
mapDes.setIgnorableProperties({"#id", "#ref"});
}
});
}
That property is not meant to be configured directly; you will need to use #JsonIgnoreProperties annotation for Map-valued properties.
You can create convenience annotation, if you want, by:
#Retention(RetentionPolicy.RUNTIME) // IMPORTANT
#JacksonAnnotationsInside
#JsonIgnoreProperties({ "#id", "#ref" })
public #interface MapIgnorals
and then use like:
public class Stuff {
#MapIgnorals public Map<String,Object> values;
}
Related
I was trying to implement a custom serializer for one of the properties of my object to get a different JSON structure when I return it from my REST controller.
My constraints are I cannot change the interface of the REST controller or the model classes (so I cannot add extra annotation etc, that would maybe make this easier). The only thing I could think of, making it render different than described in the model is a custom serializer, if there are any better approaches for this, please don't hesitate to tell me a different approach that is within the constraints.
My models look something like this:
public class WrapperModel {
// a lot of autogenerated fields
List<Property> properties;
// getters/setters
}
public class Property {
private String name;
private String value;
// getters / setters
}
So when this is rendered is looks like so:
{ ....
"properties": [
{"key1": "value1"}, {"key2": "value2"},...
]
}
What I would want is this:
{ ....
"properties": {
"key1": "value1",
"key2": "value2",
...
}
}
The serializer for this is easy enough:
public class PropertyListJSONSerializer extends StdSerializer<List<Property>> {
//....
#Override
public void serialize(List<Property> value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
for(Property p: value){
gen.writeStringField(p.getName(), p.getValue());
}
gen.writeEndObject();
}
}
Now when I try to register this serializer inside a #Configuration file:
#Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
SimpleModule module = new SimpleModule();
module.addSerializer(List<Property>.class, new PropertyListJSONSerializer());
mapper.registerModule(module);
return mapper;
}
this doesn't work, because List<Property>.class cannot be used for addSerializer since it's a template class. Is there any other way to add this serializer or something that does something similar?
I do not want to add a custom serializer for WrapperModel since this class is autogenerated and fields can be added and removed. This should be possible without modifying the application code (if I had a custom serializer you would need to add/remove the fields from the serializer also(?)). Or is there a way to just use the Standard serializer for the class and just manually handle this one List<> field.
The model classes are generated by the Spring Boot openapi code generator, so there is a very limited set of JSON annotations I can put on top of the model fields (if there's an annotation way, please dont hesitate to post as I can check in the openapi sourcecode if that particular annotation is supported). But I would rather go with either a custom serializer for List<Property> if that is at all possible or writing a serializer for WrapperModel that uses StdSerializer for everything and only handle the List property myself.
MixIn
In that case we need to use MixIn feature. Create interface like below:
interface WrapperModelMixIn {
#JsonSerialize(using = PropertyListJSONSerializer.class)
List<Property> getProperties();
}
and register it like below:
ObjectMapper mapper = new ObjectMapper();
mapper.addMixInAnnotations(WrapperModel.class, WrapperModelMixIn.class);
Older proposal
You need to use Jackson types which allow to register serialiser for generic type. Your serialiser after change could look like below:
class PropertyListJSONSerializer extends StdSerializer<List<Property>> {
public PropertyListJSONSerializer(JavaType type) {
super(type);
}
#Override
public void serialize(List<Property> value, JsonGenerator gen, SerializerProvider provider)
throws IOException {
gen.writeStartObject();
for (Property p : value) {
gen.writeStringField(p.getName(), p.getValue());
}
gen.writeEndObject();
}
}
And you can register it as below:
ObjectMapper mapper = new ObjectMapper();
CollectionType propertiesListType = mapper.getTypeFactory().constructCollectionType(List.class, Property.class);
SimpleModule module = new SimpleModule();
module.addSerializer(new PropertyListJSONSerializer(propertiesListType));
mapper.registerModule(module);
I have class with #JsonIgnore-d field:
public class MyClass {
...
#JsonIgnore
private SomeType myfield;
...
// getters & setters
}
Is it possible to configure ObjectWriter so that it includes myfield during serialization even though being ingored?
Rationale: MyClass is serialized in many places and only in single specific one I want to have myfield.
It is possible to configure ObjectMapper to disable a JsonIgnore function. Following are some possible solution you can try with:
1.
Disable JsonIgnore function for a particular annotated field.
You can create a custom JsonIgnore annotation and a custom JacksonAnnotationIntrospector to remove the annotation from mapper context.
Following are the ideas:
Annotate #MyJsonIgnore to the fields that should be ignored while serialization:
#JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class MyClass {
#MyJsonIgnore
private SomeType myField;
}
#MyJsonIgnore is a simple custom annotation that wrap #JsonIgnore:
#Retention(RetentionPolicy.RUNTIME)
#JacksonAnnotationsInside
#JsonIgnore
public #interface MyJsonIgnore {
}
A custom JacksonAnnotationIntrospector is implemented to remove #MyJsonIgnore from mapper context:
public class DisablingMyJsonIgnoreIntrospector extends JacksonAnnotationIntrospector {
#Override
public boolean isAnnotationBundle(final Annotation ann) {
if (ann.annotationType().equals(MyJsonIgnore.class)) {
return false;
} else {
return super.isAnnotationBundle(ann);
}
}
After that, you can set the introspector on a ObjectMapper during configuration:
ObjectMapper mapper = new ObjectMapper();
mapper.setAnnotationIntrospector(new DisablingMyJsonIgnoreIntrospector());
It results that the fields annotated with #MyJsonIgnore can be marshaled properly.
2.
Disable JsonIgnore function for the mapper
Your can create a custom JacksonAnnotationIntrospector and override hasIgnoreMarker method to always return false:
public static class DisablingJsonIgnoreIntrospector extends JacksonAnnotationIntrospector {
#Override
public boolean hasIgnoreMarker(final AnnotatedMember m) {
return false;
}
}
hasIgnoreMarker is to check whether there is annotation to ignore json property. Return false will disable the JsonIngore function.
3.
Disable all annotations and specify what kinds of properties are auto-detected for a given ObjectMapper:
final ObjectMapper mapper = new ObjectMapper();
mapper.disable(MapperFeature.USE_ANNOTATIONS);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
This simply disable all annotations.
Hope this can help.
One more option is to use the AnnotationIntrospector.nopInstance() if you want to avoid all Jackson's annotations in your pojo including #JsonIgnore e.g.
JsonMapper.builder().annotationIntrospector(AnnotationIntrospector.nopInstance()).build()...
or
new ObjectMapper().setAnnotationIntrospector(AnnotationIntrospector.nopInstance())...
I created a mixin for my class. The mixin itself works fine, it's not the issue that most people have where they mix faterxml/codehaus annotations.
I tested it in a unit test, creating the ObjectMapper "by hand" while using the addMixIn method - it worked just fine.
I want to use that mixin to modify the response jsons returned from my REST endpoints.
I've tried to customize Spring Boot's ObjectMapper in many different ways:
BuilderCustomizer:
#Bean
public Jackson2ObjectMapperBuilderCustomizer addMixin(){
return new Jackson2ObjectMapperBuilderCustomizer() {
#Override
public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
jacksonObjectMapperBuilder.mixIn(MyClass.class, MyClassMixin.class);
}
};
}
Builder:
#Bean
Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
return new Jackson2ObjectMapperBuilder().mixIn(MyClass.class, MyClassMixin.class);
}
Converter:
#Bean
public MappingJackson2HttpMessageConverter configureJackson(){
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(MyClass.class, MyClassMixin.class);
converter.setObjectMapper(mapper);
return converter;
}
ObjectMapper:
#Autowired(required = true)
public void configureJackon(ObjectMapper jsonMapper){
jsonMapper.addMixIn(MyClass.class, MyClassMixin.class);
}
None of these work.
As of Spring Boot 2.7, there is built-in support for mixins.
Adding the following annotation:
#JsonMixin(MyClass::class)
class MyClassMixin{
will register mixin in the auto-configured ObjectMapper.
This might depend on Spring Boot version but as per Customize the Jackson ObjectMapper defining a new Jackson2ObjectMapperBuilderCustomizer bean is sufficient
The context’s Jackson2ObjectMapperBuilder can be customized by one or more Jackson2ObjectMapperBuilderCustomizer beans. Such customizer beans can be ordered (Boot’s own customizer has an order of 0), letting additional customization be applied both before and after Boot’s customization.
I had tried the above and it did not work for me either. While debugging, I noticed that the ObjectMapper inside the message converter was null.
Referring to the post get registered message converters, I ended up replacing the default message converter for Jackson, allowing me to customize the object mapper to my needs:
#SpringBootApplication
#RestController
public class MixinTest {
public static void main(String[] args) {
SpringApplication.run(MixinTest.class, args);
}
static class Person {
private String title;
private String name;
private String nullField;
private LocalDate date;
Person(String title, String name) {
this.title = title;
this.name = name;
this.date = LocalDate.now();
}
// getters here...
}
// this will exclude nullField
#JsonInclude(JsonInclude.Include.NON_NULL)
interface PersonMixin {
#JsonProperty("fullName")
String getName();
}
#Bean
public Jackson2ObjectMapperBuilderCustomizer personCustomizer() {
return jacksonObjectMapperBuilder ->
jacksonObjectMapperBuilder.mixIn(Person.class, PersonMixin.class);
}
#Bean
public MappingJackson2HttpMessageConverter myMessageConverter(
// provided by Spring
RequestMappingHandlerAdapter reqAdapter,
Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
ObjectMapper mapper = jacksonObjectMapperBuilder
.featuresToEnable(SerializationFeature.INDENT_OUTPUT)
.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.modulesToInstall(new JavaTimeModule())
.build();
// **replace previous MappingJackson converter**
List<HttpMessageConverter<?>> converters =
reqAdapter.getMessageConverters();
converters.removeIf(httpMessageConverter ->
httpMessageConverter.getClass()
.equals(MappingJackson2HttpMessageConverter.class));
MappingJackson2HttpMessageConverter jackson = new
MappingJackson2HttpMessageConverter(mapper);
converters.add(jackson);
reqAdapter.setMessageConverters(converters);
return jackson;
}
#GetMapping("/test")
public Person get() {
return new Person("Mr", "Joe Bloggs");
}
}
Which outputs the following in the browser after hitting http://localhost:8080/test:
{
"title" : "Mr",
"date" : "2019-09-03",
"fullName" : "Joe Bloggs"
}
This way, I should be able to add as many customizers as necessary. I'm sure there's a better way to do this. It seems hacky to replace internals like this...
I want to enable the following jackson mapper feature:
MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES
According to https://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html:
Could be configured in application.properties as follows:
spring.jackson.mapper.accept_case_insensitive_properties=true
But:
#RestController
public class MyServlet {
#RequestMapping("/test")
public void test(#Valid TestReq req) {
}
}
public class TestReq {
#NotBlank
private String name;
}
Usage:
localhost:8080/test?name=test //works
localhost:8080/test?Name=test //fails with 'name may not be blank'
So, the case insensitive property is not taken into account. But why?
By the way: even using Jackson2ObjectMapperBuilderCustomizer explicit does not work:
#Bean
public Jackson2ObjectMapperBuilderCustomizer initJackson() {
Jackson2ObjectMapperBuilderCustomizer c = new Jackson2ObjectMapperBuilderCustomizer() {
#Override
public void customize(Jackson2ObjectMapperBuilder builder) {
builder.featuresToEnable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
}
};
return c;
}
spring-boot-1.5.3.RELEASE
According to spring doc you can customize it.
I fix this problem by set my application.yml like this(spring 2.0):
spring:
jackson:
mapper:
ACCEPT_CASE_INSENSITIVE_PROPERTIES: true
Did you tried change your setting accept_case_insensitive_properties to UPPER CASE?
Also you can keep output to Upper Case by setting like this:
jackson:
mapper:
ACCEPT_CASE_INSENSITIVE_PROPERTIES: true
property-naming-strategy: com.fasterxml.jackson.databind.PropertyNamingStrategy.PascalCaseStrategy
Note that PascalCaseStrategy was deprecated now, but still working.
Simple answer: it is not possible.
The Jackson2ObjectMapperBuilderCustomizer affects the JSON POST requests only. It has no effect on the get query binding.
I am working on a Spring Boot project. I just have annotation configuration. I want to include dozer to transform Entities to DTO and DTO to Entities. I see in the dozer website, they explain i have to add the following configuration in spring xml configuration file. Since i have not xml file but annotation configuration Java class, i don't know how to translate this into Java Configuration class.
<bean id="org.dozer.Mapper" class="org.dozer.DozerBeanMapper">
<property name="mappingFiles">
<list>
<value>dozer-global-configuration.xml</value>
<value>dozer-bean-mappings.xml</value>
<value>more-dozer-bean-mappings.xml</value>
</list>
</property>
</bean>
If someone could you give me an example it'll be very useful. Thanks
I think something like this should work:
#Configuration
public class YourConfiguration {
#Bean(name = "org.dozer.Mapper")
public DozerBeanMapper dozerBean() {
List<String> mappingFiles = Arrays.asList(
"dozer-global-configuration.xml",
"dozer-bean-mappings.xml",
"more-dozer-bean-mappings.xml"
);
DozerBeanMapper dozerBean = new DozerBeanMapper();
dozerBean.setMappingFiles(mappingFiles);
return dozerBean;
}
...
}
If you are using DozerBeanMapperFactoryBean instead of DozerBeanMapper you may use something like this.
#Configuration
public class MappingConfiguration {
#Bean
public DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean(#Value("classpath*:mappings/*mappings.xml") Resource[] resources) throws Exception {
final DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean = new DozerBeanMapperFactoryBean();
// Other configurations
dozerBeanMapperFactoryBean.setMappingFiles(resources);
return dozerBeanMapperFactoryBean;
}
}
This way you can import your mappings automatically. Than simple inject your Mapper and use.
#Autowired
private Mapper mapper;
Update with Dozer 5.5.1
In dozer 5.5.1, DozerBeanMapperFactoryBean is removed. So if you want to go with an updated version you need do something like below,
#Bean
public Mapper mapper(#Value(value = "classpath*:mappings/*mappings.xml") Resource[] resourceArray) throws IOException {
List<String> mappingFileUrlList = new ArrayList<>();
for (Resource resource : resourceArray) {
mappingFileUrlList.add(String.valueOf(resource.getURL()));
}
DozerBeanMapper dozerBeanMapper = new DozerBeanMapper();
dozerBeanMapper.setMappingFiles(mappingFileUrlList);
return dozerBeanMapper;
}
Now inject mapper as told above
#Autowired
private Mapper mapper;
And use like below example,
mapper.map(source_object, destination.class);
eg.
mapper.map(admin, UserDTO.class);
Just in case someone wants to avoid xml dozer file. You can use a builder directly in java. For me it's the way to go in a annotation Spring context.
See more information at mapping api dozer
#Bean
public DozerBeanMapper mapper() throws Exception {
DozerBeanMapper mapper = new DozerBeanMapper();
mapper.addMapping(objectMappingBuilder);
return mapper;
}
BeanMappingBuilder objectMappingBuilder = new BeanMappingBuilder() {
#Override
protected void configure() {
mapping(Bean1.class, Bean2.class)
.fields("id", "id").fields("name", "name");
}
};
In my case it was more efficient (At least the first time). Didn't do any benchmark or anything.