"_embedded": {
"employees": [
{
"id": "1",
"name": "Some Employee",
"_links": {
"self": {
"href": "http://localhost:8080/employees/1"
}
}
},
{
"id": "2",
"name": "Some Employee",
"_links": {
"self": {
"href": "http://localhost:8080/employees/2"
}
}
},
{
"id": "3",
"name": "Some Employee",
"_links": {
"self": {
"href": "http://localhost:8080/employees/3"
}
}
}
]
},
{
"_links": {
"self": {
"href": "http://localhost:8080/employees{?page,size,sort}"
},
"search": {
"href": "http://localhost:8080/employees/search"
}
},
"page": {
"size": 20,
"totalElements": 3,
"totalPages": 1,
"number": 0
}
}
我正在阅读第三方Rest服务,该服务返回具有上述结构或类似内容的json内容(内容类型为application / json)。
目前,我为一个通用对象创建了一个自定义json转换器,该对象具有链接,页面和嵌入对象的泛型类型的自定义对象。所以,我以某种通用的方式阅读它。但是,我想通过hateoas对象来做。我尝试了几种方法,但其中任何一种都在起作用。
在关注其他问题之后,我尝试了许多其他内容,例如配置RestTamplate
@Bean
public OAuth2RestTemplate getOAuth2Rest() {
[...]
OAuth2RestTemplate template = new OAuth2RestTemplate(resourceDetails, clientContext);
List<HttpMessageConverter<?>> existingConverters = template.getMessageConverters();
List<HttpMessageConverter<?>> newConverters = new ArrayList<>();
newConverters.add(getHalMessageConverter());
newConverters.addAll(existingConverters);
template.setMessageConverters(newConverters);
return template;
}
private HttpMessageConverter getHalMessageConverter() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.registerModule(new Jackson2HalModule());
MappingJackson2HttpMessageConverter halConverter =
new TypeConstrainedMappingJackson2HttpMessageConverter(ResourceSupport.class);
halConverter.setSupportedMediaTypes(Arrays.asList(HAL_JSON));
halConverter.setObjectMapper(objectMapper);
return halConverter;
}
然后
ResponseEntity<Resources<MyDto>> entity = restTemplate.exchange(
myUri,
HttpMethod.GET, null,
new ParameterizedTypeReference<Resources<MyDto>>() {}, Collections.emptyMap());
有类似Consuming Spring Hateoas Pageable之类的问题,但它们正在消耗json + hal内容。我认为这是我的问题,因为解串器使用的是json转换器而不是hal转换器。
我试过
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
没有工作并干扰我的REST配置。
我试过这个blog