I am trying to learn building rest web service watching videos of Java Brains. Instead of just following everything the tutor does I try to mix it up on my own. So, I was facing this problem when I built up a new service class on a new package but kept on getting the error Servlet.init() for servlet Second Jersey REST Service threw exception. Would be glad if someone explained why? I am using a netbeans IDE.
Here's my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<servlet>
<servlet-name>Jersey REST Service</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.services.messages</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--New Part added -->
<servlet>
<servlet-name>Second Jersey REST Service</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.services.profiles</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!--New part added -->
<servlet-mapping>
<servlet-name>Second Jersey REST Service</servlet-name>
<url-pattern>/rest2/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>NewServlet</servlet-name>
<servlet-class>com.servlets.myclass.NewServlet</servlet-class>
<init-param>
<param-name>Name</param-name>
<param-value>Value</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>NewServlet</servlet-name>
<url-pattern>/NewServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
and here's my new web service class:
package com.service.profiles;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/profiles")
public class ProfileClass {
public ProfileClass()
{
}
#GET
#Produces(MediaType.TEXT_PLAIN)
public String getX()
{
return "Got it";
}
}
Related
I am trying to add rest api within my application so i added two servlet one for handling JSF request another for Rest API request.
web.xml
<?xml version="1.0"?>
<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"
>
<context-param>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>org.jboss.el.ExpressionFactoryImpl</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
<param-value>-1</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>redmond</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<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.infinite.npsc.webapi</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.infinite.npsc.webapi.CorsFilter</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
so, for example i want to call api as
http://localhost:5081/ApplicationFormFillingProcess-portlet/api/savePayment/2075-531345
but now i get an error of com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
if i changed jersey servlet url mapping from
<url-pattern>/api/*</url-pattern>
to
<url-pattern>/*</url-pattern>
i can call the api. but my images and other static contents are not loaded.
I am trying one restful web service example so when I am going to hit url that time I am getting HTTP Status 404 - The requested resource is not available
below are the detail of my code, if you want any other information let me know
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>User Management</display-name>
<servlet>
<servlet-name>Jersey RESTful 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.tutorialspoint</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey RESTful Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Service class
package com.tutorialspoint;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/UserService")
public class UserService {
UserDao userDao = new UserDao();
#GET
#Path("/users")
#Produces(MediaType.APPLICATION_XML)
public List<User> getUsers(){
return userDao.getAllUsers();
}
}
ALL jars
Tomcat webapps
Obviously, your URL shoud be http://localhost:8080/UserManagement/rest/UserService/users.
Also you can try to delete * in <url-pattern>/rest/*</url-pattern>
This issue has been resolved,
Actually my web.xml was not on correct place that is why I was getting the "The requested resource is not available" . The file web.xml should be placed inside WEB-INF folder
I had the same error. I correct it with modifying the web.xml, when decalring the servlet.
<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>org.webservice.messenger.ressources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
My package was declared :
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.webservice.messenger.messengers</param-value>
</init-param>
while my classes were on package : org.webservice.messenger.ressources
I hope it s clear now.
I am new to jax rs web service. I was studying from this link- http://www.vogella.com/tutorials/REST/article.html
Now when I was trying to do my first rest service i faced some errors.
This is my service code
package de.vogella.jersey.first;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/hello")
public class Hello {
// This method is called if TEXT_PLAIN is request
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
}
My web.xml is
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web- app_2_5.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">
<display-name>de.vogella.jersey.first</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 REST Service</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>de.vogella.jersey.first</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Now when I was running this code I faced HTTP Status 404 error. Please any one help me. I give all the jars from http://jersey.java.net/ Please help me.
If you are using jersey 2.x you Web.xml servlet should be as following
<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>de.vogella.jersey.first</param-value>
</init-param>
</servlet>
In jersey 2.x you need to refer "org.glassfish" and your resource URL would be
localhost:your_port/your_app_name/rest/hello
I have a problem with creating web services from my entity classes by using netbeans. Actually I have a javaee project which works on weblogic 10g, oracle database 10g and uses JSF 2.1 with primefaces. It works quite good.
But when it comes to add web services to my project I come across with
Deployment failed. The message was: java.lang.NoClassDefFoundError: com/sun/jersey/spi/container/ContainerListener
C:\Users\Developer\Documents\NetBeansProjects\IBB_Projeler\nbproject\build-impl.xml:294: The module has not been deployed.
As I add the web services, my web.xml becomes:
<?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">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<description>Multiple packages, separated by semicolon(;), can be specified in param-value</description>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>Resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/webresources/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
3000
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsf</welcome-file>
</welcome-file-list>
<ejb-local-ref>
<ejb-ref-name>ProjectsFacade</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>com.ibb.entity.facades.ProjectsFacadeLocal</local>
<ejb-link>projetakip-ejb.jar#ProjectsFacade</ejb-link>
</ejb-local-ref>
<mime-mapping>
<extension>jpeg</extension>
<mime-type>image/jpeg</mime-type>
</mime-mapping>
<ejb-local-ref>
<ejb-ref-name>ProjectsFacade1</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>com.ibb.entity.facades.ProjectsFacadeLocal</local>
<ejb-link>projetakip-ejb.jar#ProjectsFacade</ejb-link>
</ejb-local-ref>
<ejb-local-ref>
<ejb-ref-name>ProjectsFacade2</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>com.ibb.entity.facades.ProjectsFacadeLocal</local>
<ejb-link>projetakip-ejb.jar#ProjectsFacade</ejb-link>
</ejb-local-ref>
<persistence-unit-ref>
<persistence-unit-ref-name>persistence-factory</persistence-unit-ref-name>
<persistence-unit-name>projetakip-warPU</persistence-unit-name>
</persistence-unit-ref>
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/index.jsf</location>
</error-page>
</web-app>
Basically it adds the following:
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/webresources/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<description>Multiple packages, separated by semicolon(;), can be specified in param-value</description>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>Resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
I searched a little bit, in some forums they say to add related jars. I have added jersey-server, jsr311-api, asm3.1,jersey-gf-server, and jersey-servlet.
But the problem still occurs, if someone could help I would be greatly appriciated.
i am trying to deploy my app to GAE but i allways get 404 not found error on all pages(jsp) except for default. RESTful webservices working without problem. I am using netbeans 7.1.1 and gae 1.6.4.1
When I test it locally, everything works fine without any problem. But when I upload it do gae, allways only default page is available. I tryied almost everything.
I tryied to check whether the files are on GAE using appcfg.cmd. I downloaded my files from GAE and found out that noone is missing so the update process was probably successful.
I tryied to upload it with appcfg script instead of netbeans plugin but the process failed because of:
com.google.appengine.tools.admin.JspCompilationException: Failed to compile jsp files.
I cant simply make it working using appcfg script, searech internet for almost whole day and found no possible solution. Uploading with netbeans plugin worked without problem.
So what can be the reason to allways throw 404 on gae? Please help.
Thx very much. If you need any other info, i will be glad to post it here.
Ok,Here is my web.xml
<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>facebookLogin</servlet-name>
<servlet-class>Login.FacebookLogin</servlet-class>
</servlet>
<servlet>
<servlet-name>PlanProcess</servlet-name>
<servlet-class>Input.PlanProcess</servlet-class>
</servlet>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>facebookLogin</servlet-name>
<url-pattern>/facebooklogin</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>private</servlet-name>
<jsp-file>private.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>private</servlet-name>
<url-pattern>/private</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>editPlan</servlet-name>
<jsp-file>editTrainingPlan.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>editPlan</servlet-name>
<url-pattern>/private/trainingplan/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>listplans</servlet-name>
<jsp-file>listTrainingPlans.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>listplans</servlet-name>
<url-pattern>/private/listplans</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>visualize</servlet-name>
<jsp-file>visualize.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>visualize</servlet-name>
<url-pattern>/private/visualize</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>graph</servlet-name>
<jsp-file>graph.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>graph</servlet-name>
<url-pattern>/graph/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>created</servlet-name>
<jsp-file>created.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>created</servlet-name>
<url-pattern>/created</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>login</servlet-name>
<jsp-file>login.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>newRecord</servlet-name>
<jsp-file>newRecord.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>newRecord</servlet-name>
<url-pattern>/private/newrecord</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>newPlan</servlet-name>
<jsp-file>newPlan.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>newPlan</servlet-name>
<url-pattern>/private/newplan</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>newExcercise</servlet-name>
<jsp-file>newExcercise.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>newExcercise</servlet-name>
<url-pattern>/private/newexcercise</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Jersey Web Application</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>Webservice</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>test.InsertToDb</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/testinsert</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>recordProcess</servlet-name>
<servlet-class>Input.RecordProcess</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>recordProcess</servlet-name>
<url-pattern>/record_process</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>excerciseProcess</servlet-name>
<servlet-class>Input.ExcerciseProcess</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>excerciseProcess</servlet-name>
<url-pattern>/excercise_process</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PlanProcess</servlet-name>
<url-pattern>/plan_process</url-pattern>
</servlet-mapping>
After hours of debugging and searching, i found out, that if you want to map jsp files
you have to use
<servlet>
<servlet-name>editPlan</servlet-name>
<jsp-file>**/**xxx.jsp</jsp-file>
</servlet>
instead of
<servlet>
<servlet-name>editPlan</servlet-name>
<jsp-file>xxx.jsp</jsp-file>
</servlet>
The '/' in path is essential, it will work on loaclhost but not deployed on gae. What a crap...
I never mapped JSPs to anything in web.xml. Why do you do that?