java - 在java Rest服务中获取json

标签 java json rest jakarta-ee glassfish

我从本地主机通过 netbeans 提供了一个 Restful 服务,并选择 glassfish 4.1.1 作为我的网络服务器 然后我想测试一下。 当它想给我一个 xml 响应时它正在工作,但是当我尝试获取 JSON 响应时它给了我一个 500 错误,如下所示

GET RequestFailed RequestFailed --> Status: (500) 
Response: {

HTTP Status 500 - Internal Server Error


type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception 
javax.servlet.ServletException: javax.ejb.EJBException


root cause 
javax.ejb.EJBException


root cause 
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Error Code: 0


root cause 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Error Code: 0


root cause 
java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.


root cause 
javax.resource.spi.ResourceAllocationException: Error in allocating a connection. Cause: Connection could not be allocated because: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.


root cause 
com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.


root cause 
com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.


root cause 
com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.


root cause 
javax.resource.spi.ResourceAllocationException: Connection could not be allocated because: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.


root cause 
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.


root cause 
java.net.ConnectException: Connection refused: connect


note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1.1 logs.


GlassFish Server Open Source Edition 4.1.1 

}

我在网络和堆栈上搜索,发现它是由于 eclipselink 而发生的,因此我使用 eclipselink 2.6.3 和 2.6.1 对其进行了测试,并重新安装了我的本地主机,但再次出现错误:

GET RequestFailed RequestFailed --> Status: (500) 
Response: {
HTTP Status 500 - Internal Server Error

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper
root cause

org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper
root cause

java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1.1 logs.

GlassFish Server Open Source Edition 4.1.1


}

抱歉我的英语不好 非常感谢您花时间解决我的问题,即使只是阅读它。

最佳答案

堆栈跟踪显示代码因 java.lang.NoClassDefFoundError 失败

java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper

确保 org.eclipse.persistence.jaxb.BeanValidationHelper 类依赖项在运行时存在。

您可以阅读有关 NoClassDefFoundError @ How to solve java.lang.NoClassDefFoundError? 的更多信息。

================================================== =========================== 更新:

GlassFish 4.1.1 似乎存在错误。 JIRA 仍然开放 https://java.net/jira/browse/GLASSFISH-21440 。 Jersey 上也有一个 JIRA https://java.net/jira/browse/JERSEY-2888 。最后,他们提供了一些解决该问题的方法。请验证该解决方法。

关于java - 在java Rest服务中获取json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38282230/

相关文章:

java - 可以从用户输入的列表中获取单独的数字

java - 禁用 setOnKeyPressed 的自动重复 (JavaFX 2.1)

java - Java 8 日期/时间 API 中的持续时间类

javascript - 添加到可能存在或可能不存在的 json 属性

java - 是否可以设置 Undertow 来为 Spring REST 端点提供服务?

java - iText 7 无法设置边距

objective-c - JSON解析看不懂

java - 如何在HTTP客户端库中返回JSON?

html - 在 Angular2 中显示默认图像

api - 如何将供应商特定的 MIME-TYPES 用于 "private-labeled"REST API