我有一个REST webapp,它使用Swagger作为文档,除了body参数之外,它的工作效果很好。身体的所有属性都是可选的
我的Project对象是使用XSD文件生成的,我确保 minOccurs = 1 maxOccurs = 1或无界的所有元素,并且我的所有属性都设置为 use = required 。这似乎对它没有任何影响。我尝试的下一件事是添加
@ApiParam(值="项目正文",required = true)@RequestBody ProjectInfo项目
但这不会产生任何影响,因为Project对象本身已经是必需的。有没有告诉Swagger项目的属性也是必需的?我正在使用Swagger的SpringFox依赖。
我设法通过添加 @ApiModel 和 @ApiModelProperty(值="项目的唯一名称",required = true)来满足要求到我生成的模型。
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.11
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2016.02.12 at 09:33:59 AM CET
//
package be.smartask.api.model.post;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import javax.xml.bind.annotation.*;
/**
* <p>Java class for anonymous complex type.
* <p>
* <p>The following schema fragment specifies the expected content contained within this class.
* <p>
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "projectInfo")
@ApiModel
public class ProjectInfo {
@XmlAttribute(name = "name", required = true)
protected String name;
/**
* Gets the value of the name property.
*
* @return possible object is
* {@link String }
*/
@ApiModelProperty(value = "The unique name of the project", required = true)
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value allowed object is
* {@link String }
*/
public void setName(String value) {
this.name = value;
}
}
但现在我的问题改为我可以在XSD文件中添加Swagger注释,以便它自己生成吗?
答案 0 :(得分:1)
我需要的注释是 @ApiModelProperty(值=&#34;&#34;,required = true)
要从我的退出生成这个挑战,但我设法通过执行后续步骤来实现。
将此添加到您的pom.xml
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.1</version>
<configuration>
<forceRegenerate>true</forceRegenerate>
<extension>true</extension>
<args>
<arg>-Xannotate</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>1.0.2</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-annotate-plugin-test-annox-annotations</artifactId>
<version>1.0.0</version>
</plugin>
</plugins>
</configuration>
<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
</plugin>
需要依赖项,否则他将无法在编译时找到您的注释。
在您的XSD文件中添加以下内容
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.1"
xmlns:annox="http://annox.dev.java.net"
jaxb:extensionBindingPrefixes="annox">
现在你应该能够像这样添加注释添加类级别
<xs:element name="projectInfo">
<xs:complexType>
<xsd:annotation>
<xsd:appinfo>
<annox:annotate target="getter">@io.swagger.annotations.ApiModel(value = "project")</annox:annotate>
</xsd:appinfo>
</xsd:annotation>
</xs:complexType>
</xs:element>
并且在这样的方法级别
<xs:element name="projectInfo">
<xs:complexType>
<xsd:annotation>
<xsd:appinfo>
<annox:annotate target="getter">@io.swagger.annotations.ApiModel(value = "project")</annox:annotate>
</xsd:appinfo>
</xsd:annotation>
<xs:attribute type="xs:string" name="name" use="required">
<xsd:annotation>
<xsd:appinfo>
<annox:annotate target="getter">@io.swagger.annotations.ApiModelProperty(value="Must be unique", required = true)</annox:annotate>
</xsd:appinfo>
</xsd:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
可在此处找到更多信息