Reputation: 75
I am working on intergrating Springfox 2.2.2 into my Spring MVC project but there are no api-docs generated as I suposse should be. Below some information concerning my configuration.
I have provided following dependencies (together with additional libraries as fasterxml, webjars, correct versions of spring are used etc.)
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
The Springfox is configured as follows:
package com.exemplarypackage.config;
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan("com.exemplarypackage.controller")
public class SwaggerConfig extends WebMvcConfigurerAdapter{
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo(
"My Project's REST API",
"This is a description of your API.",
"API TOS",
"url",
"[email protected]",
"API License",
"API License URL");
return apiInfo;
}
@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/");
}
}
The exemplary controller is presented below:
package com.exemplarypackage.controller;
@Api(value = "test class for springfox")
@Controller
public class TestController {
@ApiOperation(value = "Returns test details")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successful retrieval", response = Test.class),
@ApiResponse(code = 404, message = "Test does not exist"),
@ApiResponse(code = 500, message = "Internal server error")}
)
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test(Locale locale, Model model) {
logger.info("TEST");
return "test";
}
}
With the above settings, when I executed the url: localserver:8080/myApp/swagger-ui there was almost nothing to display but there was no error message.
Then, I added to the src/main/resources/META-INF the content that I found in spring-fox-swagger-ui-2.2.2.jar (I unzipped it and pasted to given folder). Now, when I go to localserver:8080/myApp/swagger-ui all green graphics are displayed but no api docs. I noticed in server logs that swagger-ui looks for swagger-resources endpoints but it gets 404 then. When I go through the server logs, I saw that no such endpoints have been created as: swagger-resources, v2/api-docs etc. However, I noticed that the classes are filtered for swagger annotations... There is a springfox.js file in META-INF/resources/webjars/springfox-swagger-ui folder where the swagger-resorces endpoint is contained - maybe it should be switched to different name?
I have no idea how to make it work... Should I declare somehow those endpoints or should they be automatatically created? Maybe I am just missing somthing small but I am fighting with the problem for the last days and can't figure out what else should be configured to make it work.
Upvotes: 2
Views: 5379
Reputation: 75
I have managed to fix the problem with help of @zubactick. Currently, I have separate config classes for springfox and mvc. So, my springfox config is like this:
package com.myPackage.config;
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.service.ApiInfo;
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 api(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.groupName("test")
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo(
"My Project's REST API",
"This is a description of your API.",
"API TOS",
"url",
"[email protected]",
"API License",
"API License URL");
return apiInfo;
}
}
And my MVC config is like this:
package com.myPackage.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@Import({SwaggerConfig.class})
@ComponentScan("com.myPackage.controller")
public class WebSpringConfig extends WebMvcConfigurerAdapter{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
registry
.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry
.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Bean
public ViewResolver configureViewResolver() {
InternalResourceViewResolver viewResolve = new InternalResourceViewResolver();
viewResolve.setPrefix("/WEB-INF/views/");
viewResolve.setSuffix(".jsp");
return viewResolve;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
Moreover, the following is specified in web.xml file:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.myPackage.config.WebSpringConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
The controller can be the same as provided in the question, however with the above swagger config, all my controllers in declared package are scanned and documented by swagger.
Upvotes: 3
Reputation: 1317
@gromajus, could you please try to use @RestController
annotation instead of @Controller
one in your exemplary controller class?
Upvotes: 0