Returned json unexpected, has “links” spelled as “_links” and structure different, in Spring hateoas

前端 未结 6 1779
情书的邮戳
情书的邮戳 2021-01-02 02:56

As the title says, I have a resource object Product extending ResourceSupport. However, the responses I receive have the property \"_links\" instea

6条回答
  •  旧巷少年郎
    2021-01-02 03:08

    I've recently had the opposite issue, expected the HAL form (i.e. _links) but always got links, although using @EnableAutoConfiguration.

    The solution to my issue might help here as well. Due to copying&pasting from an example I found, I had two mediatypes in my RestController's @RequestMappingannotation

    @RequestMapping(path="/example", produces = {MediaType.APPLICATION_JSON_VALUE, "application/hal+json"})
    

    With @EnableAutoConfiguration Spring HATEOAS registers an ObjectMapper for application/hal+json only and in my application there was another one responsible for MediaType.APPLICATION_JSON_VALUE which won the selection and rendered standard JSON.

    So my solution to only set produces="application/hal+json" so that the Spring HATEOAS object mapper is choosen. In your case you should try to keep MediaType.APPLICATION_JSON_VALUE

提交回复
热议问题