最近使用springfox(swagger)自动生成api文档时问题心得

原创
2018/06/30 00:29
阅读数 4.4K
package io.swagger.models.properties;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSetter;
import io.swagger.models.Xml;

import java.util.Map;

/**覆盖源swagger中的Property接口,将swagger(io.swagger.models.properties.Property)中接口复制过来,即包路径必须跟源码路径一致,否则无法达到覆盖的效果
 * 同时需要修改getRequired()方法,将该方法上的@JsonIgnore注解注释或删除掉即可
 * //@JsonIgnore
 *  boolean getRequired();
 * @Auther: emmy
 * @Date: 2018/6/29 22:31
 * @Description:
 */
public interface Property{

    Property title(String title);

    Property description(String description);

    String getType();

    String getFormat();

    String getTitle();

    void setTitle(String title);

    String getDescription();

    void setDescription(String title);

    Boolean getAllowEmptyValue();

    void setAllowEmptyValue(Boolean value);

    @JsonIgnore
    String getName();

    void setName(String name);

    //@JsonIgnore 当我查看swagger源码时发现将此属性给忽略掉了,所以前端不管你后台是否加了此属性最终显示都是根据对象的required来的,这样不利于接口说明
    boolean getRequired();

    void setRequired(boolean required);

    @JsonGetter
    Object getExample();

    @JsonSetter
    void setExample(Object example);

    //@Deprecated
    //@JsonIgnore
    void setExample(String example);

    Boolean getReadOnly();

    void setReadOnly(Boolean readOnly);

    Integer getPosition();

    void setPosition(Integer position);

    Xml getXml();

    void setXml(Xml xml);

    void setDefault(String _default);

    @JsonIgnore
    String getAccess();

    @JsonIgnore
    void setAccess(String access);

    Map<String, Object> getVendorExtensions();

    /**
     * creates a new instance and renames the property to the given name.
     *
     * @return new shallow copy of the property
     */
    Property rename(String newName);
}

通过修改以上一行代码,即可实现前端展示时,可以根据属性来显示字段是否必填,这样比较利于api文档阅读,注意这只是在请求参数使用@RequestBody注解标识时才会出现此情况,

很多前端渲染框架如swagger-bootstrap-ui,swagger-ui-layui,springfox-swagger-ui都根据参数类型是body时直接显示的是对象的requird值(因为后台api返回根本没有此属性)

展开阅读全文
加载中
点击加入讨论🔥(2) 发布并加入讨论🔥
打赏
2 评论
0 收藏
0
分享
返回顶部
顶部