提问者:小点点

枚举在swagger中无法正确翻译(以字符串而不是枚举的形式出现)


无法将类型“java. lang.String”的值转换为所需类型“com.x.x.Recordtype”

此外,RecordType枚举是一个反编译文件,因此无法对其进行任何更改。

在swagger-用户界面中,记录类型作为下拉列表出现。下拉列表项

  • RecordType. BILL(记录类型=BILL,键=ABC#)
  • RecordType.LAW=LAW, key=XYZ#)

使用

  • Springdoc v2 for swagger-UI(https://springdoc.org/v2/)
  • Spring靴3
  • Spring Framework 6
//RecordType.class

import lombok.Generated;

public enum RecordType {
    BILL("BILL", "ABC#"),
    LAW("LAW", "XYZ#");
    
    private String recordType;
    private String key;
    
    @Generated
    public String getRecordType() {return this.recordType; }
    
    @Generated
    public string getKey() { return this.key; }
    
    @Generated
    private RecordType(final String recordtype, final String key) {
        this.recordType = recordType;
        this.key = key;
    }
    
    @Generated
    public String toString() {
        String alpha = this.name();
        return "RecordType:"+alpha+"(recordType="+this.getRecordtype+", key="+this.getKey()+")";
    }
}

数据控制器代码

//DataController.java

import org.springframework.http.ResponseEntity;

@Restcontroller
@RequestMapping("/api/internal")
public class Controller {
    @Autowired
    private IShopData shopData;
    
    @GetMapping("/entity/number")
    public <T>ResponseEntity<T> getResponseByRecordType(@RequestParam(name="recordType") RecordType recordType, @Requestparam(name = "data", required = false) boolean data) throws Exception{
        T response = shopData.getNumber(recordType, data );
        return ResponseEntity.ok().body(response);
    }
}

在@GetMaps("/entity/number")公共响应实体getContseByRecordType

第一个参数(两个参数都来自swagger-用户界面)是String而不是RecordType


共1个答案

匿名用户

试试下面。

@RequestParam(name="recordType") @Schema(allowableValues = {"BILL", "LAW" }) RecordType recordType