question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spring HATEOAS Swagger Example Value Problem (question)

See original GitHub issue

I’m using springfox-swagger2 and springfox-swagger-ui version 2.7.0 (2.8.0 fails with an ‘auth’ problem and 2.9.0 isn’t available to me) with a Spring Boot 2.0 REST application using HATEOAS. I have a question about the output I’m seeing for the example value for one of my API endpoints. The swagger example doesn’t match the output generated by the application. My endpoint is defined in a RestController like this:

@RequestMapping(value = "/documents", method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public Resources<Resource<Document>> getAvailableDocuments)  {
 ...
}

(the Resources and Resource are standard Spring HATEOAS classes, Document is our code which just contains a bunch of document related attributes)

The output I get when I execute the method is this:

{
  "_embedded": {
    "documentList": [
      {
        "name": "...",
        "format": "...",
        "created_at": "...",       
        ...
        "_links": {
          "self": {
            "href": "..."
          }
        }
      },
      ... more documents like above ...
    ]
  },
  "_links": {
    "self": {
      "href": "..."
    }
  }
}

(note the “_embedded” and “_links” elements as well as the structure of the “_links” elements. However the example generated by the Swagger UI is this:

{
  "content": [
    {
      "created_at": "2018-05-05T13:54:27.743Z",
      "description": "string",
      ...
      "links": [
        {
          "deprecation": "string",
          "href": "string",
          "hreflang": "string",
          "media": "string",
          "rel": "string",
          "templated": true,
          "title": "string",
          "type": "string"
        }
      ],
      "name": "string",
      "system_id": "string",
      "updated_at": "2018-05-05T13:54:27.743Z"
    }
  ],
  "links": [
    {
      "deprecation": "string",
      "href": "string",
      "hreflang": "string",
      "media": "string",
      "rel": "string",
      "templated": true,
      "title": "string",
      "type": "string"
    }
  ]
}

The Swagger example doesn’t contain the “_embedded” object, “_links” are called “links” and are structured differently than what is really produced. Are there some configuration changes I can make to get the example to look more like the real result? In the Links section, are there ways to tell Swagger that the application is not going to populate certain fields (e.g. media, templated, etc.)

Any help our pointers would be appreciated. Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
vlfloriancommented, Feb 27, 2019

@dilipkrish I upgraded to 2.9.2 but am still encountering this issue. Is there anything I am missing or should it work out of the box?

Example endpoint: @GetMapping(“/{modelId}/results”) public Resources<ResultsResource> findAllBlaBla(@PathVariable(“modelId”) String modelId)

Thanks!

0reactions
panky031commented, Aug 27, 2020

How we can disable the HAL format by default. I am using the below property in properties file but didn’t affect. spring.hateoas.use-hal-as-default-json-media-type=false

Read more comments on GitHub >

github_iconTop Results From Across the Web

Swagger and RESTful spring hateoas resources
This leads me to questions like this than: - I have resource Pool . What if I have a newly installed system, without...
Read more >
Spring Boot and Swagger: Documenting RESTful Services
In this tutorial, we take a look at how to add documentation to your REST API with exposed Swagger UI annotations and Spring...
Read more >
springdoc-openapi v2.0.0
The support for Spring Hateoas is available using the dependency ... The value of the swagger-annotation description will be used.
Read more >
The problems with Swagger - Novatec Consulting GmbH
Problems and disadvantages of using Swagger as a REST API documentation tool.
Read more >
HATEOAS Driven REST APIs
Each REST framework provides its way of creating the HATEOAS links using framework capabilities. For example, in Spring Boot HATEOAS tutorial, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found