I\'m trying to use the TestRestTemplate in my Spring Boot Application\'s Integration-Test, to make a request to a Spring Data REST Repository.
The response in the br
I did a similar test, but I'm not using spring-boot. Probably is the configuration of your RestTemplate. By the way, have you tried to use the Traverson implementation rather than RestTemplate? It's seems more simple to work with HATEOAS. See bellow my test class with both approaches.
package org.wisecoding.api;
import org.junit.Test;
import org.wisecoding.api.domain.User;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.client.Traverson;
import org.springframework.hateoas.hal.Jackson2HalModule;
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.web.client.RestTemplate;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import static org.springframework.hateoas.client.Hop.rel;
public class UserApiTest {
@Test
public void testGetUsersRestTemplate() {
final ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.registerModule(new Jackson2HalModule());
final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(MediaType.parseMediaTypes(MediaTypes.HAL_JSON_VALUE));
converter.setObjectMapper(mapper);
final List> list = new ArrayList>();
list.add(converter);
final RestTemplate restTemplate = new RestTemplate(list);
final String authorsUrl = "http://localhost:8080/apiv1/users";
final ResponseEntity> responseEntity = restTemplate.exchange(authorsUrl, HttpMethod.GET, null, new ParameterizedTypeReference>() {});
final PagedResources resources = responseEntity.getBody();
final List users = new ArrayList(resources.getContent());
}
@Test
public void testGetUsersTraverson() throws Exception {
final Traverson traverson = new Traverson(new URI("http://localhost:8080/apiv1"), MediaTypes.HAL_JSON);
final ParameterizedTypeReference> resourceParameterizedTypeReference = new ParameterizedTypeReference>() {};
final PagedResources resources = traverson.follow(rel("users")).toObject(resourceParameterizedTypeReference);
final List users = new ArrayList(resources.getContent());
}
}
And also, the pom.xml in case your dependencies does not match:
4.0.0
war
org.wisecoding
0.1-SNAPSHOT
user-demo-data-rest
4.2.6.RELEASE
1.7.1
UTF-8
org.apache.maven.plugins
maven-resources-plugin
UTF-8
org.eclipse.jetty
jetty-maven-plugin
9.0.4.v20130625
com.jayway.jsonpath
json-path
2.2.0
test
com.fasterxml.jackson.datatype
jackson-datatype-json-org
2.7.5
com.fasterxml.jackson.core
jackson-databind
2.6.7
org.springframework.data
spring-data-rest-webmvc
2.5.6.RELEASE
org.aspectj
aspectjrt
org.springframework.data
spring-data-jpa
1.10.1.RELEASE
org.springframework
spring-context
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-test
${spring.version}
test
javax.servlet
javax.servlet-api
3.1.0
org.slf4j
slf4j-api
${slf4j.version}
ch.qos.logback
logback-classic
1.0.13
runtime
junit
junit
4.11
test
org.hibernate
hibernate-entitymanager
4.2.3.Final
org.springframework
spring-jdbc
${spring.version}
hsqldb
hsqldb
1.8.0.10
central
http://central.maven.org/maven2/
false