Home java How to configure Swagger Spring (non-boot)

How to configure Swagger Spring (non-boot)

Author

Date

Category

My configuration looks like this:

& lt; dependency & gt;
      & Lt; groupId & gt; io.springfox & lt; / groupId & gt;
      & Lt; artifactId & gt; springfox-swagger-ui & lt; / artifactId & gt;
      & Lt; version & gt; $ {swagger.docs.version} & lt; / version & gt;
      & Lt; scope & gt; compile & lt; / scope & gt;
    & lt; / dependency & gt;
    & lt; dependency & gt;
      & Lt; groupId & gt; io.springfox & lt; / groupId & gt;
      & Lt; artifactId & gt; springfox-swagger2 & lt; / artifactId & gt;
      & Lt; version & gt; $ {swagger.docs.version} & lt; / version & gt;
      & Lt; scope & gt; compile & lt; / scope & gt;
    & lt; / dependency & gt;
//P.S. & Lt; swagger.docs.version & gt; 2.9.2 & lt; /swagger.docs.version>
@ Configuration
@ComponentScan (basePackageClasses = {ControllerPackageMarker.class, MappersPackageMarker.class})
@EnableWebMvc
@ EnableSwagger2
@PropertySource ( "classpath: media.upload.properties")
public class WebMvcConfig implements WebMvcConfigurer {
  @Value ( "$ {upload.path}")
  private String path;
  @Value ( "$ {max.file.size}")
  private long maxFileSize;
  @Bean
  public BCryptPasswordEncoder passwordEncoder () {
    return new BCryptPasswordEncoder (12);
  }
  @Override
  public void addArgumentResolvers (List & lt; HandlerMethodArgumentResolver & gt; resolvers) {
    resolvers.add (new AuthenticationPrincipalArgumentResolver ());
  }
  @Bean (name = "multipartResolver")
  public CommonsMultipartResolver multipartResolver () {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver ();
    multipartResolver.setMaxUploadSize (maxFileSize);
    return multipartResolver;
  }
  @Override
  public void addResourceHandlers (ResourceHandlerRegistry registry) {
    boolean isWindows = System.getProperty ( "os.name") contains ( "Windows").;
    registry.addResourceHandler ( "/ img / **")
        .addResourceLocations (? isWindows "file: ///" + path: "file: //" + path);
    registry.addResourceHandler ( "swagger-ui.html")
        .addResourceLocations ( "classpath: / META-INF / resources /");
    registry.addResourceHandler ( "/ webjars / **")
        .addResourceLocations ( "classpath: / META-INF / resources / webjars /");
  }
  @Bean
  public Docket api () {
    return new Docket (DocumentationType.SWAGGER_2)
        .select ()
        .apis (RequestHandlerSelectors.any ())
        .paths (PathSelectors.any ())
        .build ();
  }
}

According to the documentation, bean and dependency pom enough, but when I turn on the link localhost: 8080 / swagger-ui.html does not show anything ((

The security config I pointed antMathers ( "/ swagger-ui.html") permitAll ();.

This is what comes out:

As you can see from the screenshots, let me in the country, but the page is completely blank

(All controllers on the spot)

UPD : / v2 / api-docs work, give json

Do not operate the following URLs (404):

app_root / v2 / swagger-ui /
app_root / swagger-ui /

Answer 1, Authority 100%

Chtosh)

allow URL /swagger-ui.html was not enough. That’s what decided my question:

@ Override
  public void configure (WebSecurity web) throws Exception {
    web.ignoring (). antMatchers ( "/ v2 / api-docs",
        "/ Configuration / ui",
        "/ Swagger-resources / **",
        "/ Configuration / security",
        "/Swagger-ui.html",
        "/ Webjars / **");
  }

it I overdered in the SpringSecurity config and everything worked


Answer 2, Authority 67%

I have in one of the projects /swagger-ui.html did not work because of the dependence

& lt; dependency & gt;
  & lt; groupid & gt; org.apache.tomcat.Embed & lt; / groupid & gt;
  & lt; artifactid & gt; Tomcat-Embed-Jasper & lt; / artifactid & gt;
  & lt; scope & gt; compile & lt; / scope & gt;
& lt; / dependency & gt;

If it is not possible to fix it, you can download Swagger UI and download it as static files (https://github.com/swagger-api/swagger-ui/release/tag/v3.47.1 Directory Dist )

But generally I recommend to watch OpenAPI

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions