Hypermedia links in Swagger UI using Springfox

2019-07-27 11:44发布

问题:

I'm building a RESTful API, using spring-hateoas library to implement hypermedia. The response of my endpoints is application/hal+json (My DTO extends ResourceSupport).

I'm using Swagger to document the endpoints, and using Springfox to autogenerate the swagger-ui.

The problem is, that the generated documentation is not correct.

As a request for a POST endpoint, the UI is setting the _links section from my DTO as part of the payload, so this is what I see I should send as a parameter for my POST endpoint:

{
  "category": "string",
  "creator": "string",
  "description": "string",
  "id": 0,
  "links": [
    {
      "href": "string",
      "rel": "string",
      "templated": true
    }
  ],
  "period": 0,
  "recipient": 0,
  "title": "string",
  "type": 0
}

So my question is: Is there any way of making springfox aware of application/hal+json as the Parameter content type? In case there isn't, is there any way of hiding the _links section from the request?

回答1:

So, the only way I found of hiding the links section was to override the setLinks method in my DTO extending ResourceSupport, and set hidden to true as follows:

@ApiModelProperty(hidden = true)
public void setLinks(final Link... links) {
    super.add(links);
}