SpringFox Swagger 使 XSD 模型需要定义属性
SpringFox Swagger make Definition properties required with XSD model
我有一个 REST web 应用程序,它使用 Swagger 作为文档,除了 body 参数外,它工作得很好。 body的所有属性都是可选的
我的项目对象是用 XSD 文件生成的,我确保所有元素 minOccurs=1 maxOccurs=1 或 unbounded 和我的所有属性都已设置use=required。这似乎对它没有任何影响。接下来我尝试添加
@ApiParam(value = "项目主体", required = true) @RequestBody ProjectInfo project
但这也没有效果,因为 Project 对象本身已经是必需的。有没有办法告诉 Swagger Project 的属性也是必需的?我正在为 Swagger 使用 SpringFox 依赖项。
更新
我设法通过添加 @ApiModel 和 @ApiModelProperty(value = "The unique name of the project", 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 注释以便它自己生成吗?
我需要的注释是 @ApiModelProperty(value = "", 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">
现在您应该可以像这样添加注释添加 class 级别
<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>
可以在此处找到更多信息
我有一个 REST web 应用程序,它使用 Swagger 作为文档,除了 body 参数外,它工作得很好。 body的所有属性都是可选的
我的项目对象是用 XSD 文件生成的,我确保所有元素 minOccurs=1 maxOccurs=1 或 unbounded 和我的所有属性都已设置use=required。这似乎对它没有任何影响。接下来我尝试添加
@ApiParam(value = "项目主体", required = true) @RequestBody ProjectInfo project
但这也没有效果,因为 Project 对象本身已经是必需的。有没有办法告诉 Swagger Project 的属性也是必需的?我正在为 Swagger 使用 SpringFox 依赖项。
更新
我设法通过添加 @ApiModel 和 @ApiModelProperty(value = "The unique name of the project", 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 注释以便它自己生成吗?
我需要的注释是 @ApiModelProperty(value = "", 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">
现在您应该可以像这样添加注释添加 class 级别
<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>
可以在此处找到更多信息