如何使用Swagger2生成Spring-boot REST API文档?

问题描述 投票:0回答:2

我正在尝试使用swagger2生成我的Spring-boot应用程序的REST API文档。

这是我的application.properties文件内容:

server.port = ${port:8082}
server.contextPath=/myServicePath
spring.h2.console.enabled=true
logging.level.org.hibernate.SQL=debug
spring.datasource.url=jdbc:mysql://${mysql-host:localhost}:${mysql-port:3306}/${mysql-dbname:mydb}
spring.datasource.username=${mysql-user:root}
spring.datasource.password=${mysql-password:password}
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

这是我的swaggerConfig.Java:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import static springfox.documentation.builders.PathSelectors.*;
@Configuration
public class ApiDocumentationConfiguration {
    @Bean
    public Docket documentation() {
    return new Docket(DocumentationType.SWAGGER_2)
      .select()
        .apis(RequestHandlerSelectors.any())
        //.paths(regex("/.*"))
        .build()
      .pathMapping("/")
      .apiInfo(metadata());
    }
    @Bean
    public UiConfiguration uiConfig() {
      return UiConfiguration.DEFAULT;
    }
    private ApiInfo metadata() {
      return new ApiInfoBuilder()
    .title("My awesome API")
    .description("Some description")
    .version("1.0")
    .contact("[email protected]")
    .build();
    }
}

这是Application.Java:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@ComponentScan
@EnableSwagger2
public class Application {
    public static void main( String[] args ) 
    {
        SpringApplication.run(Application.class, args);
    }
}

这是我的控制器:

@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RestController

public class MyController {

    @Autowired
    private HttpServletRequest request;

    @ApiOperation(value = "doStuff", nickname = "doStuff", response = Response.class)
    @RequestMapping(method = RequestMethod.GET, produces = "application/json")
    public String doStuff(@RequestBody String command) {
        return "TestString";
    }       
}

我正在使用Spring-boot 1.4.0和swagger2。 我在pom.xml中添加了以下依赖项:

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

当我使用url http:// localhost:8082 / swagger-ui.html时 ,出现了错误404。

有人可以帮我解决这个问题吗?

先感谢您。

rest spring-boot swagger swagger-ui
2个回答
0
投票

您可能需要资源处理程序。 可能是这样的:

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

0
投票

我认为您必须先将@ EnableSwagger2与@Configuration注释的类一起使用,然后再对@SpingBootApplication进行注释。 仔细看看这篇文章

尝试添加.paths(PathSelectors.any()):

    @Bean
    public Docket api() { 
        return new Docket(DocumentationType.SWAGGER_2)  
          .select()                                  
          .apis(RequestHandlerSelectors.any())              
          .paths(PathSelectors.any())                          
          .build();                                           
    }
© www.soinside.com 2019 - 2024. All rights reserved.