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

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返回根本没有此属性)

转载于:https://my.oschina.net/honesty/blog/1838007

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值