使用spring RestTemplate消耗json + hal _embedded资源的问题

时间:2017-10-06 21:31:38

标签: java rest spring-boot jackson resttemplate

我有一个简单的用例,我想使用一个用json + hal表示的资源集合。

我使用spring RestTemplate并将其配置为使用Jackson2HalModule。 当我调试我的代码时,我发现Response对象确实包含准确的元数据(例如页数和资源数)和响应头,但没有内容或链接。我在最后一天看了很多互联网上的文章和指南,我觉得我的自定义休息模板应该根据我的发现为我的用例工作。

如果有人能够对此有所了解,我将永远感激不尽。

我的服务代码如下:

@Service
public class EventServiceImpl extends BaseService implements EventService {

private static final String knownEntity = "59d786d642572853721728f6";

private static  String SERVICE_URL = "http://EVENTS-SERVER";

private static String EVENTS_PATH = "/events";


@Autowired
@LoadBalanced
protected RestTemplate restTemplate;

@Override
public ResponseEntity<PagedResources<Event>> fetchEventsList() {
    // acceptable media type
    List<MediaType> acceptableMediaTypes = Arrays.asList(HAL_JSON);

    // header
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(acceptableMediaTypes);

    HttpEntity<String> entity = new HttpEntity<String>(null, headers);

    ResponseEntity<PagedResources<Event>> response = getRestTemplateWithHalMessageConverter()
            .exchange(SERVICE_URL + EVENTS_PATH, HttpMethod.GET, entity, new ParameterizedTypeReference<PagedResources<Event>>(){});
    return response;
}

public RestTemplate getRestTemplateWithHalMessageConverter() {
    List<HttpMessageConverter<?>> existingConverters = restTemplate.getMessageConverters();
    List<HttpMessageConverter<?>> newConverters = new ArrayList<>();
    newConverters.add(getHalMessageConverter());
    newConverters.addAll(existingConverters);
    restTemplate.setMessageConverters(newConverters);
    return restTemplate;
}

private HttpMessageConverter getHalMessageConverter() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new Jackson2HalModule());
    MappingJackson2HttpMessageConverter halConverter = new TypeConstrainedMappingJackson2HttpMessageConverter(ResourceSupport.class);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    halConverter.setSupportedMediaTypes(Arrays.asList(HAL_JSON));
    halConverter.setObjectMapper(objectMapper);
    return halConverter;
}

我的简单模型是:

public class Event {

private String name;
private String location;
private int capacity;

public String getName() {
    return name;
}

public String getLocation() {
    return location;
}

public int getCapacity() {
    return capacity;
}

}

为了完整性,这里是我试图消耗的hal + json形状的一小部分:

{
"_embedded": {
    "events": [
        {
            "name": null,
            "location": null,
            "capacity": 0,
            "currentState": "CANCELLED",
            "_links": {
                "self": {
                    "href": "http://192.168.1.6:2221/events/59d786d642572853721728f6"
                },
                "event": {
                    "href": "http://192.168.1.6:2221/events/59d786d642572853721728f6"
                },
                "reinstate": {
                    "href": "http://192.168.1.6:2221/events/59d786d642572853721728f6/reinstate"
                },
                "reschedule": {
                    "href": "http://192.168.1.6:2221/events/59d786d642572853721728f6/reschedule"
                }
            }
        },
        {
            "name": null,
            "location": null,
            "capacity": 0,
            "currentState": "ADVERTISED",
            "_links": {
                "self": {
                    "href": "http://192.168.1.6:2221/events/59d7f14342572812ceca7fc6"
                },
                "event": {
                    "href": "http://192.168.1.6:2221/events/59d7f14342572812ceca7fc6"
                },
                "cancel": {
                    "href": "http://192.168.1.6:2221/events/59d7f14342572812ceca7fc6/cancel"
                },
                "reschedule": {
                    "href": "http://192.168.1.6:2221/events/59d7f14342572812ceca7fc6/reschedule"
                }
            }
        },
        {
            "name": null,
            "location": null,
            "capacity": 0,
            "currentState": "ADVERTISED",
            "_links": {
                "self": {
                    "href": "http://192.168.1.6:2221/events/59d7f14742572812ceca7fc7"
                },
                "event": {
                    "href": "http://192.168.1.6:2221/events/59d7f14742572812ceca7fc7"
                },
                "cancel": {
                    "href": "http://192.168.1.6:2221/events/59d7f14742572812ceca7fc7/cancel"
                },
                "reschedule": {
                    "href": "http://192.168.1.6:2221/events/59d7f14742572812ceca7fc7/reschedule"
                }
            }
        },
        {
            "name": null,
            "location": null,
            "capacity": 0,
            "currentState": "ADVERTISED",
            "_links": {
                "self": {
                    "href": "http://192.168.1.6:2221/events/59d7f14c42572812ceca7fc8"
                },
                "event": {
                    "href": "http://192.168.1.6:2221/events/59d7f14c42572812ceca7fc8"
                },
                "cancel": {
                    "href": "http://192.168.1.6:2221/events/59d7f14c42572812ceca7fc8/cancel"
                },
                "reschedule": {
                    "href": "http://192.168.1.6:2221/events/59d7f14c42572812ceca7fc8/reschedule"
                }
            }
        }
    ]
},
"_links": {
    "self": {
        "href": "http://192.168.1.6:2221/events{?page,size,sort}",
        "templated": true
    },
    "profile": {
        "href": "http://192.168.1.6:2221/profile/events"
    }
},
"page": {
    "size": 20,
    "totalElements": 4,
    "totalPages": 1,
    "number": 0
}

}

编辑:我可以毫无问题地消费个人事件。

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,最终我使用了org.springframework.hateoas.Resources

在下面的示例中,该对象位于org.springframework.hateoas:spring-hateoas:jar:0.25.2.RELEASE中 是从org.springframework.boot:spring-boot-starter-data-rest:jar:2.1.7.RELEASE中提取出来的,因此假设您声明了spring-boot-starter-data-rest依赖关系,它很有可能已经在您的类路径中了。

这是一个简单的示例(使用您的信息):

RestTemplate restTemplate = new RestTemplate();
Resources<Event> resourceEvents = restTemplate.getForObject("http://192.168.1.6:2221/events", Resources.class);
List<Event> events = new ArrayList<>(resourceEvents.getContent());

可能有些陷阱使事情变得不那么直接,但是希望这可以为解决您的问题提供一个开始。