使用Spring Fox 2.9.2和Spring Boot 2.1.5 RELEASE,我无法使用由Swagger生成的交互式UI。
这是未扩展的REST端点:
这是REST端点扩展的(您可以看到没有文本字段可在其中键入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显得愚蠢(颜色不一样,看上去又不那么锐利,等等)?
想通了...较早版本的Swagger使用了“ Try it Out”按钮来执行REST调用...
现在,这是一个两步过程,您必须先单击“试用”按钮,在文本字段内输入ID,然后单击水平很长的蓝色“执行”按钮。
[这在美学和UI设计方面是一个可怕的变化,并且不像以前的设计那样直观。
尝试将此添加到您的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类的软件包。
希望它能解决您的问题。 :)