【问题标题】:SpringFox Swagger make Definition properties required with XSD modelSpringFox Swagger 使 XSD 模型所需的定义属性
【发布时间】:2016-02-12 09:42:06
【问题描述】:

我有一个 REST webapp,它使用 Swagger 作为文档,除了 body 参数之外它运行良好。 body的所有属性都是可选的

我的项目对象是使用 XSD 文件生成的,我确保所有元素 minOccurs=1 maxOccurs=1 or unbounded 以及我的所有属性都设置为 use=required强>。这似乎对它没有任何影响。我尝试的下一件事是添加

@ApiParam(value = "项目主体", required = true) @RequestBody ProjectInfo 项目

但这也没有任何效果,因为 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>
 * &lt;complexType&gt;
 *   &lt;complexContent&gt;
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
 *       &lt;attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" /&gt;
 *     &lt;/restriction&gt;
 *   &lt;/complexContent&gt;
 * &lt;/complexType&gt;
 * </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 注释以便它自己生成吗?

【问题讨论】:

    标签: properties xsd spring-ws swagger-2.0 springfox


    【解决方案1】:

    我需要的注解是@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">
    

    现在您应该可以像这样添加注释添加类级别了

    <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>
    

    更多信息可以在这里找到

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-03
      • 1970-01-01
      • 2017-01-16
      • 2020-03-27
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      相关资源
      最近更新 更多