当前位置: 首页 > 知识库问答 >
问题:

无法将“java.lang.String”的值转换为必需的类型“My Enum”

郑茂材
2023-03-14

我正在使用Spring的BeanPropertyRowMapper将来自数据库的行映射到我的Bean类Employee,枚举有两种类型,Active和inactive。当beanpropertyrowmapper尝试将来自数据库的字符串转换为我的枚举时,它会失败。(Enum在类中是empStatus作为枚举,并在数据库中声明为字符串)。

我不想为每个服务编写自定义行映射器,我需要一些映射相关类枚举的通用解决方案。这是我的员工课。

package com.x2iq.attendance.generated.server.model;

    import java.util.Objects;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.annotation.JsonCreator;
    import com.fasterxml.jackson.annotation.JsonValue;
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import javax.validation.Valid;
    import javax.validation.constraints.*;

    /**
     * A model will be used for Employee
     */
    @ApiModel(description = "A model will be used for Employee")
    @javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2018-10-26T09:58:09.954+05:00")

    public class Employee   {
      @JsonProperty("ID")
      private Integer ID = null;

      @JsonProperty("name")
      private String name = null;

      @JsonProperty("refIDDesignation")
      private Integer refIDDesignation = null;

      @JsonProperty("refIDTeam")
      private Integer refIDTeam = null;

      /**
       * Gets or Sets empStatus
       */
      public enum EmpStatusEnum {
        ACTIVE("Active"),

        INACTIVE("Inactive");

        private String value;

        EmpStatusEnum(String value) {
          this.value = value;
        }

        @Override
        @JsonValue
        public String toString() {
          return String.valueOf(value);
        }

        @JsonCreator
        public static EmpStatusEnum fromValue(String text) {
          for (EmpStatusEnum b : EmpStatusEnum.values()) {
            if (String.valueOf(b.value).equals(text)) {
              return b;
            }
          }
          return null;
        }
      }

      @JsonProperty("empStatus")
      private EmpStatusEnum empStatus = null;

      @JsonProperty("empDateCreated")
      private String empDateCreated = null;

      @JsonProperty("leavingDate")
      private String leavingDate = null;

      @JsonProperty("teamLead")
      private String teamLead = null;

      @JsonProperty("email")
      private String email = null;

      @JsonProperty("tel")
      private String tel = null;

      @JsonProperty("emergencyTel")
      private String emergencyTel = null;

      @JsonProperty("emergencyContactName")
      private String emergencyContactName = null;

      @JsonProperty("emergencyContactRelation")
      private String emergencyContactRelation = null;

      @JsonProperty("qualification")
      private String qualification = null;

      @JsonProperty("shift")
      private String shift = null;

      @JsonProperty("profilePicture")
      private String profilePicture = null;

      @JsonProperty("checkinTime")
      private String checkinTime = null;

      @JsonProperty("checkoutTime")
      private String checkoutTime = null;

      @JsonProperty("desigTitle")
      private String desigTitle = null;

      @JsonProperty("title")
      private String title = null;

      public Employee ID(Integer ID) {
        this.ID = ID;
        return this;
      }

       /**
       * Get ID
       * @return ID
      **/
      @ApiModelProperty(value = "")


      public Integer getID() {
        return ID;
      }

      public void setID(Integer ID) {
        this.ID = ID;
      }

      public Employee name(String name) {
        this.name = name;
        return this;
      }

       /**
       * Get name
       * @return name
      **/
      @ApiModelProperty(value = "")


      public String getName() {
        return name;
      }

      public void setName(String name) {
        this.name = name;
      }

      public Employee refIDDesignation(Integer refIDDesignation) {
        this.refIDDesignation = refIDDesignation;
        return this;
      }

       /**
       * Get refIDDesignation
       * @return refIDDesignation
      **/
      @ApiModelProperty(value = "")


      public Integer getRefIDDesignation() {
        return refIDDesignation;
      }

      public void setRefIDDesignation(Integer refIDDesignation) {
        this.refIDDesignation = refIDDesignation;
      }

      public Employee refIDTeam(Integer refIDTeam) {
        this.refIDTeam = refIDTeam;
        return this;
      }

       /**
       * Get refIDTeam
       * @return refIDTeam
      **/
      @ApiModelProperty(value = "")


      public Integer getRefIDTeam() {
        return refIDTeam;
      }

      public void setRefIDTeam(Integer refIDTeam) {
        this.refIDTeam = refIDTeam;
      }

      public Employee empStatus(EmpStatusEnum empStatus) {
        this.empStatus = empStatus;
        return this;
      }

       /**
       * Get empStatus
       * @return empStatus
      **/
      @ApiModelProperty(value = "")


      public EmpStatusEnum getEmpStatus() {
        return empStatus;
      }

      public void setEmpStatus(EmpStatusEnum empStatus) {
        this.empStatus = empStatus;
      }

      public Employee empDateCreated(String empDateCreated) {
        this.empDateCreated = empDateCreated;
        return this;
      }

       /**
       * Get empDateCreated
       * @return empDateCreated
      **/
      @ApiModelProperty(value = "")


      public String getEmpDateCreated() {
        return empDateCreated;
      }

      public void setEmpDateCreated(String empDateCreated) {
        this.empDateCreated = empDateCreated;
      }

      public Employee leavingDate(String leavingDate) {
        this.leavingDate = leavingDate;
        return this;
      }

       /**
       * Get leavingDate
       * @return leavingDate
      **/
      @ApiModelProperty(value = "")


      public String getLeavingDate() {
        return leavingDate;
      }

      public void setLeavingDate(String leavingDate) {
        this.leavingDate = leavingDate;
      }

      public Employee teamLead(String teamLead) {
        this.teamLead = teamLead;
        return this;
      }

       /**
       * Get teamLead
       * @return teamLead
      **/
      @ApiModelProperty(value = "")


      public String getTeamLead() {
        return teamLead;
      }

      public void setTeamLead(String teamLead) {
        this.teamLead = teamLead;
      }

      public Employee email(String email) {
        this.email = email;
        return this;
      }

       /**
       * Get email
       * @return email
      **/
      @ApiModelProperty(value = "")


      public String getEmail() {
        return email;
      }

      public void setEmail(String email) {
        this.email = email;
      }

      public Employee tel(String tel) {
        this.tel = tel;
        return this;
      }

       /**
       * Get tel
       * @return tel
      **/
      @ApiModelProperty(value = "")


      public String getTel() {
        return tel;
      }

      public void setTel(String tel) {
        this.tel = tel;
      }

      public Employee emergencyTel(String emergencyTel) {
        this.emergencyTel = emergencyTel;
        return this;
      }

       /**
       * Get emergencyTel
       * @return emergencyTel
      **/
      @ApiModelProperty(value = "")


      public String getEmergencyTel() {
        return emergencyTel;
      }

      public void setEmergencyTel(String emergencyTel) {
        this.emergencyTel = emergencyTel;
      }

      public Employee emergencyContactName(String emergencyContactName) {
        this.emergencyContactName = emergencyContactName;
        return this;
      }

       /**
       * Get emergencyContactName
       * @return emergencyContactName
      **/
      @ApiModelProperty(value = "")


      public String getEmergencyContactName() {
        return emergencyContactName;
      }

      public void setEmergencyContactName(String emergencyContactName) {
        this.emergencyContactName = emergencyContactName;
      }

      public Employee emergencyContactRelation(String emergencyContactRelation) {
        this.emergencyContactRelation = emergencyContactRelation;
        return this;
      }

       /**
       * Get emergencyContactRelation
       * @return emergencyContactRelation
      **/
      @ApiModelProperty(value = "")


      public String getEmergencyContactRelation() {
        return emergencyContactRelation;
      }

      public void setEmergencyContactRelation(String emergencyContactRelation) {
        this.emergencyContactRelation = emergencyContactRelation;
      }

      public Employee qualification(String qualification) {
        this.qualification = qualification;
        return this;
      }

       /**
       * Get qualification
       * @return qualification
      **/
      @ApiModelProperty(value = "")


      public String getQualification() {
        return qualification;
      }

      public void setQualification(String qualification) {
        this.qualification = qualification;
      }

      public Employee shift(String shift) {
        this.shift = shift;
        return this;
      }

       /**
       * Get shift
       * @return shift
      **/
      @ApiModelProperty(value = "")


      public String getShift() {
        return shift;
      }

      public void setShift(String shift) {
        this.shift = shift;
      }

      public Employee profilePicture(String profilePicture) {
        this.profilePicture = profilePicture;
        return this;
      }

       /**
       * Get profilePicture
       * @return profilePicture
      **/
      @ApiModelProperty(value = "")


      public String getProfilePicture() {
        return profilePicture;
      }

      public void setProfilePicture(String profilePicture) {
        this.profilePicture = profilePicture;
      }

      public Employee checkinTime(String checkinTime) {
        this.checkinTime = checkinTime;
        return this;
      }

       /**
       * Get checkinTime
       * @return checkinTime
      **/
      @ApiModelProperty(value = "")


      public String getCheckinTime() {
        return checkinTime;
      }

      public void setCheckinTime(String checkinTime) {
        this.checkinTime = checkinTime;
      }

      public Employee checkoutTime(String checkoutTime) {
        this.checkoutTime = checkoutTime;
        return this;
      }

       /**
       * Get checkoutTime
       * @return checkoutTime
      **/
      @ApiModelProperty(value = "")


      public String getCheckoutTime() {
        return checkoutTime;
      }

      public void setCheckoutTime(String checkoutTime) {
        this.checkoutTime = checkoutTime;
      }

      public Employee desigTitle(String desigTitle) {
        this.desigTitle = desigTitle;
        return this;
      }

       /**
       * Get desigTitle
       * @return desigTitle
      **/
      @ApiModelProperty(value = "")


      public String getDesigTitle() {
        return desigTitle;
      }

      public void setDesigTitle(String desigTitle) {
        this.desigTitle = desigTitle;
      }

      public Employee title(String title) {
        this.title = title;
        return this;
      }

       /**
       * Get title
       * @return title
      **/
      @ApiModelProperty(value = "")


      public String getTitle() {
        return title;
      }

      public void setTitle(String title) {
        this.title = title;
      }


      @Override
      public boolean equals(java.lang.Object o) {
        if (this == o) {
          return true;
        }
        if (o == null || getClass() != o.getClass()) {
          return false;
        }
        Employee employee = (Employee) o;
        return Objects.equals(this.ID, employee.ID) &&
            Objects.equals(this.name, employee.name) &&
            Objects.equals(this.refIDDesignation, employee.refIDDesignation) &&
            Objects.equals(this.refIDTeam, employee.refIDTeam) &&
            Objects.equals(this.empStatus, employee.empStatus) &&
            Objects.equals(this.empDateCreated, employee.empDateCreated) &&
            Objects.equals(this.leavingDate, employee.leavingDate) &&
            Objects.equals(this.teamLead, employee.teamLead) &&
            Objects.equals(this.email, employee.email) &&
            Objects.equals(this.tel, employee.tel) &&
            Objects.equals(this.emergencyTel, employee.emergencyTel) &&
            Objects.equals(this.emergencyContactName, employee.emergencyContactName) &&
            Objects.equals(this.emergencyContactRelation, employee.emergencyContactRelation) &&
            Objects.equals(this.qualification, employee.qualification) &&
            Objects.equals(this.shift, employee.shift) &&
            Objects.equals(this.profilePicture, employee.profilePicture) &&
            Objects.equals(this.checkinTime, employee.checkinTime) &&
            Objects.equals(this.checkoutTime, employee.checkoutTime) &&
            Objects.equals(this.desigTitle, employee.desigTitle) &&
            Objects.equals(this.title, employee.title);
      }

      @Override
      public int hashCode() {
        return Objects.hash(ID, name, refIDDesignation, refIDTeam, empStatus, empDateCreated, leavingDate, teamLead, email, tel, emergencyTel, emergencyContactName, emergencyContactRelation, qualification, shift, profilePicture, checkinTime, checkoutTime, desigTitle, title);
      }

      @Override
      public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("class Employee {\n");

        sb.append("    ID: ").append(toIndentedString(ID)).append("\n");
        sb.append("    name: ").append(toIndentedString(name)).append("\n");
        sb.append("    refIDDesignation: ").append(toIndentedString(refIDDesignation)).append("\n");
        sb.append("    refIDTeam: ").append(toIndentedString(refIDTeam)).append("\n");
        sb.append("    empStatus: ").append(toIndentedString(empStatus)).append("\n");
        sb.append("    empDateCreated: ").append(toIndentedString(empDateCreated)).append("\n");
        sb.append("    leavingDate: ").append(toIndentedString(leavingDate)).append("\n");
        sb.append("    teamLead: ").append(toIndentedString(teamLead)).append("\n");
        sb.append("    email: ").append(toIndentedString(email)).append("\n");
        sb.append("    tel: ").append(toIndentedString(tel)).append("\n");
        sb.append("    emergencyTel: ").append(toIndentedString(emergencyTel)).append("\n");
        sb.append("    emergencyContactName: ").append(toIndentedString(emergencyContactName)).append("\n");
        sb.append("    emergencyContactRelation: ").append(toIndentedString(emergencyContactRelation)).append("\n");
        sb.append("    qualification: ").append(toIndentedString(qualification)).append("\n");
        sb.append("    shift: ").append(toIndentedString(shift)).append("\n");
        sb.append("    profilePicture: ").append(toIndentedString(profilePicture)).append("\n");
        sb.append("    checkinTime: ").append(toIndentedString(checkinTime)).append("\n");
        sb.append("    checkoutTime: ").append(toIndentedString(checkoutTime)).append("\n");
        sb.append("    desigTitle: ").append(toIndentedString(desigTitle)).append("\n");
        sb.append("    title: ").append(toIndentedString(title)).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    ");
      }
    }

