在 Java 中使用 Jersey 时出现 Errors$ErrorMessagesException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20578642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Errors$ErrorMessagesException when using Jersey in Java
提问by Pradeep Simha
I am using Jersy to develeop REST webservices, this is my simple code:
我正在使用 Jersy 来开发 REST webservices,这是我的简单代码:
@GET
@Path("/retrieveCustomerInformation/{jsonString}")
@Produces(MediaType.APPLICATION_JSON)
public String retrieveCustomerInformation(@PathParam("jsonString")JSONObject jsonObject)
throws Exception {
//Other codes here
}
But when I ping the rest service url from browser, I am getting below exception:
但是,当我从浏览器 ping 其余服务 url 时,出现以下异常:
javax.servlet.ServletException: Servlet.init() for servlet jersey-serlvet threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
and the root cause is:
根本原因是:
com.sun.jersey.spi.inject.Errors$ErrorMessagesException
com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
Can someone guide me, what causes this error? I am finding difficult to understand this error message as it doesn't provide any useful information to debug.
有人可以指导我,导致此错误的原因是什么?我发现很难理解此错误消息,因为它没有提供任何有用的调试信息。
This is my web.xml
这是我的 web.xml
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.test/param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
I am using Jersey 1.8, JDK 1.7 and App is running on Tomcat 7.0. If required I can provide more information.
我正在使用 Jersey 1.8、JDK 1.7 并且应用程序在 Tomcat 7.0 上运行。如果需要,我可以提供更多信息。
采纳答案by techknowcrat
Your question seems to be a duplicate and has probably been appropriately addressed. I too had the same issue some time ago. Click here to see the accepted answer that resolved it for me
您的问题似乎是重复的,并且可能已得到适当解决。前段时间我也有同样的问题。单击此处查看已为我解决的已接受答案
And in case you're reluctant to follow the link, first of all, try adding jersey-multipart.jar
and mimepull.jar
to your project libraries... And if your project is a Maven project, add the following dependencies to your pom.xml
. The mimepull
dependency should ship along with it.
如果您不愿意点击链接,首先,尝试将jersey-multipart.jar
和添加mimepull.jar
到您的项目库中……如果您的项目是 Maven 项目,请将以下依赖项添加到您的pom.xml
. 该mimepull
相关性应该与它一起发货。
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.8</version>
</dependency>
Remember to ensure that the version of jersey-multipart
that you use, is the same as the version of jersey
that you're using in the project.
请记住确保jersey-multipart
您使用的版本与jersey
您在项目中使用的版本相同。
Just in case the above solution doesn't resolve your issue, you may want to see herefor more useful tips too. Cheers!
以防万一上述解决方案不能解决您的问题,您可能还想在这里查看更多有用的提示。干杯!
回答by Aruljothi
Add dependency from the reference
从引用添加依赖项
Jersey Application Deployment and Runtime Environments
in your web.xml
在你的 web.xml 中
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.rest.portal</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.scanning.recursive</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>com.rest.portal.HelloWord</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
In Jersey 1.x only using the
在 Jersey 1.x 中仅使用
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
In 2.x series use
在 2.x 系列中使用
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
And add necessary jersy jar files
并添加必要的 jersy jar 文件
- asm-5.0.2.jar
- jersey-common.jar
- jersey-servlet-container-core.jar
- jersey-servlet-container.jar
- jersey-server.jar
- asm-5.0.2.jar
- jersey-common.jar
- jersey-servlet-container-core.jar
- jersey-servlet-container.jar
- 球衣-server.jar
回答by Tomasz O.
Check for clashing @Path annotations. This will result with the same error. It's a weird error for communicating path issues, but you can easily test it by renaming the matching paths.
检查冲突的@Path 注释。这将导致相同的错误。这是传达路径问题的一个奇怪错误,但您可以通过重命名匹配路径轻松测试它。
Example of cashing paths in the code below
下面代码中的兑现路径示例
Some class
某类
@Path("/storage")
public class BookingRestService {
@GET
@Path("/bookings")
@Produces(value = MediaType.APPLICATION_XML)
and another class
和另一个班级
@Path("/storage")
public class StorageRestService {
By renaming any of the @Path("/storage")
the problem seizes to impair your work progress.
通过重命名任何@Path("/storage")
问题都会影响您的工作进度。
回答by ucas
One more use-case, which causes the same exception, which broke my code. If you were to write the resource like this:
另一个用例会导致相同的异常,这破坏了我的代码。如果您要像这样编写资源:
public String getPOST(@QueryParam("type") Character type){}
the following data type, Character
, will cause the exception.
以下数据类型,Character
,将导致异常。