com.alon.spring.crud.api.configuration.WebConfigurer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of crud-api-base Show documentation
Show all versions of crud-api-base Show documentation
Fornece implementação básica e expansível para criação API's CRUD com Spring Boot e Spring Data JPA.
package com.alon.spring.crud.api.configuration;
import com.alon.spring.crud.core.properties.Properties;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.servlet.Filter;
import java.util.List;
@EnableWebMvc
@Configuration
public class WebConfigurer implements WebMvcConfigurer {
@Autowired
public Properties properties;
@Override
public void configureMessageConverters(List> converters) {
Hibernate5Module hibernate5Module = new Hibernate5Module();
if (!properties.serialization.forceLazyLoading)
hibernate5Module.disable(Hibernate5Module.Feature.FORCE_LAZY_LOADING);
if (properties.serialization.serializeIdentifierForLazyNotLoadedObjects)
hibernate5Module.enable(Hibernate5Module.Feature.SERIALIZE_IDENTIFIER_FOR_LAZY_NOT_LOADED_OBJECTS);
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.modulesToInstall(hibernate5Module, new JavaTimeModule());
if (!properties.serialization.writeDatesAsTimestamps)
builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
if (!properties.serialization.includeNullValues)
builder.serializationInclusion(Include.NON_NULL);
converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
}
@Bean
public Filter shallowEtagHeaderFilter() {
return new ShallowEtagHeaderFilter();
}
}