Swagger UI does not list any of the controller/end points though I am able to see the json under v2/api-docs endpoint

serah picture serah · Aug 30, 2016 · Viewed 11.4k times · Source

I am not able to get my Swagger UI to work with my project. Swagger UI comes up fine but it does not list any of my REST controllers.

I am using SPRING 4.2.6.RELEASE and Swagger 2.5.0 . My rest services are deployed to Tomcat 7.0.54 .

When Tomcat 7.0.54 comes up, it is able to fetch the swagger end points. I am able to hit the endpoint v2/api-docs that fetches the json messages. I am also able to hit the swagger-ui but I dont see any controllers listed. The dropdowns are empty, as below

enter image description here

**The issue I am facing currently is that

  1. I am not able to fetch the /swagger-resources/configuration/ui, when I launch the swagger UI I get 404 (Not Found) errror while the UI is trying to fetch /swagger-resources/configuration/ui . I have setup resource handlers for swagger-resources, but that does not seem to help. Can you please let me know what could be missing?
  2. Should I be seeing resources folder under META-INF in my expanded WAR? Should there be any springfox related files/folder inside META-INF? **

Maven dependency for Swagger
io.springfox springfox-swagger2 2.5.0
io.springfox springfox-swagger-ui 2.5.0

Below is my SwaggerCongifuration

@EnableSwagger2
public class SwaggerConfiguration {

@Bean
public Docket api() {
    List<SecurityContext> security = new ArrayList<SecurityContext>();
    security.add(securityContext());
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()
            .pathMapping("/").securityContexts(security);
}

private SecurityContext securityContext() {
    return SecurityContext.builder()
            .forPaths(PathSelectors.regex("/"))
            .build();
 }
}

Below is my WebConfig.xml

@EnableWebMvc
@Configuration
@Import(SwaggerConfiguration.class)
@ComponentScan("com.bank.direct.services")

public class WebConfig extends WebMvcConfigurerAdapter {


@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> pConverters) {
    pConverters.add(RestUtils.getJSONMessageConverter());
}


@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/");
}

}

Below is the SecurityCongif.xml

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {


@Autowired
private AuthenticationService _authenticationService;


@Autowired
public void globalUserDetails(AuthenticationManagerBuilder pAuth) throws Exception {

    pAuth.userDetailsService(_authenticationService);
}


@Override
protected void configure(HttpSecurity pHttp) throws Exception {

    // Enable HTTP caching
    pHttp.headers().cacheControl().disable();

    // Configure security
    pHttp.httpBasic()

    // -- Allow only authenticated request
    .and()
    .authorizeRequests()
    .anyRequest().authenticated()

    // -- Logout configuration
    .and()
    .logout()
    .logoutUrl("/rest/users/logout/")
    .deleteCookies("XSRF-TOKEN")
    .logoutSuccessUrl("/static/index.html")
    .invalidateHttpSession(true)

    // -- CSRF configuration
    .and()
    .csrf().csrfTokenRepository(csrfTokenRepository())
    .and()
    .addFilterAfter(csrfHeaderFilter(), SessionManagementFilter.class);

}


private Filter csrfHeaderFilter() {

    return new OncePerRequestFilter() {

        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

            CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
            if (csrf != null) {
                Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
                String token = csrf.getToken();
                if (cookie == null || token != null && !token.equals(cookie.getValue())) {
                    cookie = new Cookie("XSRF-TOKEN", token);
                    cookie.setPath("/");
                    response.addCookie(cookie);
                }
            }
            filterChain.doFilter(request, response);
        }
    };
}


private CsrfTokenRepository csrfTokenRepository() {
    HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
    repository.setHeaderName("X-XSRF-TOKEN");
    return repository;
}

Rest Controller class as below

@RestController
@RequestMapping(value = "/vehicles", produces =     MediaType.APPLICATION_JSON_UTF8_VALUE)
public class VehicleResource extends Resource {

@Autowired
private IVehicleService _vehicleService;

@RequestMapping(value = "/brands", method = RequestMethod.GET)
public APIResponseEntity getBrands(WebRequest pWebRequest) {

    IUser user = getUser(pWebRequest);
    BrandCriteria criteria = new BrandCriteria();
    criteria.setLanguageCode(user.getLanguageCode());

    List<Brand> res = _vehicleService.getBrands(user, criteria);

    return newResponseOK(res);
}

@RequestMapping(value = "/brands/{brand_code}", method = RequestMethod.GET)
public APIResponseEntity getBrand(WebRequest pWebRequest, @PathVariable("brand_code") String pBrandCode) {

    IUser user = getUser(pWebRequest);
    BrandCriteria criteria = new BrandCriteria();
    criteria.setLanguageCode(user.getLanguageCode());
    criteria.setBrandCode(pBrandCode);
    List<Brand> res = _vehicleService.getBrands(user, criteria);
    return newResponseOK(res);
 }
}   

Answer

trf picture trf · May 21, 2018

After migrating an older project from XML Spring configuration to Java Spring configuration and updating spring and Swagger versions I struggled with an issue that sounds exactly like this so I thought I'd document my solution here.

I had a number of problems but the main ones that match the OP's scenario were that while /v2/api-docs was accessible and returned JSON, my Controllers clearly weren't being picked up, and when I accessed the Swagger UI at /swagger-ui.html, I was getting a 404 when that page tried to request /swagger-resources/configuration/ui

My Swagger configuration class was:

@Configuration
@EnableSwagger2
public class SwaggerWebConfig {
    @Bean
    public Docket api() {
        ...
    }
}

The @EnableSwagger2 annotation imports another configuration class Swagger2DocumentationConfiguration, which in turn imports SwaggerCommonConfiguration, which does a component scan for classes in springfox.documentation.swagger.web which finally loads the ApiResourceController, which is where

  • /swagger-resources/
  • /swagger-resources/configuration/security and
  • /swagger-resources/configuration/ui

are served from.

What I had incorrect was that my SwaggerWebConfig class was being loaded by the root application context, when it should belong to the web application context (see ApplicationContext vs WebApplicationContext).

Beans in the web application context can access beans in the root application context, but not the other way around, which explained why Docket bean (incorrectly in the root application context) could not pick up the @Controller beans in the web application context and also explained why despite the ApiResourceController bean being created, its methods were giving 404's when trying to access them (they should be in the web application context)

A few other notes for related issues:

  • If you can hit v2/api-docs then your Docket bean is working
  • In a non-spring-boot environment, you need to register two resource handlers yourself as spring boot's auto-configuration would have done this for you as explained in the answers to this question. That should solve 404's for:
    • /swagger-ui.html (i.e. 404 fetching the actual html swagger-ui.html page)
    • and the three webjars that swagger-ui.html loads:
      • /webjars/springfox-swagger-ui/springfox.js
      • /webjars/springfox-swagger-ui/swagger-ui-bundle.js
      • /webjars/springfox-swagger-ui/swagger-ui-standalone-preset.js
  • If you are getting an access denied rather than a 404 not found, then as shown in this answer, you might need to tell spring security to allow access to:
    • /webjars/**
    • /swagger-ui.html
    • /v2/api-docs
    • /swagger-resources/**