Reputation: 766
I want to disable default 404 handler in SpringBoot like:
@ExceptionHandler(value = [NoHandlerFoundException::class])
fun handleNotFoundException(e:NoHandlerFoundException):ResponseEntity<ApiError>{
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.body(ApiError("Resource not found"))
}
and be able to server swagger UI with api documentation on /swagger-ui.html#
.
However, disabling default 404 handler requires setting
spring.resources.add-mappings=false
in application-properties
, which also disables serving swagger UI. Is there a way to combine these two?
I am using springboot:2.1.7
with springox:swagger:2.9.2
and springfox:swagger-ui:2.9.2
Upvotes: 1
Views: 1000
Reputation: 21
Yes, you can do that by extending WebMvcConfigurer
and add below method
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
Note: Make sure your class annotated with @configuration
Upvotes: 1
Reputation: 364
We have implemented the swagger-ui along with custom exception handler. The way we are approaching is by parsing the swagger file of the api.
We have configured our swagger the by configuring @Bean of Docket which is also specified in this link: https://springframework.guru/spring-boot-restful-api-documentation-with-swagger-2/
Upvotes: 0