简体   繁体   English

在Java Rest Service中获取json

[英]getting json in java rest service

I made a restful service by netbeans from my localhost and I choose glassfish 4.1.1 as my webserver then I wanted to test it. 我通过本地主机上的netbeans提供了一个轻松的服务,我选择了glassfish 4.1.1作为我的Web服务器,然后想对其进行测试。 it was working when it wanted to give me an xml response but when I tried to get an JSON response it gave me a 500 ERROR as below 当它想给我一个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 

}

I searched on web and stack and see that it occursed because of eclipselink so I tested it with eclipselink 2.6.3 and 2.6.1 and reinstall my localhost but got errors again: 我在网络和堆栈上搜索,发现它是由于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


}

sorry for my bad English language and thank you so much for putting your time for my problem even if just read it. 对于我的英语不好,我们深表歉意。非常感谢您为解决我的问题付出了时间,即使只是阅读它也是如此。

The stacktrace shows that the code is failing with java.lang.NoClassDefFoundError stacktrace显示代码因java.lang.NoClassDefFoundError而失败

java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper java.lang.NoClassDefFoundError:无法初始化类org.eclipse.persistence.jaxb.BeanValidationHelper

Make sure that org.eclipse.persistence.jaxb.BeanValidationHelper class dependecy is present during the runtime. 确保在运行时出现org.eclipse.persistence.jaxb.BeanValidationHelper类依赖项。

You can read more about NoClassDefFoundError @ How to solve java.lang.NoClassDefFoundError? 您可以阅读有关NoClassDefFoundError的更多信息@ 如何解决java.lang.NoClassDefFoundError?

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

It looks like there is a bug with GlassFish 4.1.1. GlassFish 4.1.1似乎有一个错误。 The JIRA is still OPEN https://java.net/jira/browse/GLASSFISH-21440 . JIRA仍然是开放的https://java.net/jira/browse/GLASSFISH-21440 There is one JIRA on Jersey as well https://java.net/jira/browse/JERSEY-2888 . 泽西岛上也有一个JIRA, https://java.net/jira/browse/JERSEY-2888 At the end, they have provided some workaround for the problem. 最后,他们提供了解决该问题的方法。 Please verfiy with that workaround. 请证实该解决方法。

for making the Solution bold and clear, 使解决方案大胆而清晰

recording to upper answer, in this case all you need to don't use glassfish 4.1.1 ,just install glassfish 4.1 and this matter solved (because solving the 4.1.1 error took too long time from me and i suggest you to Do not try to solve it ) 记录到最高答案,在这种情况下,您只需要不使用glassfish 4.1.1,只需安装glassfish 4.1即可解决此问题(因为从我这里解决4.1.1错误花费了很长时间,我建议您不要尝试解决它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM