|
1 | 1 | package com.baeldung.spring.configuration; |
2 | 2 |
|
3 | 3 | import com.baeldung.spring.controller.rss.ArticleRssFeedViewResolver; |
| 4 | +import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
| 5 | +import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator; |
4 | 6 | import org.springframework.context.annotation.Bean; |
5 | 7 | import org.springframework.context.annotation.ComponentScan; |
6 | 8 | import org.springframework.context.annotation.Configuration; |
| 9 | +import org.springframework.http.converter.HttpMessageConverter; |
| 10 | +import org.springframework.http.converter.feed.RssChannelHttpMessageConverter; |
| 11 | +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; |
| 12 | +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; |
| 13 | +import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter; |
| 14 | +import org.springframework.web.accept.ContentNegotiationManager; |
7 | 15 | import org.springframework.web.multipart.MultipartResolver; |
8 | 16 | import org.springframework.web.multipart.commons.CommonsMultipartResolver; |
9 | 17 | import org.springframework.web.servlet.ViewResolver; |
10 | 18 | import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; |
11 | 19 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
12 | | -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| 20 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; |
13 | 21 | import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; |
14 | 22 | import org.springframework.web.servlet.view.InternalResourceViewResolver; |
15 | | -import org.springframework.web.accept.ContentNegotiationManager; |
16 | 23 |
|
17 | | -import java.util.List; |
18 | 24 | import java.util.ArrayList; |
| 25 | +import java.util.List; |
19 | 26 |
|
20 | 27 | @Configuration |
21 | 28 | @EnableWebMvc |
22 | 29 | @ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" }) |
23 | | -class ApplicationConfiguration implements WebMvcConfigurer { |
| 30 | +class ApplicationConfiguration extends WebMvcConfigurerAdapter { |
24 | 31 |
|
25 | 32 | @Override |
26 | 33 | public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { |
@@ -49,4 +56,19 @@ public MultipartResolver multipartResolver() { |
49 | 56 | multipartResolver.setMaxUploadSize(5242880); |
50 | 57 | return multipartResolver; |
51 | 58 | } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { |
| 62 | + Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml(); |
| 63 | + builder.indentOutput(true); |
| 64 | + |
| 65 | + XmlMapper xmlMapper = builder.createXmlMapper(true).build(); |
| 66 | + xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true); |
| 67 | + |
| 68 | + converters.add(new RssChannelHttpMessageConverter()); |
| 69 | + converters.add(new MappingJackson2HttpMessageConverter()); |
| 70 | + converters.add(new MappingJackson2XmlHttpMessageConverter(xmlMapper)); |
| 71 | + |
| 72 | + super.configureMessageConverters(converters); |
| 73 | + } |
52 | 74 | } |
0 commit comments