次のコマンドを使用して、swagger を使用してスプリング ブート コードを生成します。
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...
}
@XmlRootElement(name = "AlertDef")ただし、Restful API が XML を正常に返すようにするには、クラスと@XmlElement各 set メソッドに追加する必要があります。
yaml ファイルに何か欠けているものがありますか、それとも swagger codegen は今のところサポートしていませんか?