ClassNotFoundException on ServletContainer when configuring Jersey in Intellij - java

When trying to configure jax rs in intellij I get this error:
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1945)
My webconfig looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Rest</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.umbrella.server.api</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Rest</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
I manually downloaded the entire jersey pack from the official jersey website. Which should contain all the dependencies.
The weird thing is that whenever I create a class that derives from the ServletContainer it doesn't give any errors.

I found the anwser with the help of Eugen Covaci.
In the Intellij project settings there is a tab called problems. When clicking solve on the problems listed there it started working again.
Another issue was that the jar files were located in web/WEB_INF/libs/api instead of the correct folder (web/WEB_INF/lib)

Related

OpeningRestful API ends with 404 allt the time

I have a done MVC Java project and I need to make an API for it. I'm trying to use Jersey. I installed it according to this instruction:
http://www.mazong1123.com/use-jersey-2.0-without-maven/
Since my project was already done and it was not a maven. Also, I'm following this instruction: http://www.mazong1123.com/use-jersey-2.0-without-maven/
The thing is I've downloaded and put all.jar files into my project WEB-INF, then I created RESTful Web Services from Patterns (created class within my package where the model, servlet, and .jdbc are. After that BookResource.java and ApplicationConfig.java were created as on the screen:
and edited my web.xml to look like this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" id="WebApp_ID"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<display-name>parking-space-booking-system</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>PBS</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.pbs.web.jdbc</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PBS</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
I'm using Tomcat server. Whenever I provide following URL: http://localhost:8084/PBS/webapi/booksresource I'm getting 404. Is it something wrong with my web.xml?
Regards

web.xml and pom.xml documentation

I need documentation about web.xml and pom.xml files. i would like learn web develop, so i start some tutorials about that. I use IDE eclipse and i choose the option maven project for develop the tutorials. i dont have errors but i cant deploy the projects in web. May be my problems are that i not understand in totality the concept of this files. Some documentation about this i sure that help me. I searched in Satck, in foros but i dont found documentation. I wonder how web developers know the code they need these files
web.xml is web application configuration file defines the servlet container used in the application. You can typically add start-up code, url-pattern as this will be called on application start. Example below extracted from Jersey RestFull service.
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>Jersey Web Application</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.xxxx.shopper</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.xxxx.shopper.service.Startup</listener-class>
</listener>
</web-app>
pom.xml is maven configuration file defines build process and loads any dependencies.
Good Reads:
1. https://www.mkyong.com/maven/how-to-create-a-web-application-project-with-maven/
2. Difference between web projects with pom.xml and web.xml
3. Why do we use web.xml?

404 Not Found error was encountered while trying to use an ErrorDocument to handle the request

I am developing a website and i am finding hosting issues. I have put all my .jsp files in the root directory.
I am able to see the JSPs (accessing directly to them).
But, when i try to perform the same check the WEB-INF/classes, I get the error:
Not Found The requested URL /AC/SearchController was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
This is my Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>AC</display-name>
<welcome-file-list>
<welcome-file>Home.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>AdvisorProfileServlet</servlet-name>
<servlet-class>org.AC.controller.AdvisorProfileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AdvisorProfileServlet</servlet-name>
<url-pattern>/AC/AdvisorProfilePage?aId=*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ForgotPasswordRedirectController</servlet-name>
<servlet-class>org.AC.controller.ForgotPasswordURLController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ForgotPasswordRedirectController</servlet-name>
<url-pattern>/ForgotPasswordAdvisor</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>AdvisorMyAccountRequest</servlet-name>
<servlet-class>org.AC.controller.AdvisorMyAccountRequestController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AdvisorMyAccountRequest</servlet-name>
<url-pattern>/AC/requests.jsp</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ForgotPasswordRedirectControllerUser</servlet-name>
<servlet-class>org.AC.controller.UserForgotPasswordURLController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ForgotPasswordRedirectControllerUser</servlet-name>
<url-pattern>/ForgotPasswordUser</url-pattern>
</servlet-mapping>
</web-app>
This might also be a server config, because I created the project on Tomcat server 8, but I guess the server which is now hosting it is Tomcat 7.
How can I resolve this?
Please declare "/AC/SearchController" as url pattern in your web.xml

Simple Rest service with Tomcat and Eclipse, Mac os. The requested resource is not available

I tried a lot of differents configurations following some other posts, but anything worked. I'm trying to run this simple tutorial(http://www.vogella.com/tutorials/REST/article.html) but i'm still having the message: "The requested resource is not available". Anyone?
Running on Eclipse Kepler, Tomcat 7 and MAC OS 10.8.5.
I tried this configuration for the web.xml, but didn't work too (reference: Running Jersey project (Rest web service) to tomcat)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Hello</display-name>
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
and those are the jars in the last try:
asm-3.3.1.jar
jersey-bundle-1.12.jar
jersey-server-1.12.jar
jersey-servlet-1.12.jar
jsr311-api-1.0.jar
tried the following urls:
http://localhost:8080/Hello/
http://localhost:8080/de.vogella.jersey.first/rest/hello
You mapped the rest request to the path <url-pattern>/rest/*</url-pattern> so your request should be something like http://localhost:8080/rest/Hello/

Jersey Services with Tomcat and Eclipse

I'm developing a rest service with Jersey 2.0 (I downloaded from http://repo1.maven.org/maven2/org/glassfish/jersey/bundles/jaxrs-ri/2.5/jaxrs-ri-2.5.zip) and I'm using Tomcat 7.0.47. I run Tomcat from Eclipse and my machine is a Mac.
I'm using the Eclipse tool to generate a WAR and to deploy the service.
This is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>MyServices</display-name>
<servlet>
<servlet-name>Jersey REST Service</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.service.services.Services</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
I include all Jersey jars into WEB-INF/lib, except javax.servlet-api-3.0.1.jar that it is into Apache/lib
When I deploy in Tomcat, it shows a very strange error caused by:
Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.client.ClientConfig
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
... 70 more
My solution:
Add into Tomcat/lib all libraries that you download from Jersey and
are including into /ext folder of the Jersey .zip
Add into Web-Inf/lib of my project only libraries that are under /lib folder of the Jersey zip file
Add into Web-Inf/lib of my project javax.ws.rs-api-2.jar that you can find in /api folder of Jersey
With this, I don't have problems to run Tomcat with Jersey.
And this is my web.xml for Jersey 2.0
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>MyRESTServices</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey Web Application</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.myservice.services</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Where com.myservice.services is the package where I have my services
Thanks for your comments!!
It works for me on a PC. Did you make the eclipse project a maven project? If you do that then you can use org.glassfish.jersey.bundles jaxrs-ri version 2.5 (or whichever) as a dependency in your pom and then when you do run->maven-install from eclipse it will build a war file with all the necessary dependencies in it.
Adding Jersey to an Eclipse project without maven is simple: Click properties for the project, go to the Java Build Path. Click on the Add Library on the left, select user library. After you click next you will see "user libraries" in the upper right. Click that. Click new. Add a name: jaxrs-ri-2.5 for example. Click add external jars, and add the jars in the api, lib and ext folders. You're done. This will probably also include them in any war file you make with eclipse.
Worked for me.
Go with jdk1.7.0_04 and install apache-tomcat-7.0.55 and set the environment variable for this setting and set eclipse for same for compiling building and runtime. then everything will be OK for Jax-RS-2.1

Categories