我正在使用swagger“2.0”创建我的项目 . 问题是我想在请求体中有一些必填字段,需要进行验证 . Swagger无法验证正文中的字段(Schema Object),我需要在到达实际的API实现之前抛出400 Bad请求 .

Here's the actual code I wrote in my yaml file.

'/groupChats/{pathparam}/message':
post:
tags:
- group chats
description: Adds a message to the given group chat.
operationId: addMessageToGroupChat
consumes:
- application/json
parameters:
- name: pathparam
description: some pathparam
in: path
required: true
type: string
- name: addMessageInput
description: The content of the message to add to the groupChat.
in: body
required: true
schema:
$ref: '#/definitions/AddMessageInput'
- name: Authorization
in: header
type: string
required: false
description: >-
The identification token. It can be a JSON web token, a Basic
Authorization token, etc.
- name: mode
in: header
type: string
required: false
description: >-
there is a high performance mode that tries and optimize the messages
responses:
'201':
description: Message added.
schema:
$ref: '#/definitions/AddMessageOutput'

AddMessageInput:
type: object
properties:
body:
description: The message body.
type: string
**required: true**
tags:
description: some desc
type: array
items:
$ref: '#/definitions/MessageTag'
**required:
- body**

## Model generated in Java Code by Swagger:

@javax.annotation.Generated(value = "class io.swagger.codegen.languages.CompanyHealthSpringCodegen", date = "2017-08-31T20:00:34.574+05:30")

public class AddMessageInput implements PersistenceInformationBearer {

private String body = null;
private List tags = new ArrayList();

@jsonignore
private boolean isNew = false;

/**

The message body.
**/
public AddMessageInput body(String body) {
this.body = body;
return this;
}
@apimodelproperty(required = true, value = "The message body.")
@jsonproperty("body")
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}

/**

Additional tags to be added to the message. These tags will be not encrypted. If you add two tags with the same tag name, the behavior is undefined.
**/
public AddMessageInput tags(List tags) {
this.tags = tags;
return this;
}
@apimodelproperty(value = "Additional tags to be added to the message. These tags will be not encrypted. If you add two tags with the same tag name, the behavior is undefined.")
@jsonproperty("tags")
public List getTags() {
return tags;
}
public void setTags(List tags) {
this.tags = tags;
}

/**

This method returns:
true if the object instance refers to a newly created messaging object (e.g. newly created group chat).
false if the object instance refers to an existing messaging object (e.g. returning a group chat that
already exists).
@return
*/
@override
@jsonignore
public boolean isNew() {
return this.isNew;
}
/**

Sets the isNew attribute, which describes if the object the
instance referring to (e.g. groupChat) is new or already exists
*/
@jsonignore
public void setIsNew(boolean isNew) {
this.isNew = isNew;
}
@override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AddMessageInput addMessageInput = (AddMessageInput) o;
return Objects.equals(body, addMessageInput.body) &&
Objects.equals(tags, addMessageInput.tags);
}

@override
public int hashCode() {
return Objects.hash(body, tags);
}

@override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class AddMessageInput {\n");

sb.append("    body: ").append(toIndentedString(body)).append("\n");
sb.append("    tags: ").append(toIndentedString(tags)).append("\n");
sb.append("}");
return sb.toString();
}

/**

Convert the given object to string with each line indented by 4 spaces
(except the first line).
*/
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}

我通过设置 required as true 并通过在 required: 下添加属性"body"来尝试两种方式(以粗体显示),但它显示错误's not working. Only in swagger UI it' . 但是当我尝试直接从外部命中API(比如Postman)时,它允许我按照AddMessageInput模型中的预期在没有body(String)的情况下点击API . 我不得不明确地检查这个字段 . 如果我遗漏任何东西,请建议我 .

TIA对您的回复 .