All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alon.spring.crud.api.configuration.WebConfigurer Maven / Gradle / Ivy

Go to download

Fornece implementação básica e expansível para criação API's CRUD com Spring Boot e Spring Data JPA.

There is a newer version: 1.0.9
Show newest version
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();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy