【发布时间】:2021-09-25 04:34:33
【问题描述】:
休息控制器 ...
@GET
@Path("/employee")
@Produces(MediaType.APPLICATION_JSON)
public String loginEmployee() {
Transaction transaction=null;
Session session=null;
session=HibernateUtil.getSessionFactory().openSession();
transaction=session.beginTransaction();
Employee e=new Employee();
ObjectMapper mapper=new ObjectMapper();
TypedQuery<Employee> query=session.createQuery("FROM Employee ", Employee.class);
List<Employee> list=query.getResultList();
transaction.commit();
try {
System.out.println(list);
return mapper.writeValueAsString("Success");
} catch (JsonProcessingException e1) {
// TODO Auto-generated catch block
System.out.println(e1);
}
return "Failed";
}
... 休眠 ...
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
try {
Configuration configuration = new Configuration();
Properties settings = new Properties();
settings.put(Environment.DRIVER,
"org.postgresql.Driver");
settings.put(Environment.URL, URL);
settings.put(Environment.USER, USERNAME);
settings.put(Environment.PASS, PASSWORD);
settings.put(Environment.DIALECT,
"org.hibernate.dialect.PostgreSQLDialect");
settings.put(Environment.SHOW_SQL, "true");
settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS,
"thread");
settings.put(Environment.HBM2DDL_AUTO, "update");
configuration.setProperties(settings);
configuration.addAnnotatedClass(BaseTable.class);
configuration.addAnnotatedClass(Employee.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}catch (Exception e) {
e.printStackTrace();
}
}
return sessionFactory;
}
}
... 员工 ...
@Entity
public class Employee extends BaseTable implements EmployeeDAO {
@OneToMany(cascade=CascadeType.ALL, mappedBy = "employee")
/* public Employee(String first_name, String last_name, String username, String
password, double salary, String email,
String phonenum) {
super(first_name,last_name,username,password,salary,email,
phonenum);
}
public Employee(){}
*/
private List<SupportCase> cases=new ArrayList<>();
...
...
}
... 基类 ...
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class BaseTable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private int id;
@Column(name="firstname")
private String firstName;
@Column(name="lastname")
private String lastName;
@Column(name="username")
private String username;
@Column(name="password")
private String password;
@Column(name ="salary")
private double salary;
@Column(name="email")
private String email;
@Column(name="phonenum")
private String phonenum;
protected BaseTable(String first_name, String last_name, String username, String
password, double salary, String email,
String phonenum) {
this.firstName = first_name;
this.lastName = last_name;
this.username = username;
this.password = password;
this.salary = salary;
this.email = email;
this.phonenum = phonenum;
}
public BaseTable() {}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirst_name() {
return firstName;
}
public void setFirstName(String first_name) {
this.firstName = first_name;
}
public String getLastName() {
return lastName;
}
public void setLast_name(String last_name) {
this.lastName = last_name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhonenum() {
return phonenum;
}
public void setPhonenum(String phonenum) {
this.phonenum = phonenum;
}
@Override
public String toString() {
return "BaseTable {id=" + id + ", first_name=" + firstName + ", last_name=" +
lastName + ", username="
+ username + ", password=" + password + ", salary=" + salary + ", email=" +
email + ", phonenum="
+ phonenum + "}";
}
}
... 输出 ...
休眠:选择employee0_.id 作为id1_0_,employee0_.email 作为email2_0_,employee0_.firstname 作为firstnam3_0_,employee0_.lastname 作为lastname4_0_,employee0_.password 作为password5_0_,employee0_.phonenum 作为phonenum6_0_,employee0_.salary 作为salary7_0_ ,employee0_.username 作为来自员工employee0_ 的username8_0_ [BaseTable {id=122,first_name=Sri,last_name=Va,username=csfwea,password=afdcfc,salary=10000.0,email=abc@gmail.com,phonenum=123456789},BaseTable {id=124,first_name=Sri,姓=Va,用户名=csfwea,密码=afdcfc,薪水=10000.0,电子邮件=abc@gmail.com,电话号码=123456789}] 休眠:选择 case0_.id 作为 id6_2_0_,case0_.sid 作为 sid1_2_0_,case0_.sid 作为 sid1_2_1_,case0_.date 作为 date2_2_1_,case0_.id 作为 id6_2_1_,case0_.method 作为 method3_2_1_,case0_. purpose 作为 purpose4_2_1_,case0_.status 作为 status5_2_1_来自 SupportCase case0_ where case0_.id=?
我得到的错误: com.fasterxml.jackson.databind.JsonMappingException
printStackTrace 错误大约有 1000 行,所以我刚刚发现了异常。
但是,该列表正在控制台上打印,但是当我尝试 return 这个 list(休眠查询的结果列表)作为 JSON 时,它会抛出 JSONProcessingException。我想将列表作为 JSON 数据返回。我尝试了 2 天,但我无法得到结果。请指导我如何将其作为 JSON 返回。提前致谢。
如果我包含上面评论过的员工构造函数,我会收到 JSON 响应,否则,它不会返回,但我仍然会在控制台中获得所需的输出。为什么??
问题已解决:包括 @JsonIgnore Infinite Recursion with Jackson JSON, Spring MVC 4.2 and Hibernate JPA issue
【问题讨论】:
-
附加错误日志?
-
您可以在输出中看到该错误。 printStackTrace 错误大约有 1000 行。
-
问题已解决。我需要做的就是在映射上方包含@JsonIgnore。这是推荐链接:stackoverflow.com/questions/32354856/…