【发布时间】:2021-05-14 11:06:33
【问题描述】:
我遇到了 Swagger codegen 插件生成类的问题。该插件不会为某些模型类中的某些字段生成正确的日期格式。 LocalDate 被翻译成 org.joda.time.LocalDate 而不是 java.time.LocalDate。
这是我的 pom.xml 中的插件配置:
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.3</version>
<executions>
<execution>
<id>1</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>http:aCertainHttpAdress</inputSpec>
<language>spring</language>
<modelPackage>${project.groupId}.model.myPackage</modelPackage>
<generateSupportingFiles>false</generateSupportingFiles>
<generateApis>false</generateApis>
<output>src/main/gen</output>
<configOptions>
<sourceFolder>/</sourceFolder>
</configOptions>
</configuration>
</execution>
我已经尝试添加这两个配置:
<typeMappings>
<typeMapping>LocalDate=LocalDate</typeMapping>
</typeMappings>
<importMappings>
<importMapping>org.joda.time.LocalDate=java.time.LocalDate</importMapping>
</importMappings>
但没有成功。
我也加了这个:
<dateLibrary>java8</dateLibrary>
同样的结果...
这是生成的导入错误的模型类:
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;
import java.math.BigDecimal;
import org.joda.time.LocalDate;
import javax.validation.Valid;
import javax.validation.constraints.*;
/**
* VoceBilancioDto
*/
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2021-02-10T18:39:49.642+01:00")
public class VoceBilancioDto {
@JsonProperty("codeItem")
private String codeItem = null;
@JsonProperty("dateUpdateValue")
private LocalDate dateUpdateValue = null;
@JsonProperty("value")
private BigDecimal value = null;
public VoceBilancioDto codeItem(String codeItem) {
this.codeItem = codeItem;
return this;
}
/**
* Get codeItem
* @return codeItem
**/
@ApiModelProperty(value = "")
public String getCodeItem() {
return codeItem;
}
public void setCodeItem(String codeItem) {
this.codeItem = codeItem;
}
public VoceBilancioDto dateUpdateValue(LocalDate dateUpdateValue) {
this.dateUpdateValue = dateUpdateValue;
return this;
}
/**
* Get dateUpdateValue
* @return dateUpdateValue
**/
@ApiModelProperty(value = "")
@Valid
public LocalDate getDateUpdateValue() {
return dateUpdateValue;
}
public void setDateUpdateValue(LocalDate dateUpdateValue) {
this.dateUpdateValue = dateUpdateValue;
}
public VoceBilancioDto value(BigDecimal value) {
this.value = value;
return this;
}
/**
* Get value
* @return value
**/
@ApiModelProperty(value = "")
@Valid
public BigDecimal getValue() {
return value;
}
public void setValue(BigDecimal value) {
this.value = value;
}
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
VoceBilancioDto voceBilancioDto = (VoceBilancioDto) o;
return Objects.equals(this.codeItem, voceBilancioDto.codeItem) &&
Objects.equals(this.dateUpdateValue, voceBilancioDto.dateUpdateValue) &&
Objects.equals(this.value, voceBilancioDto.value);
}
@Override
public int hashCode() {
return Objects.hash(codeItem, dateUpdateValue, value);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class VoceBilancioDto {\n");
sb.append(" codeItem: ").append(toIndentedString(codeItem)).append("\n");
sb.append(" dateUpdateValue: ").append(toIndentedString(dateUpdateValue)).append("\n");
sb.append(" value: ").append(toIndentedString(value)).append("\n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
有人可以帮忙吗?
非常感谢
【问题讨论】:
标签: spring-boot java-8 swagger-codegen localdate