fix spring-rest configuration

This commit is contained in:
DOHA 2016-09-12 12:55:46 +02:00
parent e6d11e531c
commit f2c52d6cfc
6 changed files with 27 additions and 37 deletions

View File

@ -15,10 +15,7 @@
<dependencies>
<!-- Spring Boot Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
@ -75,6 +72,11 @@
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
@ -146,11 +148,19 @@
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>${rest-assured.version}</version>
</dependency>
<!-- -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
@ -231,10 +241,6 @@
<hibernate.version>4.3.11.Final</hibernate.version>
<mysql-connector-java.version>5.1.39</mysql-connector-java.version>
<!-- marshalling -->
<jackson.version>2.7.2</jackson.version>
<!-- various -->
<hibernate-validator.version>5.2.2.Final</hibernate-validator.version>

View File

@ -15,6 +15,11 @@ import org.springframework.oxm.xstream.XStreamMarshaller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/*
* Please note that main web configuration is in src/main/webapp/WEB-INF/api-servlet.xml
*
*/
@Configuration
@EnableWebMvc
@ComponentScan({ "org.baeldung.web" })

View File

@ -35,7 +35,6 @@ public class FooController {
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Foo updateFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) {
System.out.println(foo);
return foo;
}

View File

@ -8,7 +8,12 @@
<context:component-scan base-package="org.baeldung.web" />
<mvc:annotation-driven />
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.baeldung.config.converter.KryoHttpMessageConverter"/>
<bean class="org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
<bean class="org.springframework.web.servlet.view.XmlViewResolver">

View File

@ -8,7 +8,7 @@
<display-name>Spring MVC Application</display-name>
<!-- Spring root -->
<context-param>
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext

View File

@ -3,9 +3,7 @@ package org.baeldung.web.test;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.baeldung.config.converter.KryoHttpMessageConverter;
import org.baeldung.web.dto.Foo;
@ -17,11 +15,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
import org.springframework.oxm.xstream.XStreamMarshaller;
import org.springframework.web.client.RestTemplate;
/**
@ -50,7 +44,6 @@ public class SpringHttpMessageConvertersIntegrationTestsCase {
final String URI = BASE_URI + "foos/{id}";
final RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(getMessageConverters());
final HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
@ -67,7 +60,6 @@ public class SpringHttpMessageConvertersIntegrationTestsCase {
final String URI = BASE_URI + "foos/{id}";
final RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(getMessageConverters());
final HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
@ -83,7 +75,6 @@ public class SpringHttpMessageConvertersIntegrationTestsCase {
public void givenConsumingXml_whenWritingTheFoo_thenCorrect() {
final String URI = BASE_URI + "foos/{id}";
final RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(getMessageConverters());
final Foo resource = new Foo(4, "jason");
final HttpHeaders headers = new HttpHeaders();
@ -129,20 +120,4 @@ public class SpringHttpMessageConvertersIntegrationTestsCase {
assertThat(resource, notNullValue());
}
// UTIL
private List<HttpMessageConverter<?>> getMessageConverters() {
final List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
xmlConverter.setMarshaller(xstreamMarshaller);
xmlConverter.setUnmarshaller(xstreamMarshaller);
converters.add(xmlConverter);
converters.add(new MappingJackson2HttpMessageConverter());
return converters;
}
}