Java tutorial
/* * Copyright 2013 the original author or authors. * * 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.nebhale.cyclinglibrary.web; import java.nio.charset.Charset; import java.util.Arrays; import java.util.List; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; 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.WebMvcConfigurerAdapter; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.ObjectMapper; /** * Configuration of web components */ @Configuration @ComponentScan @EnableWebMvc public class WebConfiguration extends WebMvcConfigurerAdapter { @Bean ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.getFactory().configure(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM, false); return objectMapper; } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(createJsonConverter()); converters.add(createXmlConverter()); } private HttpMessageConverter<?> createJsonConverter() { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); converter.setObjectMapper(objectMapper()); converter.setSupportedMediaTypes(Arrays.asList( // ApplicationMediaType.TYPE, // ApplicationMediaType.COLLECTION, // ApplicationMediaType.ITEM, // ApplicationMediaType.POINTS_JSON, // ApplicationMediaType.TASK)); return converter; } private HttpMessageConverter<?> createXmlConverter() { StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8")); converter.setSupportedMediaTypes(Arrays.asList(ApplicationMediaType.POINTS_XML)); return converter; } }