Spring-boot 2 和 swagger 2 (springfox) 不显示模型
Spring-boot 2 and swagger 2 (springfox) does not show model
我已经创建了我的补丁端点(Json RFC 6902 中指定的路径)。
在springfox生成的UI显示了我的端点,但是模型示例(只有补丁)没有显示。
为了在我的 Spring-boot 2 项目中使用 Json 补丁,我在 pom.xml.
上使用了该依赖项
<dependencies>
<dependency>
<groupId>com.github.java-json-tools</groupId>
<artifactId>json-patch</artifactId>
<version>1.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
在我的端点,我的代码是:
@RestController
@RequestMapping(value = "/operation", produces = "application/json")
public class IntentController {
@RequestMapping(value = "/{id}",
method = RequestMethod.PATCH,
consumes = "application/json-patch+json")
public void updateValue(@PathVariable Long id, @RequestBody JsonPatch patch){ {
// ... do magic
}
@RequestMapping(value = "/{id}",
method = RequestMethod.GET)
public MyDto getValue(@PathVariable Long id){ {
MyDto dto = service.findById(id);
return dto;
}
@RequestMapping(method = RequestMethod.POST)
public void updateValue(@RequestBody MyDto dto){ {
service.insert(dto);
}
}
我的 GET 和 POST 端点在 UI 中的示例模型生成得很好。
只有 PATCH 不能正常工作...他们的示例模型没有生成。
问题在于 JsonPatch
对象,该对象没有任何 getter 方法,因此 Springfox
库无法生成请求的模型。
一个可能的解决方案可能是,您使用 getter 和 setter 创建一个自定义 MyJsonPatch
POJO,并使用 MyJsonPatch
的数据创建一个 JsonPatch
].
我找不到解决我的问题的方法,所以我决定使用 Swagger 的@ApiParam 来描述这个字段是一个 RFC 6902 实现。
我已经创建了我的补丁端点(Json RFC 6902 中指定的路径)。 在springfox生成的UI显示了我的端点,但是模型示例(只有补丁)没有显示。
为了在我的 Spring-boot 2 项目中使用 Json 补丁,我在 pom.xml.
上使用了该依赖项<dependencies>
<dependency>
<groupId>com.github.java-json-tools</groupId>
<artifactId>json-patch</artifactId>
<version>1.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
在我的端点,我的代码是:
@RestController
@RequestMapping(value = "/operation", produces = "application/json")
public class IntentController {
@RequestMapping(value = "/{id}",
method = RequestMethod.PATCH,
consumes = "application/json-patch+json")
public void updateValue(@PathVariable Long id, @RequestBody JsonPatch patch){ {
// ... do magic
}
@RequestMapping(value = "/{id}",
method = RequestMethod.GET)
public MyDto getValue(@PathVariable Long id){ {
MyDto dto = service.findById(id);
return dto;
}
@RequestMapping(method = RequestMethod.POST)
public void updateValue(@RequestBody MyDto dto){ {
service.insert(dto);
}
}
我的 GET 和 POST 端点在 UI 中的示例模型生成得很好。
只有 PATCH 不能正常工作...他们的示例模型没有生成。
问题在于 JsonPatch
对象,该对象没有任何 getter 方法,因此 Springfox
库无法生成请求的模型。
一个可能的解决方案可能是,您使用 getter 和 setter 创建一个自定义 MyJsonPatch
POJO,并使用 MyJsonPatch
的数据创建一个 JsonPatch
].
我找不到解决我的问题的方法,所以我决定使用 Swagger 的@ApiParam 来描述这个字段是一个 RFC 6902 实现。