Move and Update Rest and Http Message Converter Article:

moved code (mostly commented out)
changed readme file
added spring boot exampel simply adding bean, for xml too
This commit is contained in:
Ger Roza
2019-02-19 16:23:44 -03:00
parent 6c237aaf3a
commit 578d35d25d
10 changed files with 101 additions and 43 deletions
@@ -8,6 +8,9 @@ import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("Foo")
@Entity
public class Foo implements Serializable {
@@ -1,10 +1,49 @@
package com.baeldung.spring;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
import org.springframework.oxm.xstream.XStreamMarshaller;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
// If we want to enable xml configurations for message-converter:
// @ImportResource("classpath:WEB-INF/api-servlet.xml")
public class WebConfig implements WebMvcConfigurer {
// @Override
// public void configureMessageConverters(final List<HttpMessageConverter<?>> messageConverters) {
// messageConverters.add(new MappingJackson2HttpMessageConverter());
// messageConverters.add(createXmlHttpMessageConverter());
// }
//
// private HttpMessageConverter<Object> createXmlHttpMessageConverter() {
// final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
//
// final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
// xstreamMarshaller.setAutodetectAnnotations(true);
// xmlConverter.setMarshaller(xstreamMarshaller);
// xmlConverter.setUnmarshaller(xstreamMarshaller);
//
// return xmlConverter;
// }
// Another possibility is to create a bean which will be automatically added to the Spring Boot Autoconfigurations
// @Bean
// public HttpMessageConverter<Object> createXmlHttpMessageConverter() {
// final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
//
// final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
// xstreamMarshaller.setAutodetectAnnotations(true);
// xmlConverter.setMarshaller(xstreamMarshaller);
// xmlConverter.setUnmarshaller(xstreamMarshaller);
//
// return xmlConverter;
// }
}
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- We have to exclude Spring Boot's WebMvcAutoConfiguration if we want the xml-configured message-converters to work properly -->
<!-- <mvc:annotation-driven> -->
<!-- <mvc:message-converters -->
<!-- register-defaults="true"> -->
<!-- <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> -->
<!-- <property name="marshaller" ref="xstreamMarshaller" /> -->
<!-- <property name="unmarshaller" ref="xstreamMarshaller" /> -->
<!-- </bean> -->
<!-- <bean -->
<!-- class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> -->
<!-- </mvc:message-converters> -->
<!-- </mvc:annotation-driven> -->
<!-- <bean id="xstreamMarshaller" -->
<!-- class="org.springframework.oxm.xstream.XStreamMarshaller"> -->
<!-- <property name="autodetectAnnotations" value="true" /> -->
<!-- </bean> -->
<!-- -->
<!-- Also, we can JUST add a HttpMessageConverter Bean to the existing Spring Boot Autoconfiguration: -->
<!-- <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> -->
<!-- <property name="marshaller" ref="xstreamMarshaller" /> -->
<!-- <property name="unmarshaller" ref="xstreamMarshaller" /> -->
<!-- </bean> -->
<!-- <bean id="xstreamMarshaller" -->
<!-- class="org.springframework.oxm.xstream.XStreamMarshaller"> -->
<!-- <property name="autodetectAnnotations" value="true" /> -->
<!-- </bean> -->
</beans>