Skip to content

Commit f2c52d6

Browse files
committed
fix spring-rest configuration
1 parent e6d11e5 commit f2c52d6

6 files changed

Lines changed: 27 additions & 37 deletions

File tree

‎spring-rest/pom.xml‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
<dependencies>
1616

1717
<!-- Spring Boot Dependencies -->
18-
<dependency>
19-
<groupId>org.springframework.boot</groupId>
20-
<artifactId>spring-boot-starter-security</artifactId>
21-
</dependency>
18+
2219
<dependency>
2320
<groupId>org.springframework.boot</groupId>
2421
<artifactId>spring-boot-starter-thymeleaf</artifactId>
@@ -74,6 +71,11 @@
7471
<groupId>com.fasterxml.jackson.core</groupId>
7572
<artifactId>jackson-databind</artifactId>
7673
</dependency>
74+
75+
<dependency>
76+
<groupId>com.fasterxml.jackson.dataformat</groupId>
77+
<artifactId>jackson-dataformat-xml</artifactId>
78+
</dependency>
7779

7880
<dependency>
7981
<groupId>com.thoughtworks.xstream</groupId>
@@ -146,11 +148,19 @@
146148
<artifactId>spring-test</artifactId>
147149
</dependency>
148150

151+
<dependency>
152+
<groupId>com.jayway.restassured</groupId>
153+
<artifactId>rest-assured</artifactId>
154+
<version>${rest-assured.version}</version>
155+
</dependency>
156+
157+
<!-- -->
149158
<dependency>
150159
<groupId>com.google.protobuf</groupId>
151160
<artifactId>protobuf-java</artifactId>
152161
<version>2.6.1</version>
153162
</dependency>
163+
154164
<dependency>
155165
<groupId>com.esotericsoftware</groupId>
156166
<artifactId>kryo</artifactId>
@@ -231,10 +241,6 @@
231241
<hibernate.version>4.3.11.Final</hibernate.version>
232242
<mysql-connector-java.version>5.1.39</mysql-connector-java.version>
233243

234-
<!-- marshalling -->
235-
236-
<jackson.version>2.7.2</jackson.version>
237-
238244
<!-- various -->
239245
<hibernate-validator.version>5.2.2.Final</hibernate-validator.version>
240246

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
1616
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
1717

18+
/*
19+
* Please note that main web configuration is in src/main/webapp/WEB-INF/api-servlet.xml
20+
*
21+
*/
22+
1823
@Configuration
1924
@EnableWebMvc
2025
@ComponentScan({ "org.baeldung.web" })

‎spring-rest/src/main/java/org/baeldung/web/controller/FooController.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public Foo findById(@PathVariable final long id) {
3535
@ResponseStatus(HttpStatus.OK)
3636
@ResponseBody
3737
public Foo updateFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) {
38-
System.out.println(foo);
3938
return foo;
4039
}
4140

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88

99
<context:component-scan base-package="org.baeldung.web" />
1010

11-
<mvc:annotation-driven />
11+
<mvc:annotation-driven>
12+
<mvc:message-converters register-defaults="true">
13+
<bean class="org.baeldung.config.converter.KryoHttpMessageConverter"/>
14+
<bean class="org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter"/>
15+
</mvc:message-converters>
16+
</mvc:annotation-driven>
1217

1318
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
1419
<bean class="org.springframework.web.servlet.view.XmlViewResolver">

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<display-name>Spring MVC Application</display-name>
99

1010
<!-- Spring root -->
11-
<context-param>
11+
<context-param>
1212
<param-name>contextClass</param-name>
1313
<param-value>
1414
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
@@ -18,7 +18,7 @@
1818
<param-name>contextConfigLocation</param-name>
1919
<param-value>org.baeldung.config</param-value>
2020
</context-param>
21-
21+
2222
<listener>
2323
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
2424
</listener>

‎spring-rest/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersIntegrationTestsCase.java‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import static org.hamcrest.Matchers.notNullValue;
44
import static org.junit.Assert.assertThat;
55

6-
import java.util.ArrayList;
76
import java.util.Arrays;
8-
import java.util.List;
97

108
import org.baeldung.config.converter.KryoHttpMessageConverter;
119
import org.baeldung.web.dto.Foo;
@@ -17,11 +15,7 @@
1715
import org.springframework.http.HttpMethod;
1816
import org.springframework.http.MediaType;
1917
import org.springframework.http.ResponseEntity;
20-
import org.springframework.http.converter.HttpMessageConverter;
21-
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
2218
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
23-
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
24-
import org.springframework.oxm.xstream.XStreamMarshaller;
2519
import org.springframework.web.client.RestTemplate;
2620

2721
/**
@@ -50,7 +44,6 @@ public void givenConsumingXml_whenReadingTheFoo_thenCorrect() {
5044
final String URI = BASE_URI + "foos/{id}";
5145

5246
final RestTemplate restTemplate = new RestTemplate();
53-
restTemplate.setMessageConverters(getMessageConverters());
5447

5548
final HttpHeaders headers = new HttpHeaders();
5649
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
@@ -67,7 +60,6 @@ public void givenConsumingJson_whenReadingTheFoo_thenCorrect() {
6760
final String URI = BASE_URI + "foos/{id}";
6861

6962
final RestTemplate restTemplate = new RestTemplate();
70-
restTemplate.setMessageConverters(getMessageConverters());
7163

7264
final HttpHeaders headers = new HttpHeaders();
7365
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
@@ -83,7 +75,6 @@ public void givenConsumingJson_whenReadingTheFoo_thenCorrect() {
8375
public void givenConsumingXml_whenWritingTheFoo_thenCorrect() {
8476
final String URI = BASE_URI + "foos/{id}";
8577
final RestTemplate restTemplate = new RestTemplate();
86-
restTemplate.setMessageConverters(getMessageConverters());
8778

8879
final Foo resource = new Foo(4, "jason");
8980
final HttpHeaders headers = new HttpHeaders();
@@ -129,20 +120,4 @@ public void givenConsumingKryo_whenReadingTheFoo_thenCorrect() {
129120
assertThat(resource, notNullValue());
130121
}
131122

132-
// UTIL
133-
134-
private List<HttpMessageConverter<?>> getMessageConverters() {
135-
final List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
136-
137-
final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
138-
final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
139-
xmlConverter.setMarshaller(xstreamMarshaller);
140-
xmlConverter.setUnmarshaller(xstreamMarshaller);
141-
142-
converters.add(xmlConverter);
143-
converters.add(new MappingJackson2HttpMessageConverter());
144-
145-
return converters;
146-
}
147-
148123
}

0 commit comments

Comments
 (0)