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

时间:2018-10-26 05:34:03

标签: java spring hibernate spring-boot jpa

我正在使用Spring的 BeanPropertyRowMapper 将来自数据库的行映射到我的Bean Class Employee,Enum有2种类型,活动的和不活动的。 当beanpropertyrowmapper尝试将来自数据库的String转换为我的Enum时,它将失败。 (枚举在类中作为empStatus属于empStatus,并且在数据库中声明为String)。

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

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    ");
      }
    }

例外:

  

org.springframework.beans.TypeMismatchException:转换失败   类型'java.lang.String'的属性值转换为必需的类型   'com.x2iq.attendance.genic.server.model.Employee $ EmpStatusEnum'   对于属性“ empStatus”;嵌套异常为   org.springframework.core.convert.ConversionFailedException:失败   从类型[java.lang.String]转换为类型   [@ io.swagger.annotations.ApiModelProperty   @ com.fasterxml.jackson.annotation.JsonProperty   com.x2iq.attendance.generation.server.model.Employee $ EmpStatusEnum]用于   值“有效”;嵌套异常为   java.lang.IllegalArgumentException:没有枚举常量   com.x2iq.attendance.generation.server.model.Employee.EmpStatusEnum.Active         在org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:598)         在org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:615)         在org.springframework.beans.AbstractNestablePropertyAccessor.processLocalProperty(AbstractNestablePropertyAccessor.java:462)         在org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:290)         在org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:258)         在com.x2iq.developerUtils.CustomBeanPropertyRowMapper.mapRow(CustomBeanPropertyRowMapper.java:280)         在org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:93)         在org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:60)         在org.springframework.jdbc.core.JdbcTemplate $ 1.doInPreparedStatement(JdbcTemplate.java:703)         在org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:639)         在org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:690)         在org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:722)         在org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:732)         在org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:787)         在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处         在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)         在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)         在java.lang.reflect.Method.invoke(Method.java:498)         在org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite $ PojoCachedMethodSite.invoke(PojoMetaMethodSite.java:192)处         在org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)         在org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)         在org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)         在org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:141)         在com.x2iq.attendance.employee.EmployeeRepo.getEmployeeById(EmployeeRepo.groovy:75)         在com.x2iq.attendance.employee.EmployeeRepo $$ FastClassBySpringCGLIB $$ 4831dba.invoke()         在org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)         在org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)         在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)         在org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)         在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)         在org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)         在com.x2iq.attendance.employee.EmployeeRepo $$ EnhancerBySpringCGLIB $$ f5cc9901.getEmployeeById()         在com.x2iq.attendance.employee.EmployeeService.getEmployeeById(EmployeeService.java:51)         在com.x2iq.attendance.employee.EmployeeController.getEmployeeById(EmployeeController.java:42)         在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处         在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)         在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)         在java.lang.reflect.Method.invoke(Method.java:498)         在org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)         在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)         在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)         在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)         在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)         在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)         在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)         在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)         在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)         在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)         在javax.servlet.http.HttpServlet.service(HttpServlet.java:635)         在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)         在javax.servlet.http.HttpServlet.service(HttpServlet.java:742)         在org.apache.catalina.core.ApplicationFilterChain等处

Employee Class是由以下 Swagger2.0 规范生成的,因此我无法更改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 个答案:

答案 0 :(得分:0)

如果您不想将列类型更改为enum,则应使用EnumType.String

@Enumerated(EnumType.STRING)
//@Column(name = "name of the column to map with")
@JsonProperty("empStatus")
private EmpStatusEnum empStatus;