簡體   English   中英

無法將“ java.lang.String”的值轉換為所需的類型“ myEnum”

[英]Failed to convert value of 'java.lang.String' to required type 'myEnum'

我正在使用Spring的BeanPropertyRowMapper來將數據庫中的行映射到我的Bean類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”的屬性值轉換為屬性“ empStatus”的必需類型“ com.x2iq.attendance.generation.server.model.Employee $ EmpStatusEnum”; 嵌套異常是org.springframework.core.convert.ConversionFailedException:無法從[java.lang.String]類型轉換為[@ io.swagger.annotations.ApiModelProperty @ com.fasterxml.jackson.annotation.JsonProperty com.x2iq。值“有效”的Attenance.generation.server.model.Employee $ EmpStatusEnum]; 嵌套的異常是java.lang.IllegalArgumentException:在org.springframework上org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:598)處沒有枚舉常量com.x2iq.attendance.generation.server.model.Employee.EmpStatusEnum.Active .beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:615)在org.springframework.beans.AbstractNestablePropertyAccessor.processLocalProperty(AbstractNestablePropertyAccessor.java:462)在org.springframework.beans.AbstractNestablePropertyor。 com.x2iq.developerUtils.CustomBeanPropertyRowMapper.mapRow(CustomBeanPropertyRowMapper.java:280)上的springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:258)在org.springframeworksetextractor.org在org.springframework.jdb c.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:60)位於org.springframework.jdbc.core.JdbcTemplate $ 1.doInPreparedStatement(JdbcTemplate.java:703)位於org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate。 :639)位於org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:690),位於org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:722)org.springframework.jdbc.core。 org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:787)的JdbcTemplate.query(JdbcTemplate.java:732)在sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)處在sun.reflect.NativeMethodAccessorImpl.invoke(Native Method) org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite上的java.lang.reflect.Method.invoke(Method.java:498)上的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)上的NativeMethodAccessorImpl.java:62) org.codehaus.groovy.runtime.callsite.PojoM上的$ PojoCachedMethodSite.invoke(PojoMetaMethodSite.java:192) org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)的etaMethodSite.call(PojoMetaMethodSite.java:56)org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java: 113)在com.x2iq.attendance.employee.EmployeeRepo.getEmployeeById(EmployeeRepo.groovy:75)在org.org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:141) .EmployeeRepo $$ FastClassBySpringCGLIB $ 4831dba.invoke()在org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)在org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy。 738)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)at org.springframework.aop.framework.ReflectiveMethodInvocation .proceed(Reflec tiveMethodInvocation.java:179),位於org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673),位於com.x2iq.attendance.employee.EmployeeRepo $$ EnhancerBySpringCGLIB $$ f5cc9901(getEmployeeBy)。位於sun的x2iq.attendance.employee.EmployeeService.getEmployeeById(EmployeeService.java:51)位於com.x2iq.attendance.employee.EmployeeController.getEmployeeById(EmployeeController.java:42)位於sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)。在org.springframework.web上的java.lang.reflect.Method.invoke(Method.java:498)上的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)上的reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)上的.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)在org.springframework.web.servlet.mvc.method.annotation .ServletInvocab org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)的leHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)在org.springframework.web.servlet.mvc.method.annotation處。 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java :)上的org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)處的RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) 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.se org.apache.catalina.core.ApplicationFilterChain上的rvlet.http.HttpServlet.service(HttpServlet.java:742)等

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

如果您不想將列類型更改為enum則應使用EnumType.String

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM