1

我使用以下命令使用 swagger 生成 spring boot 代码

java -jar ./swagger-codegen-cli-2.2.1.jar generate \
        -i /swagger.yaml \
        -o ./swagger-demo-restful \
        -l spring

在 swagger.yaml 中使用以下定义:

definitions:
  AlertDef:
    type: object
    properties:
      alertName:
        type: string
        description: name of the alert
        xml:
          name: AlertName

生成的代码如下:

package io.swagger.model;

import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * AlertDef
 */
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringCodegen", date = "2016-11-10T13:06:05.500+08:00")

public class AlertDef   {
  private String alertName = null;

  public AlertDef alertName(String alertName) {
    this.alertName = alertName;
    return this;
  }

   /**
   * name of the alert
   * @return alertName
  **/
  @ApiModelProperty(value = "name of the alert")
  public String getAlertName() {
    return alertName;
  }

  public void setAlertName(String alertName) {
    this.alertName = alertName;
  }
  // toString, hashcode, etc...
}

但是要让restful API成功返回XML,需要添加@XmlRootElement(name = "AlertDef")到类和@XmlElement每个set方法中。

yaml文件中是否缺少某些内容或swagger codegen目前不支持它?

4

0 回答 0