Reputation: 11
I followed this guide in order to enable Swagger UI with JAX-RS in Spring Boot Application: https://atechref.com/using-swagger-2-with-spring-boot-spring-fox-and-jax-rs-project/ but I get the following error all the time: Failed to load API definition
I use following dependencies:
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Spring boot version: 2.3.3.RELEASE
These are the classes I have right now:
@EnableSwagger2
@SpringBootApplication
@Import({MyAppWebSpringConfig.class})
public class MyAppApplication {
public static void main(String[] args) {
SpringApplication.run(MyAppApplication.class, args);
}
}
@Component
@Primary
public class CombinedResourceProvider implements SwaggerResourcesProvider{
@Resource
private InMemorySwaggerResourcesProvider inMemorySwaggerResourcesProvider;
public List<SwaggerResource> get() {
SwaggerResource jerseySwaggerResource = new SwaggerResource();
jerseySwaggerResource.setLocation("/my-app/swagger.json");
jerseySwaggerResource.setSwaggerVersion("2.0");
jerseySwaggerResource.setName("Jersey");
return Stream.concat(Stream.of(jerseySwaggerResource),
inMemorySwaggerResourcesProvider.get().stream()).collect(Collectors.toList());
}
}
@Configuration
@ApplicationPath("/my-app")
public class MyAppResourceConfig extends ResourceConfig{
@Autowired
public MyAppResourceConfig(@Qualifier("ets-feature") Feature jerseyOperationalFeature){
register(jerseyOperationalFeature);
register(MyAppResource.class);
//exceptions
register(GenericExceptionMapper.class);
configureJersey();
}
public void configureJersey(){
BeanConfig swaggerConfig = new BeanConfig();
swaggerConfig.setBasePath("/my-app");
SwaggerConfigLocator.getInstance().putConfig(SwaggerContextService.CONFIG_ID_DEFAULT, swaggerConfig);
packages(getClass().getPackage().getName(), ApiListingResource.class.getPackage().getName());
}
@Configuration
@EnableConfigurationProperties
@ComponentScan(basePackages = {"com.my.app"})
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
@Import({CoreSpringConfig.class})
public class MyAppWebSpringConfig extends SpringBootServletInitializer{
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.my.app"))
.paths(PathSelectors.any())
.build();
}
}
Any idea what I'm doing wrong?..
Upvotes: 1
Views: 562
Reputation: 1
According to the article above. I've made some adjustments to the class JerseyConfig
see the code :
@Component
@ApplicationPath("/api")
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
configureSwagger();
}
private void configureSwagger() {
register(ApiListingResource.class);
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("0.0.1");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("localhost:8080"); // change if necessary
beanConfig.setBasePath("/");
beanConfig.setDescription("Sample");
beanConfig.setContact("VIIGit");
beanConfig.setResourcePackage("ch.vii.git.swagger.sample.rest"); // chanage the package path which you need to scan
beanConfig.setPrettyPrint(true);
beanConfig.setScan(true);
}
}
After that, try to open http://localhost:8080/api/swagger.json
and if you see the swagger json data, than open the page http://localhost:8080/swagger-ui.html
will no longer show the error.
And the rest of the guide which you followed (https://atechref.com/using-swagger-2-with-spring-boot-spring-fox-and-jax-rs-project/
) seems no problem.
Upvotes: 0