2

I am working on a SpringBoot based REST Api and using Springfox to generate the swagger docs.

Here is the Swagger configuration:

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
    @Autowired
    private AppConfiguration appConfiguration;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("app-api")
                .apiInfo(apiInfo())
                .select()
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("App API")
                .termsOfServiceUrl(appConfiguration.getApiTosUrl())
                .version("1.0").build();
    }
}

And here is the web security configuration I am using:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable().authorizeRequests()
                .antMatchers("/no-auth/**").permitAll()
                .antMatchers("/v2/api-docs", "/swagger-resources/configuration/ui", "/swagger-resources", "/swagger-resources/configuration/security", "/swagger-ui.html", "/webjars/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .addFilter(new JWTAuthenticationFilter(securityConfiguration, authenticationManager()))
                // this disables session creation on Spring Security
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
    }

    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
        return source;
    }

Visiting the url: http://localhost:8080/swagger-ui.html throws up HTTP error 405: GET request not supported.

Although I can visit the url: http://localhost:8080/v2/api-docs?group=app-api

I have scoured the web to find a resolution but I am not getting why this is happening.

You may instruct me on how to resolve this issue.

EDIT: I noticed that swagger-ui.htm is not being mapped at all! I am using springfox 2.8.0

2018-05-14 12:17:47.019 INFO 1012 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/swagger-resources]}" onto public org.springframework.http.ResponseEntity> springfox.documentation.swagger.web.ApiResourceController.swaggerResources() 2018-05-14 12:17:47.020 INFO 1012 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/swagger-resources/configuration/ui]}" onto public org.springframework.http.ResponseEntity springfox.documentation.swagger.web.ApiResourceController.uiConfiguration() 2018-05-14 12:17:47.021 INFO 1012 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/swagger-resources/configuration/security]}" onto public org.springframework.http.ResponseEntity springfox.documentation.swagger.web.ApiResourceController.securityConfiguration() 2018-05-14 12:17:47.026 INFO 1012 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)

conquester
  • 1,082
  • 2
  • 21
  • 44

2 Answers2

0

I had the same when I have a request mapping /**. The cause of the 405 is that the Swagger request is handled by another requestmapping. You can change it for swagger URL in the DispatcherServlet. I have made a minimal example at https://github.com/pmoerenhout/springfox-boot-bug.

You can use this Spring mapping to exclude the Swagger UI patterns:

@RequestMapping(value = { "/", "/{part:^(?!swagger-ui$).*}/**" })
0

This comment saved a lot of my time. In short - I found that someone in my project added mapping to controller like this:

@RestController("/api/test")

but of course it should look like this:

@RestController
@RequestMapping("/api/test")

Because of above I got 405 responses while trying to see swagger-ui.

@RestConroller's docs explain the issue more precisely:

The value may indicate a suggestion for a logical component name, to be turned into a Spring bean in case of an autodetected component. Returns: the suggested component name, if any (or empty String otherwise) Since: 4.0.1