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.

Swagger support when wrap handler response with generic type

See original GitHub issue

Hi, I am learning to build a RESTful service using spring boot with springfox-swagger2 2.6.1

I have a handler which return User

@GetMapping(value = "{userId}")
    public User get(@PathVariable int userId) {}

And it will wrap the handler response to a generic type called Response by using ResponseBodyAdvice Response.java

class Response<T> {

    private HttpStatus status;
    private T data;
}

ResponseBodyAdvice.java::beforeBodyWrite

        if (!serverHttpRequest.getURI().getPath().startsWith("/v1/")
                || body instanceof Response) {
            return body;
        }

        return ResponseFactory.success(body);

My question is, how can I tell swagger to render Response type, I read some question and I think I should add an alternateTypeRules like this:

               .alternateTypeRules(AlternateTypeRules.newRule(
                        // replace any T
                        typeResolver.resolve(WildcardType.class),
                        // with Response<T> of T
                        typeResolver.resolve(Response.class, WildcardType.class)
                ))

But it didn’t work. Can somebody help me out, you can find my code here: https://github.com/tienducitt/restful-service

Thanks for your help!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
phlppnhllngrcommented, Apr 5, 2019

This would be practical for use with Spring’s ResponseBodyAdvice.

1reaction
liangjiezhucommented, Dec 18, 2019

use spring ResponseBodyAdvice and set @RestControllerAdvice(basePackages = {“org.my.pkg”})

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to specify a generic type class for Swagger API response
I am using springdoc-openapi-ui (1.5.0), which depends upon Swagger JARs, I understand. Since the common response class takes a generic T , the...
Read more >
Describing Responses - Swagger
An API operation can return a file, such as an image or PDF. In this case, define the response schema with type: file...
Read more >
Swagger: Specify Two Responses with the Same ... - Baeldung
In this article, we'll write an API specification that allows returning two different objects for the same response code.
Read more >
Swagger with APIs that use Java Generics - Google Groups
For example, one of my "list" API returns a response of type PagedResponse<T>. In the Swagger UI, this shows up as follows under...
Read more >
Type-Safe HTTP Servers in Go via Generics - Polymatheia
Handler interface. With a lot of request-response endpoints, you'd like to abstract away the common logic, which tends to be input parsing, ...
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