@ApiModelProperty Swagger-Mvc [英] @ApiModelProperty Swagger-Mvc

查看:27
本文介绍了@ApiModelProperty Swagger-Mvc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在使用 swagger-mvc(0.9.5 版)时遇到问题.

Hello guys I have a problem with swagger-mvc (version 0.9.5).

我有以下休息控制器:

CanvasController.java(片段)

CanvasController.java (snippet)

/**
 * The Class CanvasController.
 */
@Api(description = "Operations for canvases", value="canvascontroller")
@RestController
@RequestMapping(value = "/api/canvas")
public class CanvasController {


  ...

  @ApiResponses(value = {
    @ApiResponse(code = 200, message = "No problem occurred.")
  })
  @ApiOperation(value = "Returns all canvases.", notes = "Returns all canvases.", response = Canvas.class, responseContainer = "List")
  @RequestMapping(value = {"/all", "", "/"}, method = {        RequestMethod.GET, RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_VALUE)
  public @ResponseBody ResponseEntity < List < Canvas >> getAll() {
    return new ResponseEntity < List < Canvas >> (canvasService.findAll(),
      HttpStatus.OK);
  }

}

Canvas.java

Canvas.java

package cloudincubator.canvas.db;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.codehaus.jackson.annotate.JsonProperty;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.wordnik.swagger.annotations.ApiModel;
import com.wordnik.swagger.annotations.ApiModelProperty;

/**
 * The Class Canvas.
 */
@Entity
@Table(name = "Canvas", uniqueConstraints = {@UniqueConstraint(columnNames = {"C_Id"})})
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@canvasId")
@ApiModel(value = "Canvas", description = "Represents a canvas in the database.")
public class Canvas
 {

    /** The {@link CanvasGroup} to which this Canvas belongs. */
    @ManyToOne(optional = true)
    @JoinColumn(name = "C_Canvas_Group_Id", referencedColumnName = "CG_Id", nullable = true)
    private CanvasGroup ccanvasgroup;

    /** The description of this Canvas. */
    @Column(name = "C_Description")
    @ApiModelProperty(value="Description")
    @JsonProperty("TEST")
    private String cdescription;

    /** The x position in the CanvasGroup grid. */
    @Column(name = "C_GridX")
    @ApiModelProperty(value="GridX")
    private Integer cgridx;

    /** The y position in the CanvasGroup grid. */
    @Column(name = "C_GridY")
    @ApiModelProperty(value="GridY")
    private Integer cgridy;     

    /** The height of this Canvas. */
    @Column(name = "C_Height")
    private Integer cheight;

    /** The path of the icon which belongs to this Canvas. */
    @Column(name = "C_Icon")
    private String cicon;

    /** The identifier of this Canvas. */
    @Id
    @Column(name = "C_Id", table = "Canvas")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long cid;

    /** The name of this Canvas. */
    @Column(name = "C_Name")
    private String cname;

    /** The shortcut of this Canvas. */
    @Column(name = "C_Shortcut")
    private String cshortcut;

    /** The {@link Sticker Stickers} which belong to this Canvas. */
    @OneToMany(cascade = CascadeType.ALL, mappedBy="scanvas")   
    @Column(nullable = true)    
    private List<Sticker> cstickers;

    /** The width of this Canvas. */
    @Column(name = "C_Width")
    private Integer cwidth;

    /**
     * Instantiates a new Canvas.
     */
    public Canvas(){}

    /**
     * Instantiates a new Canvas with the given identifier .
     *
     * @param cId : The identifier of this Canvas.
     */
    public Canvas(Long cId)
    {
        this.cid = cId;
    }

    /**
     * Instantiates a new Canvas with the given name.
     *
     * @param cName : The name of this Canvas. 
     */
    public Canvas(String cName) {       
        this.cname  = cName;
    }   

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object other) {
        if (this == other) return true;
        if ( !(other instanceof Canvas) ) return false;

        final Canvas canvas = (Canvas) other;

        if ( !canvas.getCId().equals( getCId() ) ) return false;        

        return true;
    }

    /**
     * Gets the {@link CanvasGroup} to which this Canvas belongs.
     *
     * @return the {@link CanvasGroup}.
     */
//  @ApiModelProperty(position=1, required = true, hidden = false, dataType="cloudincubator.canvas.db.CanvasGroup")
    public CanvasGroup getCCanvasGroup() {
        return ccanvasgroup;
    }

    /**
     * Gets the description of this Canvas.
     *
     * @return the description of this Canvas.
     */ 
    public String getCDescription() {
        return cdescription;
    }

    /**
     * Gets the x position in the CanvasGroup grid of this Canvas.
     *
     * @return the x position in the CanvasGroup grid of this Canvas.
     */
    public Integer getCGridX() {
        return cgridx;
    }

