gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:05:24 26 4
gpt4 key购买 nike

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

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

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

异常(exception):

org.springframework.beans.TypeMismatchException: Failed to convert
property value of type 'java.lang.String' to required type
'com.x2iq.attendance.generated.server.model.Employee$EmpStatusEnum'
for property 'empStatus'; nested exception is
org.springframework.core.convert.ConversionFailedException: Failed to
convert from type [java.lang.String] to type
[@io.swagger.annotations.ApiModelProperty
@com.fasterxml.jackson.annotation.JsonProperty
com.x2iq.attendance.generated.server.model.Employee$EmpStatusEnum] for
value 'Active'; nested exception is
java.lang.IllegalArgumentException: No enum constant
com.x2iq.attendance.generated.server.model.Employee.EmpStatusEnum.Active
at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:598)
at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:615)
at org.springframework.beans.AbstractNestablePropertyAccessor.processLocalProperty(AbstractNestablePropertyAccessor.java:462)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:290)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:258)
at com.x2iq.developerUtils.CustomBeanPropertyRowMapper.mapRow(CustomBeanPropertyRowMapper.java:280)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:93)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:60)
at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:703)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:639)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:690)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:722)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:732)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:787)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSite.invoke(PojoMetaMethodSite.java:192)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:141)
at com.x2iq.attendance.employee.EmployeeRepo.getEmployeeById(EmployeeRepo.groovy:75)
at com.x2iq.attendance.employee.EmployeeRepo$$FastClassBySpringCGLIB$$4831dba.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java: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(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
at com.x2iq.attendance.employee.EmployeeRepo$$EnhancerBySpringCGLIB$$f5cc9901.getEmployeeById(<generated>)
at com.x2iq.attendance.employee.EmployeeService.getEmployeeById(EmployeeService.java:51)
at com.x2iq.attendance.employee.EmployeeController.getEmployeeById(EmployeeController.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain and more

Employee类是由以下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;

关于java - 无法将 'java.lang.String' 的值转换为所需类型 'myEnum',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53002077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com