我使用SpringFox库来获取我的spring boot应用程序的休息文档.当我单击模型时,所有元素都将作为可选项返回.有没有办法将所需元素显示为必需元素?是否需要添加其他配置?
我正在尝试更改 API 文档的基本访问 url。网址是“ http://localhost:8080/swagger-ui.html ”。我想得到类似“ http://localhost:8080/myapi/swagger-ui.html ”的东西。
我使用的是 Springfox 2.8.0 Swagger、Java 8、Spring Boot 2.0 的 swagger 配置是:
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket api(ServletContext servletContext) {
return new Docket(DocumentationType.SWAGGER_2)
.pathProvider(new RelativePathProvider(servletContext) {
@Override
public String getApplicationBasePath() {
return "/myapi";
}
})
.select()
.apis(RequestHandlerSelectors.any())
.paths(Predicates.not(PathSelectors.regex("/error")))
.build()
.useDefaultResponseMessages(false);
}
}
Run Code Online (Sandbox Code Playgroud)
自定义路径提供程序必须提供帮助,但我仍然可以通过使用 url“ http://localhost:8080/swagger-ui.html ”访问 api 文档。如果我使用 url " http://localhost:8080/myapi/swagger-ui.html ",我会收到404 错误。看看下面的截图。
在 Springfox 3.0 中,springfox.documentation.service.Parameter已弃用。
是否有提供参数及其构建器的更新方法?
以及如何提供其default价值?
有两种方法可以将 OpenAPI 3 添加到 Spring Boot 项目。
\n<groupId>org.springdoc</groupId>\n<artifactId>springdoc-openapi-ui</artifactId>\nRun Code Online (Sandbox Code Playgroud)\nhttps://www.dariawan.com/tutorials/spring/documenting-spring-boot-rest-api-springdoc-openapi-3/
\n<groupId>io.springfox</groupId>\n<artifactId>springfox-boot-starter</artifactId>\nRun Code Online (Sandbox Code Playgroud)\nhttps://medium.com/@hala3k/setting-up-swagger-3-with-spring-boot-2-a7c1c3151545
\n还有配置和注释的迁移问题。
\n问题是:对于 Spring Boot 项目,有什么理由在它们之间做出选择吗?
\n更新:迁移到 OpenAPI 3。不太难:)也许会有帮助:
\nOpenApiConfig ,\n pom.xml ,\n \xd0\x9e\xd0\xbf\xd0\xb8\xd1\x81\xd0\xb0\xd0\xbd\xd0\xb8\xd0\xb5
\n我正在使用springfox的swagger实现.我想修改swagger-ui.html以获取自定义标头值.如何修改此文件?或者告诉spring fox使用备用文件?
如图所示,它为添加操作显示"响应类(状态200)".但是,添加操作已经实现,它永远不会返回200.成功时返回201.
我的问题是如何将(状态200)更改为(状态201)?该部分的代码如下:
@RequestMapping(method = RequestMethod.PUT, value = "/add")
@ApiOperation(value = "Creates a new person", code = 201)
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Record created successfully"),
@ApiResponse(code = 409, message = "ID already taken")
})
public ResponseEntity<String> add(@RequestParam(value = "name", required = true) String name,
@RequestParam(value = "id", required = true) String id) {
if (PD.searchByID(id).size() == 0) {
Person p = new Person(name, id);
PD.addPerson(p);
System.out.println("Person added.");
return new ResponseEntity<String>(HttpStatus.CREATED);
} else {
System.out.println("ID already taken.");
return …Run Code Online (Sandbox Code Playgroud) 有没有办法在 springfox swagger (2.7.0) 中公开继承/多态性?我知道 swagger 规范支持 allOf。springfox 支持这个吗?下面是示例域模型。
@ApiModel
public abstract class Animal{
private String name;
}
@ApiModel(parent=Animal.class)
public class Dog extends Animal{
...
}
@ApiModel(parent=Animal.class)
public class Cat extends Animal{
...
}
Run Code Online (Sandbox Code Playgroud)
如果控制器返回 Animal,则 swagger 合约不会公开 Cat 或 Dog。它只返回 Animal 及其属性。
我想知道是否有任何方法可以使 SpringFox 不显示在调用特定端点时不需要的某个实体的所有字段。
例如:
具有以下实体:
public class Car {
long id;
String name;
int wheels;
String type;
boolean canFly;
}
Run Code Online (Sandbox Code Playgroud)
以及以下端点:
@RequestMapping(method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public Car get(@RequestParam(value = "carId", required = true) long projectId) {
return carService.get(carId);
}
@RequestMapping(method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public Car create(@RequestBody Car car) {
return carService.create(car);
}
@RequestMapping(method = RequestMethod.PUT,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public Car update(@RequestBody Car car) {
return carService.update(car);
}
Run Code Online (Sandbox Code Playgroud)
问题是,在 create Car …
我正在尝试为我的 spring 应用程序配置 swagger。下面是配置。但是,得到一个错误
[springfox/documentation/spring/web/SpringfoxWebMvcConfiguration.class] 无法打开,因为它不存在
Spring 版本 - 4.0.4 Springfox 版本 - 2.9.2
它不是 Maven 项目,我将所有必需的 jar 文件添加到类路径中。
弹簧上下文.xml
<bean id="Swagger" class="skt.test.SwaggerConfig" />
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
Run Code Online (Sandbox Code Playgroud)
Swagger 配置类
package skt.test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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
@EnableWebMvc
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
包含 Jar 文件 https://i.imgur.com/sYaYnrY.png
org.springframework.beans.factory.BeanDefinitionStoreException: …Run Code Online (Sandbox Code Playgroud) 我迁移到最新springfox-swagger2版本,2.10.0但看起来@EnableSwagger2已被弃用。
为了在 Spring Boot 项目中启用 Swagger,我应该使用什么注释?@EnableSwagger2WebMvc?
springfox ×10
swagger ×8
java ×6
spring ×4
spring-boot ×3
rest ×2
swagger-2.0 ×2
swagger-ui ×2
spring-mvc ×1