- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用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/
在 Tomcat 6/Ubuntu 12.04 上启动 Grails 2.1.0 应用程序时出现以下错误。 Error 500 - Internal Server Error. groovy.lang
在运行 Storm 拓扑时,我收到此错误。拓扑完美运行 5 分钟,没有任何错误,然后失败。我正在使用 Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS as 300 sec i
我有一个 jsp 代码在其中一台机器上运行良好。但是当我复制到另一台机器时,我得到了这个 no such method found 异常。我是 Spring 的新手。有人可以解释我错过了什么吗? 以下
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我的代码在下面给出了一个错误; Exception in thread "main" java.lang.NoSuchMethodError: com/myApp/Client.cypherCBC(L
我正在尝试一个 Restful web 服务示例,所以当我要访问 url 时,我遇到了异常 java.lang.NoSuchMethodError: jersey.repackaged.com.goo
我正在将一个 Spring web 项目转换为一个 Maven 项目,但我收到了这个错误: java.lang.NoSuchMethodError: org.jboss.logging.Logger.
在我的项目中,我有一个像这样的枚举: public enum MyEnum { FIRST(1), SECOND(2); private int value; private MyEnum(int v
我创建了这个简单的示例,用于读取 Linux 正常运行时间: public String getMachineUptime() throws IOException { String[] di
我正在使用 Eclipse,并且正在使用 Java。我的目标是使用 bogoSort 方法对 vector 进行排序在一个 vector (vectorExample)中适应我的 vector 类型,
我正在运行以下查询。它显示一条错误消息。如何解决这个错误? ListrouteList=null; List companyList = session.createS
我有以下模型类: @Entity @Table(name="user_content") @org.hibernate.annotations.NamedQueries({ @org.
我有那个错误。这是我的代码: GmailSettingsService service = new GmailSettingsService(APPLICATION_NAME, DOMAIN_NAME
实际上我在执行我的java程序时遇到了下面提到的错误 Exception in thread "pool-1-thread-1" java.lang.ClassCastException: jav
java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.String 我在以下代码中遇到此异常: Strin
我正在尝试从 linkedhashset 中检索随机元素。下面是我的代码,但它每次都给我异常。 private static void generateRandomUserId(Set userIds
我已经完成了 Android 中的代码: List spinnerArray = new ArrayList(); for (int i = 0; i item = (LinkedTreeMap)
这个问题已经有答案了: Explanation of ClassCastException in Java (12 个回答) 已关闭 6 年前。 我已经编写了 java 到 Json 的代码,同时从页
这个问题在这里已经有了答案: ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn (4 个答案) 关闭 6 年前
我在运行时遇到问题来编译这段代码,这给我一个错误,java.lang.Integer 无法转换为 Java.lang.Double。如果有人帮助我更正此代码,我将非常高兴 double x; pu
我是一名优秀的程序员,十分优秀!