Spring Boot 3 serverside forwarding
The best articles and links to interesting posts for technical team leaders building sophisticated websites, applications and mobile apps. Think about: software architecture, hardware architecture, design, programming, frameworks, scalability, performance, quality assurance, security, resolving issues, fixing bugs and Android.
Geplaatst door
Techie
op
10:00 AM
0
reacties
Labels: forwarding, jakarta servlet, java, kotlin, modelandview, oas, server-side, serverside, spring boot 3, spring mvc, swagger
Add to:
Del.icio.us
DZone
Reddit
Digg
When you have multiple classes with the same name in your classpath, SpringDoc with Swagger API annotations potentially picks the wrong class with the same name when generating the Swagger UI documentation.
And you specified your endpoint like this, where you want to have it use org.example.BookDto:
@Operation(summary = "Get a list of books for a given shop")
@ApiResponses(
value = [
ApiResponse(
responseCode = "200",
description = "A list of books",
content = [Content(mediaType = "application/json",
array = ArraySchema(schema = Schema(implementation = BookDto::class)))]
)
]
)
@GetMapping("/books/{shopId}")
fun getBooksByShopId(
@Parameter(description = "Shop to search for")
@PathVariable shopId: Long
): List<BookDto> {
return bookService.getBooksByShopId(shopId)
.map { BooksMapper.mapDto(it) }
}
Then whatever it finds first on the classpath will be visible in https://localhost:8080/swagger-ui.html. Not necessarily the class you meant, it might pick org.example.domain.BookDto.
Setup:
Several solutions exist:
Specify in your application.yml:
springdoc:
use-fqn: true
Disadvantage: the Swagger documentation in the swagger-ui.html endpoint has then the fully specified package classpath + classname in it. Looks ugly.
Setting it in the @Bean configuration:
import io.swagger.v3.core.jackson.TypeNameResolver
@Bean
fun openAPI(): OpenAPI? {
TypeNameResolver.std.setUseFqn(true)
return OpenAPI()
.addServersItem(Server().url("/"))
.info(
Info().title("Books Microservice")
.description("The Books Microservice")
.version("v1")
)
.externalDocs(
ExternalDocumentation()
.description("Books Microservice documentation")
.url("https://github.com/myproject/README.md")
)
}
Disadvantage: also in this solution the Swagger documentation in the swagger-ui.html endpoint has then the fully specified package classpath + classname in it. Looks ugly.
You can create your own ModelConverters, but that is much more work. Examples here: https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Extensions#extending-core-resolver and https://groups.google.com/g/swagger-swaggersocket/c/kKM546QXGY0
Make sure for each endpoint you specify the response class with full class package path:
@Operation(summary = "Get a list of books for a given shop")
@ApiResponses(
value = [
ApiResponse(
responseCode = "200",
description = "A list of books",
content = [Content(mediaType = "application/json",
array = ArraySchema(schema = Schema(implementation = org.example.BookDto::class)))]
)
]
)
@GetMapping("/books/{shopId}")
fun getBooksByShopId(
@Parameter(description = "Shop to search for")
@PathVariable shopId: Long
): List<BookDto> {
return bookService.getBooksByShopId(shopId)
.map { BooksMapper.mapDto(it) }
}
See the bold Schema implementation value for what changed.
Geplaatst door
Techie
op
12:46 PM
0
reacties
Labels: class, fqn, fully qualified name, incorrect class, kotlin, openapi, openapi 3, spring boot, springdoc, swagger, swagger-ui.html, wrong class
Add to:
Del.icio.us
DZone
Reddit
Digg