1

This is my first time on here. So we have an application built in Spring MVC handling REST calls with 2 java classes serving as the controller(s). One of them is explicitly annotated with @Controller while the other is annotated with @Component. (2 slightly different ways to defining our routes)

The issue we are facing is that despite our configuration/setup, we are only seeing an empty UI with no endpoints and documentation filled out. It's basically a blank canvas. We did a good amount of searching and despite trying out multiple configurations of beans and definitions, we were still running into the same issue.

Most recently we tried out this example : Swagger Springfox Configuration Issue

Some snippets of our setup/configuration and relevant files are produced below.

Controller(s) classes:

UserDeviceAuthenticationController

@RestController
public class UserDeviceAuthenticationController {

private static final Logger LOGGER = Logger
        .getLogger(UserDeviceAuthenticationController.class);

/**
 * 
 * @param response
 * @param request
 * @param deviceID: a misnomer. This field is different for every client set up on Fitbit
 * @throws IOException
 * @throws ParseException
 */
@RequestMapping(value = "/fitbitEndPoint", method = RequestMethod.POST)
public void fitbitEndPoint(HttpServletResponse response,
      //Body logic redacted

}

PHDDeviceRestService

@Path("device/v2.0")
@Component
public class PHDDeviceRestService extends BaseServiceImpl {

    @POST
    @Path("/{deviceId}/url")
    @Produces({ MediaType.APPLICATION_JSON })
    @Consumes({ MediaType.APPLICATION_JSON })
    public Response getDeviceURL(GetDeviceURLRequest getDeviceURLRequest, @PathParam("deviceId") Long deviceId) throws PHDWebServiceException {
         //Logic redacted
    }

Configuration Files :

Our relevant Swagger Springfox Dependencies in our Pom.xml

<!-- Swagger dependency -->
<dependencies>
   <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.6.0</version>
   </dependency>
   <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.6.0</version>
   </dependency>
</dependencies>

Our web.xml containing the servlets and url patterns:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

<servlet>
  <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/mvc/webmvc-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all *.spring requests to the DispatcherServlet for handling -->
<servlet-mapping>
  <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
  <url-pattern>/devices/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
  <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
  <url-pattern>/healthyweight/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
  <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
  <url-pattern>/DevicesGateway/*</url-pattern>
</servlet-mapping>
<servlet>
  <servlet-name>jersey</servlet-name>
  <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
  <init-param>
     <param-name>com.sun.jersey.config.property.packages</param-name>
     <param-value>com.uhg.phd.rest;com.uhg.phd.rest.MongoDB</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet>
  <servlet-name>CXFSevlet</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>CXFSevlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>CXFSevlet</servlet-name>
    <url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<!-- <servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping> -->
<servlet-mapping>
    <servlet-name>CXFSevlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<!-- <servlet-mapping>
   <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
   <url-pattern>/swagger-ui.html</url-pattern>
</servlet-mapping> -->
</web-app>

Our SwaggerConfig class where Docket Bean is defined :

@EnableSwagger2
public class SwaggerConfig {

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build();
}

}

Our WebMvcConfig class with ViewControllers and ResourceHandlers :

@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addRedirectViewController("/documentation/v2/api-docs", "/v2/api-docs?group=restful-api");
    registry.addRedirectViewController("/documentation/swagger-resources/configuration/ui","/swagger-resources/configuration/ui");
    registry.addRedirectViewController("/documentation/swagger-resources/configuration/security","/swagger-resources/configuration/security");
    registry.addRedirectViewController("/documentation/swagger-resources", "/swagger-resources");
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("documentation/swagger-ui.html")
    .addResourceLocations("classpath:/META-INF/resources/");

    registry.addResourceHandler("documentation/webjars/**")
    .addResourceLocations("classpath:/META-INF/resources/webjars/");
}

}

Thank you for reading through all of this. Let me know if you would like to see anymore of our code/files or if I'm missing anything. Any input is appreciated!

JWU1000
  • 11
  • 1

1 Answers1

0

You probably found an answer already but if somebody has the same problem maybe this helps them:

What did the trick for me was to add the package in which I have my SwaggerConfig to the list of ComponentScan basePackages. Basically I have the same SwaggerConfig but my Application file(I'm using Spring boot) looks like this:

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {
        "my.test.config",
        "my.test.controllers",
        "my.test.models"})
public class SwaggerTestApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SwaggerTestApplication.class, args);
    }
}

The important part being the "my.test.config" package in the ComponentScan - where the SwaggerConfig is defined.

tudor
  • 151
  • 2
  • 8
  • Hi tudor. Thanks for your suggestion. We are in Spring MVC and not Spring boot. Anyway, we have a web.xml file defined that points to a webmvc-config.xml and applicationContext.xml files. Within those files, "" has been set and defined. The base package encompasses all the classes and java files including where I defined SwaggerConfig already. For example, SwaggerConfig file falls under com.uhg.phd.Configuration and that should already be scanned inclusively. Is XML not as robust as defining them directly inline Java? – JWU1000 Sep 12 '17 at 21:53
  • The xml is as robust as defining them in Java - so that's not the problem. If you didn't already, you may want to throw a look at the example in the official repository: https://github.com/springfox/springfox-demos/tree/master/spring-xml-swagger And make sure your SwaggerConfig is found (from the example): ... that's all I can think of right now. – tudor Sep 13 '17 at 08:05