尝试获取JSON响应时无法初始化类org.eclipse.persistence.jaxb.BeanValidationHelper [英] Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper while trying to get JSON response
问题描述
我正在使用JAVA EE,JAX-RS,JPA,GLASSFISH进行申请.对于 MediaType.APPLICATION_XML ,响应正常工作.它不能在 MediaType.APPLICATION_JSON 中使用.
I am making application using JAVA EE,JAX-RS,JPA,GLASSFISH. Response is working properly in case of MediaType.APPLICATION_XML. It's not working in MediaType.APPLICATION_JSON.
这是我的pojo课
@Entity
@XmlRootElement
public class Book {
private int id;
private String name;
private String publication;
private String price;
@Id
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "publication")
public String getPublication() {
return publication;
}
public void setPublication(String publication) {
this.publication = publication;
}
@Basic
@Column(name = "price")
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
if (id != book.id) return false;
if (name != null ? !name.equals(book.name) : book.name != null) return false;
if (publication != null ? !publication.equals(book.publication) : book.publication != null) return false;
if (price != null ? !price.equals(book.price) : book.price != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (publication != null ? publication.hashCode() : 0);
result = 31 * result + (price != null ? price.hashCode() : 0);
return result;
}
}
这是pom.xml文件.
Here is the pom.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rajesh</groupId>
<artifactId>Library</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
端点的资源类
@Path("books")
public class BookResource {
@GET
@Path("{bookId}")
@Produces({MediaType.APPLICATION_JSON})
public Response getBookById(@PathParam("bookId") int id) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("abc");
EntityManager entityManager = entityManagerFactory.createEntityManager();
Book book = entityManager.find(Book.class, id);
if (book == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
return Response.status(Response.Status.OK).entity(book).build();
}
}
推荐答案
这是Glassfish中的错误: http://www.payara.fish/是Glassfish的补丁和更新版本(如果需要,甚至可以提供商业支持).
This is a bug in Glassfish: https://github.com/javaee/glassfish/issues/21440 It probably won't be fixed, you should switch to http://www.payara.fish/ which is patched and updated version of Glassfish (has even commercial support if needed).
此错误已在Payara中修复.
This bug is fixed in Payara.
这篇关于尝试获取JSON响应时无法初始化类org.eclipse.persistence.jaxb.BeanValidationHelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!