Swagger support when wrap handler response with generic type
See original GitHub issueHi, 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:
- Created 5 years ago
- Reactions:7
- Comments:11 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This would be practical for use with Spring’s ResponseBodyAdvice.
use spring ResponseBodyAdvice and set @RestControllerAdvice(basePackages = {“org.my.pkg”})