Динамический тип каста Spring pathvariable - PullRequest
0 голосов
/ 01 июня 2018

Я планирую создать один простой проект Spring Rest Service с JPA, который будет извлекать детали из базы данных на основе имени объекта и идентификатора объекта, указанных в переменных пути.

рассмотрите следующий код.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ds.dao.EntityDAO;
import com.ds.entities.Employees;

import javax.persistence.Entity;
@Controller
@RequestMapping("/")
public class DynaRestController {

@Autowired
EntityDAO entityDAO;


@RequestMapping(value = "{entityName}/{enityId}",method = RequestMethod.GET)
public @ResponseBody Object getEntity(@PathVariable("entityName") String entityName,@PathVariable("enityId") Object id) {
    return entityDAO.getEntityById(entityName, id);
}

}

Сущность DAO Class

public class EntityDAO {
@Autowired
EntityManager entityManager;


public Object getEntityById(String entityName, Object id) {
    EntityType<?> entityType = getEntityByName(entityName);
    Object idcasted = entityType.getIdType().getJavaType().cast(id);
    System.out.println(idcasted.getClass().getName());
    Object entity = entityManager.find(entityType.getJavaType(), idcasted);
    System.out.println("Entity.. Name .." + entityName);
    // Employees entity = session.load(Employees.class, id);
    return entity;

}

private EntityType<?> getEntityByName(String name) {
    Set<EntityType<?>> entities = entityManager.getMetamodel().getEntities();
    for (Iterator<EntityType<?>> iterator = entities.iterator(); iterator.hasNext();) {
        EntityType<?> entityType = (EntityType<?>) iterator.next();
        if (entityType.getName().equals(name))
            return entityType;
    }

    return null;
}

}

Employe Class

@Configurable
@Entity
@Table(name = "employees", catalog = "employees")
public class Employees implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private int empNo;
private Date birthDate;
private String firstName;
private String lastName;
private String gender;
private Date hireDate;
private Set<Titles> titleses = new HashSet<Titles>(0);
private Set<Salaries> salarieses = new HashSet<Salaries>(0);
private Set<DeptEmp> deptEmps = new HashSet<DeptEmp>(0);
private Set<DeptManager> deptManagers = new HashSet<DeptManager>(0);

public Employees() {
}

public Employees(int empNo, Date birthDate, String firstName, String lastName, String gender, Date hireDate) {
    this.empNo = empNo;
    this.birthDate = birthDate;
    this.firstName = firstName;
    this.lastName = lastName;
    this.gender = gender;
    this.hireDate = hireDate;
}

public Employees(int empNo, Date birthDate, String firstName, String lastName, String gender, Date hireDate,
        Set<Titles> titleses, Set<Salaries> salarieses, Set<DeptEmp> deptEmps, Set<DeptManager> deptManagers) {
    this.empNo = empNo;
    this.birthDate = birthDate;
    this.firstName = firstName;
    this.lastName = lastName;
    this.gender = gender;
    this.hireDate = hireDate;
    this.titleses = titleses;
    this.salarieses = salarieses;
    this.deptEmps = deptEmps;
    this.deptManagers = deptManagers;
}

@Id
@Column(name = "emp_no", unique = true, nullable = false)
public int getEmpNo() {
    return this.empNo;
}

public void setEmpNo(int empNo) {
    this.empNo = empNo;
}

@Temporal(TemporalType.DATE)
@Column(name = "birth_date", nullable = false, length = 10)
public Date getBirthDate() {
    return this.birthDate;
}

public void setBirthDate(Date birthDate) {
    this.birthDate = birthDate;
}

@Column(name = "first_name", nullable = false, length = 14)
public String getFirstName() {
    return this.firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

@Column(name = "last_name", nullable = false, length = 16)
public String getLastName() {
    return this.lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

@Column(name = "gender", nullable = false, length = 2)
public String getGender() {
    return this.gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

@Temporal(TemporalType.DATE)
@Column(name = "hire_date", nullable = false, length = 10)
public Date getHireDate() {
    return this.hireDate;
}

public void setHireDate(Date hireDate) {
    this.hireDate = hireDate;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "employees")
public Set<Titles> getTitleses() {
    return this.titleses;
}

public void setTitleses(Set<Titles> titleses) {
    this.titleses = titleses;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "employees")
public Set<Salaries> getSalarieses() {
    return this.salarieses;
}

public void setSalarieses(Set<Salaries> salarieses) {
    this.salarieses = salarieses;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "employees")
@JsonBackReference
public Set<DeptEmp> getDeptEmps() {
    return this.deptEmps;
}

public void setDeptEmps(Set<DeptEmp> deptEmps) {
    this.deptEmps = deptEmps;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "employees")
public Set<DeptManager> getDeptManagers() {
    return this.deptManagers;
}

public void setDeptManagers(Set<DeptManager> deptManagers) {
    this.deptManagers = deptManagers;
}

}

Когда я динамически приводим переменную пути с помощью следующего кода

Object idcasted = entityType.getIdType().getJavaType().cast(id);
Object entity = entityManager.find(entityType.getJavaType(), idcasted);

, он выдает ClassCastExpcetion

java.lang.ClassCastException: Невозможно привести java.lang.String.int в java.lang.Class.cast (Class.java:3369) ~ [na: 1.8.0_112] в com.techm.att.ds.dao.EntityDAO.getEntityById (EntityDAO.java:33) ~ [classes /: na] at com.techm.att.ds.dao.EntityDAO $$ FastClassBySpringCGLIB $$ 8e64d745.invoke () ~ [classes /: na] в org.springframework.cglib.proxy.MethodProxy.invoke (MethodProxy.java:20) ~ [spring-core-4.3.13.RELEASE.jar: 4.3.13.RELEASE] в org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint (CglibAopProxy.java:738) ~ [spring-aop-4.313.RELEASE.jar: 4.3.13.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (ReflectiveMethodInvocation.java:157) ~ [spring-aop-4.3.13.RELEASE.jar: 4.3.13.RELEASE] at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke (PersistenceExceptionTranslationInterceptor.java:136) ~ [spring-tx-4.3.13.RELEASE.jar: 4.3.13.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (ReflectiveMethodInvocation.java:179) ~ [spring-aop-4.3.13.RELEASE.jar: 4.3.13.RELEASE]

любая помощьбыть очень ценным ..

1 Ответ

0 голосов
/ 05 июня 2018

Я пишу вам простой пример с комментариями.Это то же самое поведение.Ваш RestController фактически получает строку:

  public static void main(String[] args) {
    Object myString = "myString";
    System.out.println(myString.getClass()); // class java.lang.String
    int.class.cast(myString);
}

Метод приведения проверяет экземпляр заданного вами значения, и он не работает:

 public T cast(Object obj) {
    if (obj != null && !isInstance(obj))
        throw new ClassCastException(cannotCastMsg(obj));
    return (T) obj;
}
...