我正在尝试使用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("my-email@domain.org")
.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>
当我使用网址http://localhost:8082/swagger-ui.html时, 我收到错误404。
有人可以帮我解决这个问题吗?
提前谢谢。
答案 0 :(得分: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/");
}
答案 1 :(得分:0)
我认为在@SpingBootApplication注释之前,必须在@Configuration注释类中使用@EnableSwagger2。看看这个article更近的
尝试添加.paths(PathSelectors.any()):
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}