It's my first post so feel free to leave some feedback or if i do something wrong :)
I am using spring-boot and resteasy :
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.paypal.springboot</groupId>
<artifactId>resteasy-spring-boot-starter</artifactId>
<version>2.3.4-RELEASE</version>
</dependency>
I am trying to use Swagger to have a view of my endpoints, so I added this dependency :
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
This dependencies are loaded to my repo, everything looks good.
I added this class to make the easiest config class :
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
When i Launch the app in the debug mod I enter in this previous Bean. Evrything looks fine.
When I launch the Spring app :
@SpringBootApplication
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class,RabbitAutoConfiguration.class})
public class SituationServicesApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SituationServicesApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
}
}
Everything looks fine :
2019-07-06 15:43:27.222 INFO 6336 --- [ main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [v2/api-docs] onto method [public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)]
But when I try to reach the swagger-ui.html :
http://localhost:8092/test/v2/api-docs
or
http://localhost:8092/test/swagger-ui.html
I've got a 404 error :
"RESTEASY003210: Could not find resource for full path: http://localhost:8092/test/v2/api-docs".
I tried to modify the default URL by adding a property : springfox.documentation.swagger.v2.path=v2/api-docs-test
It stills 404.
I tried from scratch with a new empty project and it worked just fine. Something is wrong with my project I guess.
I am sure of the URL used.
Do you know how I can debug this issue? Can I see some created source from Swagger? If I can put some log on it to know where the issue comes from?
Swagger UI allows anyone — be it your development team or your end consumers — to visualize and interact with the API’s resources without having any of the implementation logic in place.
Once Spring Security package is included, cannot navigate to /swagger-ui.html but every other swagger resource is available. Sorry, something went wrong. You need to tell spring security to permit the swagger resources and swagger-ui.
This latest release enables users to use the Swagger Editor to describe OAS 3.0 APIs, and the Swagger UI to visual and automatically generate documentation of an API defined in OAS 3.0. Swagger UI is just one open source project in the thousands that exist in the Swagger ecosystem.
Other swagger endpoints are working as expected (/swagger-resources/configuration/ui, /swagger-resources/configuration/security, /v2/api-docs). I can see the JSON response. spring-boot 1.3.5 (spring-boot-starter-tomcat). This is a spring-boot app and serves RESTful api's and does not have any static content.
If you are using Spring Security, then you have to add some more configuration as below.
Complete Working Example
SecurityConfig.java
package com.swagger.demo.security;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer{
//Swagger Resources
@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/");
}
//If you are using Spring Security Add Below Configuration
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/v2/api-docs", "/configuration/**", "/swagger*/**", "/webjars/**")
.antMatchers(HttpMethod.OPTIONS,"/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception{
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/**", "/v2/api-docs", "/configuration/**", "/swagger*/**", "/webjars/**")
.permitAll()
.anyRequest().authenticated();
}
}
SwaggerConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket apiDocket() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.swagger.demo"))
.paths(PathSelectors.any())
.build();
return docket;
}
}
pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-core</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With