Skip to content

Commit 13d63ad

Browse files
authored
Merge pull request eugenp#6380 from rozagerardo/geroza/BAEL-11405-Update-and-move-Http-Message-Converters-article-code
[BAEL-11405] Move and Update Rest and Http Message Converter Article
2 parents 7f34c5e + 578d35d commit 13d63ad

10 files changed

Lines changed: 101 additions & 43 deletions

File tree

‎spring-boot-rest/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ Module for the articles that are part of the Spring REST E-book:
66
4. [Build a REST API with Spring and Java Config](http://www.baeldung.com/building-a-restful-web-service-with-spring-and-java-based-configuration)
77
5. [HATEOAS for a Spring REST Service](http://www.baeldung.com/rest-api-discoverability-with-spring)
88
6. [REST API Discoverability and HATEOAS](http://www.baeldung.com/restful-web-service-discoverability)
9+
7. [Http Message Converters with the Spring Framework](http://www.baeldung.com/spring-httpmessageconverter-rest)

‎spring-boot-rest/pom.xml‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,26 @@
2525
<groupId>com.fasterxml.jackson.dataformat</groupId>
2626
<artifactId>jackson-dataformat-xml</artifactId>
2727
</dependency>
28-
28+
<!-- We'll need to comment out the jackson-dataformat-xml dependency if we want to use XStream: -->
29+
<dependency>
30+
<groupId>org.springframework</groupId>
31+
<artifactId>spring-oxm</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.thoughtworks.xstream</groupId>
35+
<artifactId>xstream</artifactId>
36+
<version>${xstream.version}</version>
37+
</dependency>
38+
2939
<dependency>
3040
<groupId>com.h2database</groupId>
3141
<artifactId>h2</artifactId>
3242
</dependency>
33-
<dependency>
43+
<dependency>
3444
<groupId>org.springframework.boot</groupId>
3545
<artifactId>spring-boot-starter-data-jpa</artifactId>
36-
</dependency>
37-
46+
</dependency>
47+
3848
<!-- util -->
3949

4050
<dependency>
@@ -67,5 +77,6 @@
6777
<properties>
6878
<start-class>com.baeldung.SpringBootRestApplication</start-class>
6979
<guava.version>27.0.1-jre</guava.version>
80+
<xstream.version>1.4.11.1</xstream.version>
7081
</properties>
7182
</project>

‎spring-boot-rest/src/main/java/com/baeldung/persistence/model/Foo.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import javax.persistence.GenerationType;
99
import javax.persistence.Id;
1010

11+
import com.thoughtworks.xstream.annotations.XStreamAlias;
12+
13+
@XStreamAlias("Foo")
1114
@Entity
1215
public class Foo implements Serializable {
1316

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,49 @@
11
package com.baeldung.spring;
22

3+
import java.util.List;
4+
5+
import org.springframework.context.annotation.Bean;
36
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.ImportResource;
8+
import org.springframework.http.converter.HttpMessageConverter;
9+
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
10+
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
11+
import org.springframework.oxm.xstream.XStreamMarshaller;
412
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
513

6-
714
@Configuration
15+
// If we want to enable xml configurations for message-converter:
16+
// @ImportResource("classpath:WEB-INF/api-servlet.xml")
817
public class WebConfig implements WebMvcConfigurer {
918

19+
// @Override
20+
// public void configureMessageConverters(final List<HttpMessageConverter<?>> messageConverters) {
21+
// messageConverters.add(new MappingJackson2HttpMessageConverter());
22+
// messageConverters.add(createXmlHttpMessageConverter());
23+
// }
24+
//
25+
// private HttpMessageConverter<Object> createXmlHttpMessageConverter() {
26+
// final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
27+
//
28+
// final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
29+
// xstreamMarshaller.setAutodetectAnnotations(true);
30+
// xmlConverter.setMarshaller(xstreamMarshaller);
31+
// xmlConverter.setUnmarshaller(xstreamMarshaller);
32+
//
33+
// return xmlConverter;
34+
// }
35+
36+
// Another possibility is to create a bean which will be automatically added to the Spring Boot Autoconfigurations
37+
// @Bean
38+
// public HttpMessageConverter<Object> createXmlHttpMessageConverter() {
39+
// final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
40+
//
41+
// final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
42+
// xstreamMarshaller.setAutodetectAnnotations(true);
43+
// xmlConverter.setMarshaller(xstreamMarshaller);
44+
// xmlConverter.setUnmarshaller(xstreamMarshaller);
45+
//
46+
// return xmlConverter;
47+
// }
48+
1049
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:mvc="http://www.springframework.org/schema/mvc"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:context="http://www.springframework.org/schema/context"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
7+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
8+
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
9+
10+
<!-- We have to exclude Spring Boot's WebMvcAutoConfiguration if we want the xml-configured message-converters to work properly -->
11+
<!-- <mvc:annotation-driven> -->
12+
<!-- <mvc:message-converters -->
13+
<!-- register-defaults="true"> -->
14+
15+
<!-- <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> -->
16+
<!-- <property name="marshaller" ref="xstreamMarshaller" /> -->
17+
<!-- <property name="unmarshaller" ref="xstreamMarshaller" /> -->
18+
<!-- </bean> -->
19+
<!-- <bean -->
20+
<!-- class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> -->
21+
<!-- </mvc:message-converters> -->
22+
<!-- </mvc:annotation-driven> -->
23+
24+
<!-- <bean id="xstreamMarshaller" -->
25+
<!-- class="org.springframework.oxm.xstream.XStreamMarshaller"> -->
26+
<!-- <property name="autodetectAnnotations" value="true" /> -->
27+
<!-- </bean> -->
28+
<!-- -->
29+
30+
<!-- Also, we can JUST add a HttpMessageConverter Bean to the existing Spring Boot Autoconfiguration: -->
31+
<!-- <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> -->
32+
<!-- <property name="marshaller" ref="xstreamMarshaller" /> -->
33+
<!-- <property name="unmarshaller" ref="xstreamMarshaller" /> -->
34+
<!-- </bean> -->
35+
<!-- <bean id="xstreamMarshaller" -->
36+
<!-- class="org.springframework.oxm.xstream.XStreamMarshaller"> -->
37+
<!-- <property name="autodetectAnnotations" value="true" /> -->
38+
<!-- </bean> -->
39+
</beans>
40+
41+

‎spring-rest/README.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
55

66
### Relevant Articles:
77
- [Spring @RequestMapping](http://www.baeldung.com/spring-requestmapping)
8-
- [Http Message Converters with the Spring Framework](http://www.baeldung.com/spring-httpmessageconverter-rest)
98
- [Returning Custom Status Codes from Spring Controllers](http://www.baeldung.com/spring-mvc-controller-custom-http-status-code)
109
- [A Guide to OkHttp](http://www.baeldung.com/guide-to-okhttp)
1110
- [Binary Data Formats in a Spring REST API](http://www.baeldung.com/spring-rest-api-with-binary-data-formats)

‎spring-rest/pom.xml‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@
5252
<groupId>org.springframework</groupId>
5353
<artifactId>spring-webmvc</artifactId>
5454
</dependency>
55-
<dependency>
56-
<groupId>org.springframework</groupId>
57-
<artifactId>spring-oxm</artifactId>
58-
</dependency>
5955
<dependency>
6056
<groupId>commons-fileupload</groupId>
6157
<artifactId>commons-fileupload</artifactId>
@@ -86,12 +82,6 @@
8682
<artifactId>jackson-dataformat-xml</artifactId>
8783
</dependency>
8884

89-
<dependency>
90-
<groupId>com.thoughtworks.xstream</groupId>
91-
<artifactId>xstream</artifactId>
92-
<version>${xstream.version}</version>
93-
</dependency>
94-
9585
<!-- util -->
9686
<dependency>
9787
<groupId>com.google.guava</groupId>
@@ -281,7 +271,6 @@
281271
<protobuf-java-format.version>1.4</protobuf-java-format.version>
282272
<protobuf-java.version>3.1.0</protobuf-java.version>
283273
<commons-lang3.version>3.5</commons-lang3.version>
284-
<xstream.version>1.4.9</xstream.version>
285274

286275
<!-- util -->
287276
<guava.version>20.0</guava.version>

‎spring-rest/src/main/java/com/baeldung/sampleapp/config/WebConfig.java‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import org.springframework.http.converter.HttpMessageConverter;
99
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
1010
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
11-
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
12-
import org.springframework.oxm.xstream.XStreamMarshaller;
1311
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
1412
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
1513

@@ -33,22 +31,12 @@ public void configureMessageConverters(final List<HttpMessageConverter<?>> messa
3331
.dateFormat(new SimpleDateFormat("dd-MM-yyyy hh:mm"));
3432
messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build()));
3533
// messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build()));
36-
37-
// messageConverters.add(createXmlHttpMessageConverter());
34+
3835
// messageConverters.add(new MappingJackson2HttpMessageConverter());
3936
4037
// messageConverters.add(new ProtobufHttpMessageConverter());
4138
4239
}
4340
44-
private HttpMessageConverter<Object> createXmlHttpMessageConverter() {
45-
final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
46-
47-
final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
48-
xmlConverter.setMarshaller(xstreamMarshaller);
49-
xmlConverter.setUnmarshaller(xstreamMarshaller);
50-
51-
return xmlConverter;
52-
}
5341
*/
5442
}

‎spring-rest/src/main/java/com/baeldung/sampleapp/web/dto/Foo.java‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.baeldung.sampleapp.web.dto;
22

3-
import com.thoughtworks.xstream.annotations.XStreamAlias;
4-
5-
@XStreamAlias("Foo")
63
public class Foo {
74
private long id;
85
private String name;

‎spring-rest/src/main/webapp/WEB-INF/api-servlet.xml‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,11 @@
1010

1111
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" >
1212
<mvc:message-converters register-defaults="true">
13-
<!--
14-
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
15-
16-
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
17-
<property name="marshaller" ref="xstreamMarshaller" />
18-
<property name="unmarshaller" ref="xstreamMarshaller" />
19-
</bean>
20-
-->
2113
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
2214
<!-- <bean class="org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter"/> -->
2315
</mvc:message-converters>
2416
</mvc:annotation-driven>
2517

26-
<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller" />
27-
<!-- -->
2818

2919
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
3020
<bean class="org.springframework.web.servlet.view.XmlViewResolver">

0 commit comments

Comments
 (0)