Skip to content

Commit 72edb72

Browse files
author
eugenp
committed
further marshalling work
1 parent 71c8dff commit 72edb72

File tree

4 files changed

+96
-21
lines changed

4 files changed

+96
-21
lines changed

spring-rest/pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,23 @@
1616
<groupId>org.springframework</groupId>
1717
<artifactId>spring-web</artifactId>
1818
<version>${org.springframework.version}</version>
19+
<exclusions>
20+
<exclusion>
21+
<artifactId>commons-logging</artifactId>
22+
<groupId>commons-logging</groupId>
23+
</exclusion>
24+
</exclusions>
1925
</dependency>
2026
<dependency>
2127
<groupId>org.springframework</groupId>
2228
<artifactId>spring-webmvc</artifactId>
2329
<version>${org.springframework.version}</version>
2430
</dependency>
31+
<dependency>
32+
<groupId>org.springframework</groupId>
33+
<artifactId>spring-oxm</artifactId>
34+
<version>${org.springframework.version}</version>
35+
</dependency>
2536

2637
<!-- web -->
2738

@@ -67,6 +78,31 @@
6778
<version>3.2.1</version>
6879
</dependency>
6980

81+
<!-- logging -->
82+
83+
<dependency>
84+
<groupId>org.slf4j</groupId>
85+
<artifactId>slf4j-api</artifactId>
86+
<version>${org.slf4j.version}</version>
87+
</dependency>
88+
<dependency>
89+
<groupId>ch.qos.logback</groupId>
90+
<artifactId>logback-classic</artifactId>
91+
<version>${logback.version}</version>
92+
<!-- <scope>runtime</scope> -->
93+
</dependency>
94+
<dependency>
95+
<groupId>org.slf4j</groupId>
96+
<artifactId>jcl-over-slf4j</artifactId>
97+
<version>${org.slf4j.version}</version>
98+
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
99+
</dependency>
100+
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
101+
<groupId>org.slf4j</groupId>
102+
<artifactId>log4j-over-slf4j</artifactId>
103+
<version>${org.slf4j.version}</version>
104+
</dependency>
105+
70106
<!-- test scoped -->
71107

72108
<dependency>
Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
1-
package org.baeldung.config;
2-
3-
import org.springframework.context.annotation.ComponentScan;
4-
import org.springframework.context.annotation.Configuration;
5-
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
6-
7-
@EnableWebMvc
8-
@Configuration
9-
@ComponentScan({ "org.baeldung.web" })
10-
public class WebConfig {
11-
12-
public WebConfig() {
13-
super();
14-
}
15-
16-
}
1+
package org.baeldung.config;
2+
3+
import java.util.List;
4+
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.http.converter.HttpMessageConverter;
8+
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
9+
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
10+
import org.springframework.oxm.xstream.XStreamMarshaller;
11+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
12+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
13+
14+
@Configuration
15+
@EnableWebMvc
16+
@ComponentScan({ "org.baeldung.web" })
17+
public class WebConfig extends WebMvcConfigurerAdapter {
18+
19+
public WebConfig() {
20+
super();
21+
}
22+
23+
// API
24+
25+
@Override
26+
public void configureMessageConverters(final List<HttpMessageConverter<?>> messageConverters) {
27+
messageConverters.add(marshallingHttpMessageConverter());
28+
messageConverters.add(new MappingJackson2HttpMessageConverter());
29+
30+
super.configureMessageConverters(messageConverters);
31+
}
32+
33+
// UTIL
34+
35+
private final MarshallingHttpMessageConverter marshallingHttpMessageConverter() {
36+
final MarshallingHttpMessageConverter marshallingHttpMessageConverter = new MarshallingHttpMessageConverter();
37+
final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
38+
marshallingHttpMessageConverter.setMarshaller(xstreamMarshaller);
39+
marshallingHttpMessageConverter.setUnmarshaller(xstreamMarshaller);
40+
41+
return marshallingHttpMessageConverter;
42+
}
43+
44+
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
55

66
import org.baeldung.web.dto.Foo;
7+
import org.springframework.http.HttpStatus;
78
import org.springframework.stereotype.Controller;
89
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.RequestBody;
911
import org.springframework.web.bind.annotation.RequestMapping;
1012
import org.springframework.web.bind.annotation.RequestMethod;
1113
import org.springframework.web.bind.annotation.ResponseBody;
14+
import org.springframework.web.bind.annotation.ResponseStatus;
1215

1316
@Controller
1417
public class FooController {
@@ -17,12 +20,20 @@ public FooController() {
1720
super();
1821
}
1922

20-
// API
23+
// API - read
2124

2225
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}")
2326
public @ResponseBody
2427
Foo findById(@PathVariable final long id) {
2528
return new Foo(Long.parseLong(randomNumeric(2)), randomAlphabetic(4));
2629
}
2730

31+
// API - write
32+
33+
@RequestMapping(method = RequestMethod.PUT, value = "/foos/{id}")
34+
@ResponseStatus(HttpStatus.OK)
35+
public void updateFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) {
36+
System.out.println(foo);
37+
}
38+
2839
}

spring-security-rest-full/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
<dependency>
197197
<groupId>com.jayway.restassured</groupId>
198198
<artifactId>rest-assured</artifactId>
199-
<version>2.1.0</version>
199+
<version>2.2.0</version>
200200
<scope>test</scope>
201201
</dependency>
202202

@@ -275,12 +275,12 @@
275275

276276
<!-- persistence -->
277277
<hibernate.version>4.3.0.Final</hibernate.version>
278-
<mysql-connector-java.version>5.1.27</mysql-connector-java.version>
278+
<mysql-connector-java.version>5.1.28</mysql-connector-java.version>
279279
<spring-data-jpa.version>1.4.3.RELEASE</spring-data-jpa.version>
280280

281281
<!-- marshalling -->
282282

283-
<jackson.version>2.2.3</jackson.version>
283+
<jackson.version>2.3.0</jackson.version>
284284

285285
<!-- logging -->
286286
<org.slf4j.version>1.7.5</org.slf4j.version>
@@ -291,7 +291,7 @@
291291

292292
<!-- util -->
293293
<guava.version>15.0</guava.version>
294-
<commons-lang3.version>3.1</commons-lang3.version>
294+
<commons-lang3.version>3.2.1</commons-lang3.version>
295295

296296
<!-- testing -->
297297
<org.hamcrest.version>1.3</org.hamcrest.version>

0 commit comments

Comments
 (0)