例外情况:

Employee类是由以下swagger2.0规范生成的,所以我不能更改Employee类。这是我对employee类的规范

Employee:
      description: A model will be used for Employee
      type: object
      properties:
         ID:
           type: integer
         name:
           type: string
         refIDDesignation:
           type: integer
         refIDTeam:
           type: integer
         empStatus:
           type: string
           enum: [Active, Inactive]
         empDateCreated:
              type: string
              format: YYYY-MM-DD
         leavingDate:
           type: string
           format: YYYY-MM-DD
         teamLead:
           type: string
         email:
           type: string
         tel:
           type: string
         emergencyTel:
             type: string
         emergencyContactName:
             type: string
         emergencyContactRelation:
             type: string
         qualification:
             type: string
         shift:
             type: string
             enum: [Morning, Evening]
         profilePicture:
             type: string
         checkinTime:
             type: string
         checkoutTime:
            type: string
         desigTitle:
            type: string
         title:
            type: string

共有1个答案

壤驷睿
2023-03-14

如果不想将列类型更改为enum,则应使用enumtype.string

@Enumerated(EnumType.STRING)
//@Column(name = "name of the column to map with")
@JsonProperty("empStatus")
private EmpStatusEnum empStatus;
 类似资料:
  • 这是控制器代码部分: 我收到这条消息: 出现错误(类型=错误请求,状态=400)。无法将类型[java.lang.String]的值转换为所需类型[java.util.Date];嵌套异常为org.springframework.core.convert.conversionfailedexception:无法将值“Wed Jun 08 00:00:00 WET 2016”从类型[java.lan

  • 我有两个实体,它们使用一个主键互相引用,主键是一个实体的整数。我不确定我做这件事的方式是否正确。 下面是引用主键id为int的实体 下面是我们从上面的实体中将外键设置为Kmichango kandaMchango的实体。 这里是表单的一部分,我在这里提交了用户在jumuiya_michango_form.html中提供的数据 下面是我的控制器中用于链接到表单和发布数据的两个方法 在我提交表单后,我

  • 我正在关注Spring in Action 5,在按下提交按钮后创建Taco模型时遇到问题。这是我的设计Taco控制器类: 以及我捕获的错误消息: 炸玉米饼实体如下所示: 以及我的配料实体: 这是一个html页面,必须使用所选成分创建新的Taco对象: 我该怎么修理它?谢谢你的预付款。

  • 我正试图通过提供JSON格式的所需请求来使用postman来访问一个服务: 邮递员屏幕 但我得到了一个错误: “无法将类型”java.lang.String“的值转换为所需的类型”SampleRequestObject“;嵌套异常为java.lang.IllegalStateException:无法将类型[java.lang.String]的值转换为所需类型“ 控制器 SampleRequestO

  • 问题内容: 我有一个JSON文件,其中包含2个JSON数组:一个用于路线的数组,一个用于景点的数组。 一条路线应由用户导航到的多个景点组成。不幸的是我遇到了错误: JSONException:无法将类型为java.lang.String的值转换为JSONObject 这是我的变量和解析JSON文件的代码: Log.i(“ JSON Parser”,json); 告诉我在生成的字符串的开头有一个奇怪

  • 我是Spring MVC的新手,试图构建一个示例应用程序,我试图将数据保存到一个名为EmployeeDetails的表中,该表与表用户有一对一的关系。提交表单后,我在字段“user”的对象“employee details”中得到了这个错误字段:rejected value[Users];代码[typemismatch.employeeDetails.user,typemismatch.user,