ch.thp.proto.spring.time.web.config.SpringMVCRestConfig.java Source code

Java tutorial

Introduction

Here is the source code for ch.thp.proto.spring.time.web.config.SpringMVCRestConfig.java

Source

/*
 * Copyright 2014 thpeng.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ch.thp.proto.spring.time.web.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JSR310Module;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Configures the MVC and the static Resources. Configuration for Spring-MVC contains
 * the 310 Jackson Module (Support for java 8 time api)
 * @author thpeng
 */
@Configuration
@EnableWebMvc
public class SpringMVCRestConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui/**").addResourceLocations("/swagger-ui/");
        registry.addResourceHandler("/app/**").addResourceLocations("/app/");
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(converter());
        converters.add(new StringHttpMessageConverter()); //do not forget this one. else all plain-text responses will not work 
    }

    @Bean
    MappingJackson2HttpMessageConverter converter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(register310TimeModule());
        return converter;
    }

    private ObjectMapper register310TimeModule() {
        ObjectMapper obj = new ObjectMapper();
        obj.registerModule(new JSR310Module());
        obj.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        return obj;
    }
}