    /**
     * Gets the y position in the CanvasGroup grid of this Canvas.
     *
     * @return the y position in the Canvasgroup grid of this Canvas.
     */
    public Integer getCGridY() {
        return cgridy;
    }

    /**
     * Gets the height of this Canvas.
     *
     * @return the height of this Canvas.
     */
    public Integer getCHeight() {
        return cheight;
    }

    /**
     * Gets the path of the icon which belongs to this Canvas.
     *
     * @return the icon path of this Canvas.
     */
    public String getCIcon() {
        return cicon;
    }

    /**
     * Gets the identifier of this Canvas.
     *
     * @return the identifier of this Canvas.
     */
    public Long getCId() {
        return cid;
    }

    /**
     * Gets the name of this Canvas.
     *
     * @return the name of this Canvas.
     */
    public String getCName() {
        return cname;
    }

    /**
     * Gets the shortcut of this Canvas.
     *
     * @return the shortcut of this Canvas.
     */
    public String getCShortcut() {
        return cshortcut;
    }

    /**
     * Gets the {@link Sticker Stickers} which belong to this Canvas.
     *
     * @return the {@link Sticker Stickers} which belong to this Canvas.
     */
    public List<Sticker> getCStickers()
    {
        return cstickers;
    }

    /**
     * Gets the width of this Canvas.
     *
     * @return the width of this Canvas.
     */
    public Integer getCWidth() {
        return cwidth;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    public int hashCode() {        
        return getCId().hashCode();
    }


    /**
     * Sets the {@link CanvasGroup} to which this Canvas belongs.
     *
     * @param cCanvasGroup : The {@link CanvasGroup} to set.
     */
    public void setCCanvasGroup(CanvasGroup cCanvasGroup) {
        ccanvasgroup = cCanvasGroup;
    }

    /**
     * Sets the description of this Canvas.
     *
     * @param cDescription : The description to set.
     */
    public void setCDescription(String cDescription) {
        cdescription = cDescription;
    }

    /**
     * Sets the x position in the CanvasGroup grid.
     *
     * @param cGridX : The x position to set.
     */
    public void setCGridX(Integer cGridX) {
        cgridx = cGridX;
    }

    /**
     * Sets the y position in the CanvasGroup grid.
     *
     * @param cGridY : The y position to set.
     */
    public void setCGridY(Integer cGridY) {
        cgridy = cGridY;
    }

    /**
     * Sets the height of this Canvas.
     *
     * @param cHeight : The height to set.
     */
    public void setCHeight(Integer cHeight) {
        cheight = cHeight;
    }

    /**
     * Sets the icon path of this Canvas.
     *
     * @param cIcon : The icon path to set.
     */
    public void setCIcon(String cIcon) {
        cicon = cIcon;
    }

    /**
     * Sets the identifier of this Canvas.
     *
     * @param cid : The identifier to set.
     */
    public void setCId(Long cid)
    {
        this.cid = cid;
    }

    /**
     * Sets the name of this Canvas.
     *
     * @param cName : The name to set
     */
    public void setCName(String cName) {
        cname = cName;
    }

    /**
     * Sets the shortcut of this Canvas.
     *
     * @param cShortcut : The shortcut to set
     */
    public void setCShortcut(String cShortcut) {
        cshortcut = cShortcut;
    }

    /**
     * Sets the {@link Sticker Stickers} which belong to this Canvas.
     *
     * @param cStickers : The list of {@link Sticker Stickers} to set.
     */
    public void setCStickers(List<Sticker> cStickers)
    {
        this.cstickers = cStickers;
    }

    /**
     * Sets the width of this Canvas.
     *
     * @param cWidth : The width to set.
     */
    public void setCWidth(Integer cWidth) {
        cwidth = cWidth;
    } 
}

招摇输出

{
    "apiVersion": "1.0",
    "swaggerVersion": "1.2",
    "basePath": "/",
    "resourcePath": "/api/canvas",
    "produces": [
        "application/json"
    ],
    "consumes": [
        "application/json"
    ],
    "apis": [
        {
            "path": "/api/canvas",
            "description": "getAll",
            "operations": [
                {
                    "method": "GET",
                    "summary": "Returns all canvases.",
                    "notes": "Returns all canvases.",
                    "nickname": "getAll",
                    "produces": [
                        "application/json"
                    ],
                    "consumes": [
                        "application/json"
                    ],
                    "parameters": [],
                    "responseMessages": [
                        {
                            "code": 200,
                            "message": "No problem occurred.",
                            "responseModel": "List"
                        },
                        {
                            "code": 401,
                            "message": "Unauthorized"
                        },
                        {
                            "code": 403,
                            "message": "Forbidden"
                        },
                        {
                            "code": 404,
                            "message": "Not Found"
                        }
                    ],
                    "deprecated": "false",
                    "type": "Canvas"
                },
                {
                    "method": "POST",
                    "summary": "Returns all canvases.",
                    "notes": "Returns all canvases.",
                    "nickname": "getAll",
                    "produces": [
                        "application/json"
                    ],
                    "consumes": [
                        "application/json"
                    ],
                    "parameters": [],
                    "responseMessages": [
                        {
                            "code": 200,
                            "message": "No problem occurred.",
                            "responseModel": "List"
                        },
                        {
                            "code": 201,
                            "message": "Created"
                        },
                        {
                            "code": 401,
                            "message": "Unauthorized"
                        },
                        {
                            "code": 403,
                            "message": "Forbidden"
                        },
                        {
                            "code": 404,
                            "message": "Not Found"
                        }
                    ],
                    "deprecated": "false",
                    "type": "Canvas"
                }
            ]
        },
        {
            "path": "/api/canvas/",
            "description": "getAll",
            "operations": [
                {
                    "method": "GET",
                    "summary": "Returns all canvases.",
                    "notes": "Returns all canvases.",
                    "nickname": "getAll",
                    "produces": [
                        "application/json"
                    ],
                    "consumes": [
                        "application/json"
                    ],
                    "parameters": [],
                    "responseMessages": [
                        {
                            "code": 200,
                            "message": "No problem occurred.",
                            "responseModel": "List"
                        },
                        {
                            "code": 401,
                            "message": "Unauthorized"
                        },
                        {
                            "code": 403,
                            "message": "Forbidden"
                        },
                        {
                            "code": 404,
                            "message": "Not Found"
                        }
                    ],
                    "deprecated": "false",
                    "type": "Canvas"
                },
                {
                    "method": "POST",
                    "summary": "Returns all canvases.",
                    "notes": "Returns all canvases.",
                    "nickname": "getAll",
                    "produces": [
                        "application/json"
                    ],
                    "consumes": [
                        "application/json"
                    ],
                    "parameters": [],
                    "responseMessages": [
                        {
                            "code": 200,
                            "message": "No problem occurred.",
                            "responseModel": "List"
                        },
                        {
                            "code": 201,
                            "message": "Created"
                        },
                        {
                            "code": 401,
                            "message": "Unauthorized"
                        },
                        {
                            "code": 403,
                            "message": "Forbidden"
                        },
                        {
                            "code": 404,
                            "message": "Not Found"
                        }
                    ],
                    "deprecated": "false",
                    "type": "Canvas"
                }
            ]
        }
    ],
    "models": {
        "ResponseEntity": {
            "description": "",
            "id": "ResponseEntity",
            "properties": {
                "headers": {
                    "required": false,
                    "type": "HttpHeaders"
                },
                "body": {
                    "required": false,
                    "type": "object"
                },
                "statusCode": {
                    "enum": [
                        "100",
                        "101",
                        "102",
                        "103",
                        "200",
                        "201",
                        "202",
                        "203",
                        "204",
                        "205",
                        "206",
                        "207",
                        "208",
                        "226",
                        "300",
                        "301",
                        "302",
                        "302",
                        "303",
                        "304",
                        "305",
                        "307",
                        "308",
                        "400",
                        "401",
                        "402",
                        "403",
                        "404",
                        "405",
                        "406",
                        "407",
                        "408",
                        "409",
                        "410",
                        "411",
                        "412",
                        "413",
                        "413",
                        "414",
                        "414",
                        "415",
                        "416",
                        "417",
                        "418",
                        "419",
                        "420",
                        "421",
                        "422",
                        "423",
                        "424",
                        "426",
                        "428",
                        "429",
                        "431",
                        "500",
                        "501",
                        "502",
                        "503",
                        "504",
                        "505",
                        "506",
                        "507",
                        "508",
                        "509",
                        "510",
                        "511"
                    ],
                    "required": false,
                    "type": "string"
                }
            }
        },
        "Canvas": {
            "description": "Represents a canvas in the database.",
            "id": "Canvas",
            "properties": {}
        },
        "Void": {
            "description": "",
            "id": "Void",
            "properties": {}
        }
    }
}

我的问题是 swagger 没有从画布类返回 api 模型属性,正如您在 swagger 输出中看到的那样我希望你们能帮助我.

My problem is that swagger does not return the api model properties from the canvas class as you can see in the swagger output I hope you guys can help me with that.

提前致谢.

推荐答案

问题原因: 其他java文件中存在同名注解.可以在@ApiModel("Name")中查找,看该注解是否存在于其他文件中.

The Reason of the problem is: there are annotations with the same name in other java files. you can look for in @ApiModel("Name"), and see whether the annotation exists in other files.

这篇关于@ApiModelProperty Swagger-Mvc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