Redpike February 2016

ClassNotFoundException after adding JAX-RS annotations (@Path etc.) in classes in Java

I need some help with JAX-RS and Jersey in my multi module Java EE App.

I will start with describing my environment:

  1. Java 8 (Java EE 7)
  2. Maven 3.3.3
  3. GlassFish 4.1.

After properly deploying on GlassFish there are such errors in logs:

[2016-02-08T14:06:10.302+0100] [glassfish 4.1] [WARNING] [AS-WEB-UTIL-00035] [javax.enterprise.web.util] [tid: _ThreadID=44 _ThreadName=admin-listener(2)] [timeMillis: 1454936770302] [levelValue: 900] [[
  Unable to load class pl.com.softnet.rest.RestConfig, reason: java.lang.ClassNotFoundException: pl.com.softnet.rest.RestConfig]]

[2016-02-08T14:06:10.304+0100] [glassfish 4.1] [WARNING] [AS-WEB-UTIL-00035] [javax.enterprise.web.util] [tid: _ThreadID=44 _ThreadName=admin-listener(2)] [timeMillis: 1454936770304] [levelValue: 900] [[
  Unable to load class pl.com.softnet.rest.GraphConfig, reason: java.lang.ClassNotFoundException: pl.com.softnet.rest.GraphConfig]]

[2016-02-08T14:06:10.304+0100] [glassfish 4.1] [WARNING] [AS-WEB-UTIL-00035] [javax.enterprise.web.util] [tid: _ThreadID=44 _ThreadName=admin-listener(2)] [timeMillis: 1454936770304] [levelValue: 900] [[
  Unable to load class pl.com.softnet.rest.GraphConfig, reason: java.lang.ClassNotFoundException: pl.com.softnet.rest.GraphConfig]]

[2016-02-08T14:06:10.304+0100] [glassfish 4.1] [WARNING] [AS-WEB-UTIL-00035] [javax.enterprise.web.util] [tid: _ThreadID=44 _ThreadName=admin-listener(2)] [timeMillis: 1454936770304] [levelValue: 900] [[
  Unable to load class pl.com.softnet.rest.GraphConfig, reason: java.lang.ClassNotFoundException: pl.com.softnet.rest.GraphConfig]]

[2016-02-08T14:06:10.305+0100] [glassfish 4.1] [WARNING] [AS-WEB-UTIL-00035] [javax.enterprise.web.util] [tid: _ThreadID=44 _ThreadName=admin-listener(2)] [timeMillis: 1454936770305] [levelValue: 900] [[
  Unable to load class pl.com.softnet.rest.GraphConfig, reason: java.lang.ClassNotFoundException: pl.com.softnet.rest.GraphConfig]]

[2016-02-08        

Answers


YouYou February 2016

Assuming you are using servlet 3.0 or higher

From https://jersey.java.net/nonav/documentation/2.0/deployment.html

There are multiple deployment options for the class that implements Application interface in the Servlet 3.0 container. For simple deployments, no web.xml is needed at all. Instead, an @ApplicationPath annotation can be used to annotate the user defined application class and specify the the base resource URI of all application resources

First remove all related Jersey configuration from web.xml

Update your RestConfig to be :

@ApplicationPath("rest")
public class MyApplication extends ResourceConfig {
    public MyApplication() {
        packages("pl.com.softnet.rest");// This will scan for rest resources
    }      
}

The other approach is to use web.xml in that case remove MyApplication class

and update your web.xml to be like the following:

      <servlet>
        <servlet-name>jersey-servlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
      <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>pl.com.softnet.rest</param-value>
      </init-param>
     </servlet>
    ...
    <servlet-mapping>
        <servlet-name>jersey-servlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

I hope that can work ! :)


pmreis February 2016

In your web.xml you just need this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <servlet>
        <servlet-name>ServletAdaptor</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
</web-app>

Note that is important the web-app version of web.xml

In your pom.xml

   <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.12</version>
    </dependency>

And, you need a class that implements Application, but you don't need any Implementation. Just Like this is enough:

@ApplicationPath("/rest")
public class RestConfig extends Application {

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of SimetPersistence
     */
    public RestConfig() {
    }
}

and, your application:

@Path("config")
public class GraphConfig {

 public GraphConfig() {}

    @GET
    @Path("{id}")
    @Produces(MediaType.APPLICATION_XML)
    public Response getMethod {
...

It, must work. =)


pmreis February 2016

Enviroment :

  • Java version: 1.7.0_79, vendor: Oracle Corporation
  • Apache Maven 3.0.3
  • Glassfish 4.1 (build 13)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>br.ceptro.measure</groupId>
    <artifactId>restfullexample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>restfullexample</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
         <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.12</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs&g 

Post Status

Asked in February 2016
Viewed 1,201 times
Voted 9
Answered 3 times

Search




Leave an answer