io.springagora.store.rest.RestConfig.java Source code

Java tutorial

Introduction

Here is the source code for io.springagora.store.rest.RestConfig.java

Source

/*
 * Spring Agora 
 * https://github.com/ThiagoUriel/spring-agora-web
 * Copyright (C) 2014 - Thiago Uriel M. Garcia
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package io.springagora.store.rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Properly configures the REST context provided by the Dispatcher Servlet.
 * 
 * @author Thiago Uriel M. Garcia
 */
@Configuration
@EnableWebMvc
@PropertySource("classpath:application.properties")
@ComponentScan({ "io.springagora.store.rest.controllers" })
public class RestConfig extends WebMvcConfigurerAdapter {
    /** Configuration property: Pretty print for JSON results? */
    @Value("${rest.prettyPrintJSON}")
    Boolean prettyPrintJSON;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(Include.NON_NULL);

        MappingJackson2HttpMessageConverter jackson = new MappingJackson2HttpMessageConverter();
        jackson.setObjectMapper(mapper);
        jackson.setPrettyPrint(prettyPrintJSON);
        converters.add(jackson);
    }

    /**
     * Bean responsible for placeholder proper configuration, using the properties
     * file defined in as the Property Source (via {@code PropertySource} annotation.
     * 
     * @return
     *      The {@code PropertySourcesPlaceholderConfigurer} required to correctly
     *      change variables for properties defined in the configuration file. 
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}