com.gnoht.tlrl.config.ApplicationConfig.java Source code

Java tutorial

Introduction

Here is the source code for com.gnoht.tlrl.config.ApplicationConfig.java

Source

/**
 * Copyright 2015-2016 ikumen@gnoht.com
 *
 * 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 com.gnoht.tlrl.config;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.core.env.Environment;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

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

/**
 * Main application level configuration.
 *  
 * @author ikumen@gnoht.com
 */
@Configuration
public class ApplicationConfig {

    @Resource
    private Environment env;

    /**
     * Creates a custom {@link HttpMessageConverter} with the following attributes:
     * 
     * - suppressed null fields during serialization
     *   
     * @return a custom {@link HttpMessageConverter}.
     */
    @Bean(name = "jacksonHttpMessageConverter")
    public MappingJackson2HttpMessageConverter jacksonHttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.getObjectMapper().setSerializationInclusion(Include.NON_NULL);
        converter.getObjectMapper().configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
        return converter;
    }

    /**
     * Returns a {@link MessageSourceAccessor} instance, that's pre-configured to
     * use a {@link LocaleContextHolder} for locale lookups. Note: Working with 
     * MessageSourceAccessor is more convenient then working directly with {@link MessageSource}.
     * 
     * @param messageSource being wrapped
     * @return instance of {@link MessageSourceAccessor}
     */
    @Autowired
    @Bean
    public MessageSourceAccessor messageSourceAccessor(MessageSource messageSource) {
        return new MessageSourceAccessor(messageSource);
    }
}