当我没有@EnableWebMvc时,我可以访问
http://localhost:8080/swagger-ui.html
但看起来像这样:
当我启用时,我无法访问该网址(似乎不存在)
在我的启动日志中我只看到
Mapped "{[/custom],methods=[GET]}" onto java.lang.String com.example.demo.web.rest.HelloController.hi()
2017-08-08 15:27:29.951 INFO 94164 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/u],methods=[GET]}" onto java.lang.String com.example.demo.web.rest.HelloController.home()
2017-08-08 15:27:29.952 INFO 94164 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/v2/api-docs],methods=[GET],produces=[application/json || application/hal+json]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)
2017-08-08 15:27:29.955 INFO 94164 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/swagger-resources/configuration/ui]}" onto org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.UiConfiguration> springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()
2017-08-08 15:27:29.956 INFO 94164 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/swagger-resources/configuration/security]}" onto org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.SecurityConfiguration> springfox.documentation.swagger.web.ApiResourceController.securityConfiguration()
2017-08-08 15:27:29.956 INFO 94164 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/swagger-resources]}" onto org.springframework.http.ResponseEntity<java.util.List<springfox.documentation.swagger.web.SwaggerResource>> springfox.documentation.swagger.web.ApiResourceController.swaggerResources()
2017-08-08 15:27:29.959 INFO 94164 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-08-08 15:27:29.959 INFO 94164 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-08-08 15:27:30.137 INFO 94164 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter
我的配置文件是:
@Configuration
//@EnableWebMvc
@EnableSwagger2
public class WebConfig {
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.web.rest"))
.paths(PathSelectors.any())
.build()
.apiInfo(metaData());
}
private ApiInfo metaData() {
ApiInfo apiInfo = new ApiInfo(
"Spring Boot REST API",
"Spring Boot REST API for Online Store",
"1.0",
"Terms of service",
"someone",
"Apache License Version 2.0",
"https://www.apache.org/licenses/LICENSE-2.0");
return apiInfo;
}
}
我的主要是:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
和我的pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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.4.0</version>
</dependency>
任何想法都错了吗?
答案 0 :(得分:1)
如果您的WebConfig
与DemoApplication
课程不同,您可能希望将该论坛添加到basePackages
@ComponentScan
答案 1 :(得分:0)
有你的资源处理员吗?
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger/**").addResourceLocations("classpath:/swagger/");
}
我在一个扩展WebMvcConfigurerAdapter
答案 2 :(得分:0)
如果您的应用程序已定义自定义contextPath(application.properties中的server.contextPath),则必须将其包含在URL中。 http://localhost:8080/contextPath/swagger-ui.html
您还可以尝试将RequestHandlerSelectors.basePackage("com.example.demo.web.rest")
更改为RequestHandlerSelectors.any()
,看看会发生什么。