web services - ClassNotFoundException after adding JAX-RS annotations (@Path etc.) in classes in Java -
i need jax-rs , jersey in multi module java ee app.
i start describing environment:
- java 8 (java ee 7)
- maven 3.3.3
- glassfish 4.1.
after deploying on glassfish there 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 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 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 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 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 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 load class pl.com.softnet.rest.restconfig, reason: java.lang.classnotfoundexception: pl.com.softnet.rest.restconfig]]
here poms dependency of web service module:
<!-- javaee api 7--> <dependency> <groupid>javax</groupid> <artifactid>javaee-api</artifactid> <version>7.0</version> <scope>provided</scope> </dependency> <!-- servlet--> <dependency> <groupid>javax.servlet.jsp</groupid> <artifactid>jsp-api</artifactid> <exclusions> <exclusion> <groupid>javax.el</groupid> <artifactid>el-api</artifactid> </exclusion> </exclusions> <scope>provided</scope> </dependency> <dependency> <groupid>org.glassfish.jersey.containers</groupid> <artifactid>jersey-container-servlet</artifactid> <version>2.22</version> <scope>provided</scope> </dependency> <dependency> <groupid>org.glassfish.jersey.core</groupid> <artifactid>jersey-client</artifactid> <version>2.22</version> <scope>provided</scope> </dependency> <!-- richafaces --> <dependency> <groupid>org.richfaces</groupid> <artifactid>richfaces</artifactid> <version>4.5.0.final</version> <scope>provided</scope> </dependency> <dependency> <groupid>javax.el</groupid> <artifactid>el-api</artifactid> <version>2.1.2-b04</version> <scope>provided</scope> </dependency> <!-- jsf --> <dependency> <groupid>org.glassfish</groupid> <artifactid>javax.faces</artifactid> <version>2.2.0</version> <scope>provided</scope> </dependency>
this resource class:
package pl.com.softnet.rest; import org.w3c.dom.document; import org.w3c.dom.element; import org.xml.sax.saxexception; import pl.com.softnet.ejb3.localbeans.fsmadministratorbean; import pl.com.softnet.ejb3.localbeans.modulbezpieczenstwabean; import pl.com.softnet.entity.procesydef; import pl.com.softnet.entity.uzytkownik; import pl.com.softnet.filters.testfilter; import pl.com.softnet.util.servicelocator; import pl.com.softnet.util.xmlutils; import pl.com.softnet.wyjatki.modyfikacjaprocesuexception; import javax.naming.namingexception; import javax.ws.rs.*; import javax.ws.rs.core.mediatype; import javax.ws.rs.core.response; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.parsers.parserconfigurationexception; import javax.xml.transform.transformerconfigurationexception; import javax.xml.transform.transformerexception; import javax.xml.xpath.xpathexpressionexception; import java.io.ioexception; import java.util.uuid; @path("config") public class graphconfig { @get @path("{id}") @produces(mediatype.application_xml) public response getmethod { } @post @path("{id}") @consumes(mediatype.application_xml) public response postmethod() { } }
there web service class:
package pl.com.softnet.rest; import javax.ws.rs.applicationpath; import javax.ws.rs.core.application; import java.util.hashset; import java.util.set; @applicationpath("/rest/*") public class restconfig extends application { @override public set<class<?>> getclasses() { set<class<?>> myresources = new hashset<class<?>>(); myresources.add(graphconfig.class); return myresources; } }
my web.xml file:
<servlet> <servlet-name>jersey-servlet</servlet-name> <servlet-class>pl.com.softnet.rest.restconfigr</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-servlet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>
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>${endorsed.dir}</endorseddirs> </compilerarguments> </configuration> </plugin> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-war-plugin</artifactid> <version>2.3</version> <configuration> <failonmissingwebxml>false</failonmissingwebxml> </configuration> </plugin> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-dependency-plugin</artifactid> <version>2.6</version> <executions> <execution> <phase>validate</phase> <goals> <goal>copy</goal> </goals> <configuration> <outputdirectory>${endorsed.dir}</outputdirectory> <silent>true</silent> <artifactitems> <artifactitem> <groupid>javax</groupid> <artifactid>javaee-endorsed-api</artifactid> <version>7.0</version> <type>jar</type> </artifactitem> </artifactitems> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
class extends application
/* * change license header, choose license headers in project properties. * change template file, choose tools | templates * , open template in editor. */ package br.ceptro.measure.restfullexample; import java.util.set; import javax.ws.rs.core.application; /** * * @author polianareis */ @javax.ws.rs.applicationpath("resources") public class applicationconfig extends application { @override public set<class<?>> getclasses() { set<class<?>> resources = new java.util.hashset<>(); addrestresourceclasses(resources); return resources; } /** * not modify addrestresourceclasses() method. * automatically populated * resources defined in project. * if required, comment out calling method in getclasses(). */ private void addrestresourceclasses(set<class<?>> resources) { resources.add(br.ceptro.measure.restfullexample.olabrasilresource.class); } }
your rest web services
/* * change license header, choose license headers in project properties. * change template file, choose tools | templates * , open template in editor. */ package br.ceptro.measure.restfullexample; import javax.ws.rs.core.context; import javax.ws.rs.core.uriinfo; import javax.ws.rs.consumes; import javax.ws.rs.put; import javax.ws.rs.path; import javax.ws.rs.post; import javax.ws.rs.produces; import org.codehaus.jettison.json.jsonexception; import org.codehaus.jettison.json.jsonobject; /** * rest web service * * @author polianareis */ @path("ola-brasil") public class olabrasilresource { @context private uriinfo context; /** * creates new instance of olabrasilresource */ public olabrasilresource() { } /** * retrieves representation of instance of br.ceptro.measure.restfullexample.olabrasilresource * @return instance of java.lang.string */ @post @produces("application/json") public string postjson(string content) throws jsonexception { jsonobject jsonobject = new jsonobject(content); string value = jsonobject.getstring("value"); return "your web service works !!! valueis="+ value; } /** * put method updating or creating instance of olabrasilresource * @param content representation resource * @return http response content of updated or created resource. */ @put @consumes("application/json") public string putjson(string content) { return "example put"; } }
you need, clean , build. , deploy war artifact in glassfish 4.1
for test, use rest client in navigator.
this complete example , works . see image below:
Comments
Post a Comment