在 java 休息服务中获得 json

getting json in java rest service

我在本地主机上通过 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 class 依赖项在运行时存在。

您可以阅读更多关于 NoClassDefFoundError @ How to solve java.lang.NoClassDefFoundError?

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

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

为了使解决方案变得粗体和清晰,

记录上面的答案,在这种情况下,您只需要不使用glassfish 4.1.1,只需安装glassfish 4.1即可解决此问题(因为解决4.1.1错误对我和我来说花费了太长时间建议你不要尝试解决它)