serah
serah

Reputation: 2117

Spring Application: Why is swagger-ui not able to do HTTP GET on swagger-resources/configuration/ui?

I have been trying to setup swagger-ui with my spring REST application. I am using spring 4.2.6.RELEASE and swagger core & ui 2.5.0 . I am not using swagger annotations , expecting swagger to pick up spring REST annotations.

I am able to get swagger to generate the api docs and I am able to view it under v2/api-docs.

I am able to hit the swagger-ui.html page but It does not show any api-docs or controller information on the page. On enabling debugger mode on the browser I see that it is errors out while trying to GET "swagger-resources/configuration/ui" - returns 404 (Not Found).

I have followed the below link for setting up swagger-ui http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

I had initially setup the resource handler as specified in the link above but that did not help either and gave me the same 404 error. I have tried tweaking the resource handler so see if it might help swagger-ui do a GET on swagger-resources/configuration/ui .

Why is swagger-ui not able to GET resource swagger-resources/configuration/ui?

I have setup my resource handlers as below .

SwaggerConfiguration

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

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

Web config file

@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("**/**").addResourceLocations("classpath:/META-INF/resources/");
    registry
    .addResourceHandler("swagger-ui.html")
    .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
    registry
    .addResourceHandler("/webjars/**")
    .addResourceLocations("classpath:/META-INF/resources/webjars/");
}

}

I do have a SecurityConfig setup for the webapp, but I have kept it to the bare minimum just in case it might be causing any issues.

@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 unauthenticated request (must be done before allowing only authenticated requests)
    .and()
    .authorizeRequests()
    .antMatchers("/rest/application/information/").permitAll();

}

I do see some resource mapping when the application starts up

2016-08-31 11:24:55 INFO [localhost-startStop-1] RequestMappingHandlerMapping - Mapped "{[/v2/api-docs],methods=[GET],produces=[application/json || application/hal+json]}" onto public org.springframework.http.ResponseEntity springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest) 2016-08-31 11:24:55 INFO [localhost-startStop-1] RequestMappingHandlerMapping - Mapped "{[/swagger-resources/configuration/security]}" onto org.springframework.http.ResponseEntity springfox.documentation.swagger.web.ApiResourceController.securityConfiguration() 2016-08-31 11:24:55 INFO [localhost-startStop-1] RequestMappingHandlerMapping - Mapped "{[/swagger-resources]}" onto org.springframework.http.ResponseEntity> springfox.documentation.swagger.web.ApiResourceController.swaggerResources() 2016-08-31 11:24:55 INFO [localhost-startStop-1] RequestMappingHandlerMapping - Mapped "{[/swagger-resources/configuration/ui]}" onto org.springframework.http.ResponseEntity springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()

Upvotes: 7

Views: 8012

Answers (1)

ChrisD
ChrisD

Reputation: 31

Assuming you were closely following that guide (http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api) you'll have ended up with different versions for your swagger dependencies which seems to cause the 404 (for me at least...)

Try changing the version of the springfox-swagger-ui dependency to match springfox-swagger2.

I used the following (the guide has swagger-ui as 2.4.0):

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

Upvotes: 3

Related Questions