Pradeep Simha
Pradeep Simha

Reputation: 18123

Errors$ErrorMessagesException when using Jersey in Java

I am using Jersy to develeop REST webservices, this is my simple code:

@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:

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

<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.

Upvotes: 5

Views: 26393

Answers (4)

Tomasz O.
Tomasz O.

Reputation: 542

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.

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 is remediated.

Upvotes: 4

ucas
ucas

Reputation: 487

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.

Upvotes: 0

Aruljothi
Aruljothi

Reputation: 497

Add dependency from the reference

Jersey Application Deployment and Runtime Environments

in your 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

<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

In 2.x series use

<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

And add necessary jersy jar files

  1. asm-5.0.2.jar
  2. jersey-common.jar
  3. jersey-servlet-container-core.jar
  4. jersey-servlet-container.jar
  5. jersey-server.jar

Upvotes: 0

SourceVisor
SourceVisor

Reputation: 1996

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.

<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.

Just in case the above solution doesn't resolve your issue, you may want to see here for more useful tips too. Cheers!

Upvotes: 3

Related Questions