使用RestTemplate测试Spring Data Rest端点在SpringBoot 2.0.0.BUILD-SNAPSHOT中不起作用

时间:2017-05-16 03:27:32

标签: java spring-boot spring-data-jpa spring-data-rest

我使用Spring Data REST将Spring Data JPA存储库公开为REST端点。

@Entity
@Table(name = "USERS")
public class User implements Serializable
{
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;
    private String email;
    private String password;
}

public interface UserRepository extends JpaRepository<User, Integer>
{
}

使用SpringBoot 1.5.3.RELEASE时我可以使用 RestTemplate 测试端点http://localhost:8080/users,如下所示:

@Test
public void testGetUsers()
{
    RestTemplate restTemplate = restTemplate();

    ResponseEntity<PagedResources<User>> responseEntity = 
            restTemplate.exchange(
                            "http://localhost:8080/users",
                            HttpMethod.GET, 
                            null, 
                            new ParameterizedTypeReference<PagedResources<User>>()
                            {}, 
                            Collections.emptyMap()
                            );
    PagedResources<User> userResource = responseEntity.getBody();
    Collection<User> users = userResource.getContent();
    assertNotNull(users);
}

protected RestTemplate restTemplate() 
{
      ObjectMapper mapper = new ObjectMapper();
      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      mapper.registerModule(new Jackson2HalModule());

      MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
      converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
      converter.setObjectMapper(mapper);
      return new RestTemplate(Collections.<HttpMessageConverter<?>> singletonList(converter));
}

但是当我升级到SpringBoot 2.0.0.BUILD-SNAPSHOT时,我收到以下异常。

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [org.springframework.hateoas.PagedResources<com.myapp.entities.User>] and content type [application/hal+json;charset=UTF-8]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:117)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:946)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:930)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:684)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:650)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:598)
    at com.myapp.demo.SpringbootDataRestDemoApplicationTest.testGetUsers(SpringbootDataRestDemoApplicationTest.java:53)

有什么指针可以解决这个问题吗?

PS: 我尝试使用@Autowired TestRestTemplate,它在内部使用已配置的ObjectMapper但问题相同,在1.5.3.RELEASE下工作正常但在2.0.0.BUILD-SNAPSHOT时失败。

@RunWith(SpringRunner.class)
@SpringBootTest(classes=DemoApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DemoApplicationTests
{

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void testGetUsers()
    {
        ResponseEntity<PagedResources<User>> responseEntity =
                restTemplate.exchange("/users",
                                        HttpMethod.GET, 
                                        null, 
                                        new ParameterizedTypeReference<PagedResources<User>>() {}
                                        );
        assertTrue(responseEntity.getStatusCode() == HttpStatus.OK);
        PagedResources<User> userResource = responseEntity.getBody();
        Collection<User> users = userResource.getContent();
        assertNotNull(users);
        assertEquals(3, users.size());
    }   
}

使用剩余的数据((链接,页面)正在ResponseBody中正确填充,但内容变为空列表。

0 个答案:

没有答案