使用Spring Fox 2.9.2和Spring Boot 2.1.5 RELEASE,我无法使用Swagger生成的交互式UI。
这是未扩展REST端点的情况:
这是REST Endpoint扩展的(如您所见,没有文本字段可在其中键入id):
Maven pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
Swagger2Config.java:
package com.myservice.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors
.basePackage("com.myservice"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiEndPointsInfo());
}
private ApiInfo apiEndPointsInfo() {
return new ApiInfoBuilder().title("MyService API")
.description("Buildings you measure.")
.contact(new Contact(null, null, null))
.license("Apache 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
.version("1.0.0")
.build();
}
}
还注意到,我不需要在RestController内使用任何Swagger特定的注释(例如@ApiOperation & @ApiResponse
)(我确实尝试将它们放在getByUsingId(Integer id)
方法上方,尽管我的文档是可见的仍然没有id文本字段):
RestController:
@RequestMapping(value = {"/{id}" }, method = RequestMethod.GET, produces = "APPLICATION/JSON")
public ResponseEntity<Object> getByUsingId(@PathVariable(value = "id", required = true) Integer id)
throws IOException {
MyResponse response = myDao.getById(id);
if (response == null) {
return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<Object>(response, headers, HttpStatus.OK);
}
将其与旧版Spring Boot 1.5.6 RELEASE项目进行比较,Spring Fox 2.6.1具有更好的UI且具有交互性(请注意,展开时具有更好的颜色和可见文本字段):
这正是我需要和想要的。
Maven pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependencies>
Swagger2Config.java:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.common.base.Predicates;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@Configuration
public class SwaggerConfig {
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(Predicates.not(PathSelectors.regex("/error"))).build().apiInfo(metaInfo());
}
private ApiInfo metaInfo() {
ApiInfo apiInfo = new ApiInfo("Hierarchy Project", "Hierarchy Demo as a Spring Boot based Microservice", "1.0", null,
new Contact("", "", ""), "", "");
return apiInfo;
}
}
RestController:
@ApiOperation(httpMethod = "GET", value = "Get All Records Within a Level of Hierarchy Based On Parent Node.",
notes = "List records within a level of the hierarchy, for a given parent node.",
produces = "application/json")
@ApiResponses(value =
{
@ApiResponse(code = 200, message = "Successful GET Command", response = String.class),
@ApiResponse(code = 404, message = "Not Found")
}
)
@RequestMapping(value = { "/api/nodes" }, method = RequestMethod.GET, produces = "APPLICATION/JSON")
public ResponseEntity<Object> getHierarchyByUsingNode(Node node) throws Exception {
if (null == node) {
return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
}
List<Node> nodes = nodeService.getHierarchyPerParentNode(node);
if (nodes.isEmpty()) {
return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
}
else {
return new ResponseEntity<Object>(nodes, headers, HttpStatus.OK);
}
}
当我尝试在Spring Boot 2.1.5.RELEASE项目中使用Spring Fox 2.6.1依赖项时,swagger-ui.html页面甚至无法呈现!
我们可以注意到,虽然Spring Fox 2.9.2不需要注释,但是生成的swagger-ui.html页面不仅看起来有所不同(在颜色等方面),而且甚至没有任何文本字段,例如我的第二个示例Hierarchy Project做到了。
问题:
这是Spring Fox 2.9.2中的一个可能的错误(非交互式文本字段)吗?
为什么swagger-ui显得愚蠢(颜色不同且外观不太清晰等)?
答案 0 :(得分:-1)
弄清楚了...较早版本的Swagger使用“试用”按钮执行REST调用...
现在,这是一个两步过程,您必须首先单击“试用”按钮,在文本字段内输入ID,然后单击水平很长的蓝色“执行”按钮。
这是一个可怕的变化(在美学和UI设计方面),并且不像以前的设计那样直观。
答案 1 :(得分:-2)
尝试将其添加到您的pom.xml
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
还有
@Bean
public Docket apiDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("myPackage"))
.paths(PathSelectors.any())
.build();
}
其中myPackage是包含Spring boot starter类的软件包。
希望它将解决您的问题。 :)