我有使用kotlin('1.3.50')编写的springboot(2.11. RELEASE)web流应用程序。我想添加swagger留档。我添加到我的build.gradle:
compile group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'
我的Rest控制器看起来像这样:
@RestController
@RequestMapping("/path", produces = [MediaType.APPLICATION_JSON_VALUE])
@Validated
class CreditApplicationController @Autowired constructor(
private val creditService: CreditService
) {
@PostMapping(consumes = [MediaType.APPLICATION_JSON_VALUE], produces = [MediaType.APPLICATION_JSON_VALUE])
fun applyCredit(
@Valid @RequestBody request: ApplyCreditRequest,
@OperationId operationId: String
): Mono<ResponseEntity<ApplicationCreditResponse>> {
...
ApplyCreditRequest
是一个简单的kotlin数据类
@Validated
data class ApplyCreditRequest(
@get:JsonProperty("application_id", required = true)
@NotBlank(message = "application_id cannot be empty")
val applicationId: String,
@get:JsonProperty("customer_info", required = true)
@field:Valid
val customerInfo: CustomerInfo,
@get:JsonProperty("credit_details", required = true)
@field:Valid
val creditDetails: CreditDetails,
@get:JsonProperty("agent", required = true)
@Valid
val agent: Agent
)
更新:
更新为:
@PostMapping(consumes = [MediaType.APPLICATION_JSON_VALUE], produces = [MediaType.APPLICATION_JSON_VALUE])
fun applyCredit(
@io.swagger.v3.oas.annotations.parameters.RequestBody(
content = [
Content(
schema = Schema(
implementation = ApplyCreditRequest::class
)
)
]
) @Valid @RequestBody request: ApplyCreditRequest,
@OperationId operationId: String
): Mono<ResponseEntity<ApplicationCreditResponse>> {
但还是不起作用
通常,您可以向将传入请求的对象添加@ApiModel
注释。
(您还可以使用@ApiModelProperty
注释来注释对象的属性。)
Java中的一个示例如下所示:
// TopicContoller.java
// The POST operation accepts a 'Topic' object in the request body, and returns the same object
@RestController
public class TopicContoller {
// Other detail omitted...
@ApiOperation(
value = "Add a topic",
notes = "Adds a new topic.",
response = Topic.class)
@RequestMapping(method = RequestMethod.POST,value = "/topics")
public Topic addTopic(@RequestBody Topic topic) {
topicService.addTopic(topic);
return topic;
}
}
// Topic.java
// The 'Topic' object
@ApiModel(description = "An object that represents a given topic.")
public class Topic {
@ApiModelProperty(notes = "The Topic ID, as a unique String", example = "php")
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
// Other detail omitted...
}
我的一些请求遇到了类似的问题,有些使用了@Request estBody模式,有些没有,最终我在swagger文档中发现了以下内容
与OpenAPI 2.0的区别
如果您之前使用过OpenAPI 2.0,以下是帮助您开始使用OpenAPI 3.0的更改摘要:
似乎基本的@Request estMaps默认为get请求。
在我的例子中,将映射更改为显式@PostMaps会大摇大摆地看到请求正文中的Schema。
…但这改变了RestAPI的语义学,对我来说,这是两害相权取其轻。
我认为您包含了错误的@Request estBody,您应该使用Spring boot而不是Spring fox提供的注释。