I am following a simple webservice tutorial and can't seem to interact with the Java code. I suspect my web.xml has an error but I'm not sure. There are no obvious errors and the index.jsp is server without any problems.
So, when I'm running it on the server, it opens index.jsp and I then try the following urls, but I'm getting 'HTTP 404 Errors'
http://localhost:8080/RestApi/ - works, shows html page
http://localhost:8080/RestApi/rest - http 404 error
http://localhost:8080/RestApi/rest/hello - http 404 error
http://localhost:8080/RestApi/rest/hello/somevalue - http 404 error
Here is what i have
Dynamic web project with jersey libs imported.
A note on this - I got an error for class not found and saw that I had to use Glassfish.org... instead of the com.sun one, don't know why, but there ya go.
My web.xml is as follows. No errors.
<?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>RestApi</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>
<display-name>Rest Web Services App by me</display-name>
<servlet>
<servlet-name>exampleServlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.rest.example</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>exampleServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
My java class is as follows. No errors.
package com.rest.example;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
#Path("/hello")
public class HelloWorld {
#GET
#Path("/{param}")
public Response getMsg(#PathParam("param") String msg){
String output = "Welcome to the world of Rest : "+msg;
return Response.status(200).entity(output).build();
}
}
You are using the old Jersey 1.x property
com.sun.jersey.config.property.packages
For Jersey 2.x it should be
jersey.config.server.provider.packages
As a general rule, anything where you see com.sun.jersey is for Jersey 1.x and org.glassfish.jersey is for 2.x.
try:
http://localhost:8080/rest/RestApi/hello
check my case
the rule seems like this: /{url-pattern}/{project}/{path}
I run my case with jetty
another example
the above issue can occur due to following reasons :
First check the name of your application deployed in the webapps folder of the tomcat, whether it is matching your url or not that is giving 404.But in the above case, as it is showing the welcome page, then it is not the concern here.
Check the url pattern mention in the web.xml as it must be the same as in the url you are hitting.
<url-pattern>/rest/*</url-pattern>
Third thing to check is that the path defined in the rest class with #Path annotation.
Check the web.xml for following entries if you are using jersey jars 2.x as suggested above by #Paul Samsotha
<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.pizzeria</param-value>
</init-param>
</servlet>
There may be a possibility to get this error when your server has been up without any errors but the context of your application hasn't. You can check it in the logs.For further information please refer this Link ---> 404 error due to SEVERE: Context [/example] startup failed due to previous errors
Related
I'm learning Java EE through a simple "Hello World" application, and when I run the program using index.html it works well. However, when I run the same program with index.xhtml, it throws a 404 Error saying "The requested resource is not available."
My folder structure is as follows:
When I run the program using index.html, I use the following URL: http://localhost:8081/index.html, and the page shows up with Hello World. And when I run using index.xhtml, I tried both: http://localhost:8081/example2/index.xhtml and http://localhost:8081/index.xhtml. Both give a 404 Error.
My web.xml is as follows:
<?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_3_1.xsd"
version="3.1">
<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>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
I've also tried multiple browsers (Chrome and Firefox). Any ideas why I'm getting a 404 on XHTML but not HTML?
You can config the home page in the web.xml with the code below:
<display-name>NameOfProject</display-name>
<!-- Configuration of your home page -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.xhtml</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
So you can use both:
http://localhost:8081/NameOfProject/ or
http://localhost:8081/NameOfProject/index.xhtml
The solution was pretty simple. When I checked the Tomcat logs, there was a ClassNotFoundException because Tomcat does not come with the jsf-api and jsf-impl jars. I created a new directory in WEB-INF called lib and put the 2 jars there. Yes, you need both jars. Now the XHTML file is found and the 404 goes away.
I wrote a very simple example using Jersey.
I downloaded the latest jar files from the jersey website into lib folder in WEB-INF.
My class and web.xml are below.
When I provide URL localhost:8080/SimpleJersey/rest/test I get 404 error (not found).
However when I use Maven, it works.
I use Eclipse Kepler, Glassfish 4 server and Java 7.
What am I doing wrong in the non-Maven version?
Thanks.
Class:
package com.simplejersey;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
#Path("/test")
public class MyResources
{
#GET
#Produces("text/plain")
public String getIt()
{
return "Hello there!";
}
}
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>SimpleJersey</display-name>
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.simplejersey</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
I found the solution in this post by Michal Gajdos: Jersey REST Web Service, Tomcat, Eclipse and 404's
The issue is (quoting from the abovementioned post):
Jersey 2.0 does not recognize init-param with name com.sun.jersey.config.property.packages (web.xml). Try to change it to jersey.config.server.provider.packages as described in ServerProperties.PROVIDER_PACKAGES (link)."
Be carefeul when you copy web.xml from websites that show older solutions or versions (like I did). Jersey is being updated too...
I am trying to demo simple Ajax/Servlet request.For this i have created a new Dynamic web project in eclipse and added a simple Servlet to take the request and present the same back to UI. I have included my Servlet details in web.xml. I am using Tomcat as my server. No Build scripts yet(i felt not needed at this point)
Servlet Code:
response.setContentType("text/html");
PrintWriter out =response.getWriter();
String txt = request.getQueryString();
out.println(txt);
Js Code:
$(function(){
$.get('/jirarequest','OK',function(op){
$('#maindiv').appendTo(op);
});
});
Html Code:
<?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>Jira-Synchronization</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>JiraSyncServlet</servlet-name>
<servlet-class>src.JiraSyncServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JiraSyncServlet</servlet-name>
<url-pattern>/jirarequest/*</url-pattern>
</servlet-mapping>
My Project structure:
My HTML:
I am getting a error in Fire bug saying
"NetworkError: 404 Not Found - http://localhost:8080/jirarequest?OK"
The URL i am using in browser is http://localhost:8080/Jira-Synchronization/index.html. i have double checked all kind of typo. I debugged by putting breaking point but the debugger never hit my Servelt. I am not sure what is wrong? URL is wrong or wiring is wrong or the way it set up something is missing?
Some info:
The 404 error means Tomcat doesn't have any file or servlet or anything exposed for serving at that URL path.
The 500 error means Tomcat has a servlet (or jsp) exposed at that URL path and an unexpected exception was generated while trying to run the java code.
Collection of things we know need to be changed:
$.get('/jirarequest/whatever','OK',function(op){
and
<servlet>
<servlet-name>JiraSyncServlet</servlet-name>
<servlet-class>JiraSyncServlet</servlet-class>
</servlet>
Plus you can test in the browser by typing this URL which should return OK:
http://localhost:8080/Jira-Synchronization/jirarequest/whatever?OK
I am testing a very simple REST server with Jersey and Servlet 3.0 implementation on Tomcat 7.0. I have programmed a simple PoJo:
package toplevel;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
#Path("/pojo")
public class PoJo {
#GET
#Produces("text/plain")
public String hello() {
return "Hello, World";
}
}
I have put the following in the WEB-INF/web.xml file (running on Servlet 3.0):
<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">
<display-name>RestTest</display-name>
<servlet>
<servlet-name>toplevel.PoJo</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>toplevel.PoJo</servlet-name>
<url-pattern>/pojo</url-pattern>
</servlet-mapping>
</web-app>
When I deploy, I get a HTTP Status 500 response. This seems to me that the webserver is recognizing that something should be served from /pojo, but that the corresponding class PoJo is not found. The jersey specific jars (version 1.17) are in the WEB-INF/lib dir:
activation-1.1.1.jar jersey-client-1.17.jar junit-4.9.jar
asm-3.3.1.jar jersey-core-1.17.jar persistence-api-1.0.2.jar
jaxb-api-2.2.4.jar jersey-server-1.17.jar stax-api-1.0-2.jar
jaxb-impl-2.2.4-1.jar jsr311-api-1.1.1.jar
Does anyone recognize this ?
You need to tell Jersey where to find your REST resource. Your web.xml should look something like this:
<servlet>
<servlet-name>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>toplevel</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Adding jersey-servlet:1.17.jar took care of that problem for me.
Within SpringSource Tool Suite I created a standard google app engine project. I added Jersey for REST support. The development server starts up fine, but when I try to GET a URL (e.g. http://localhost:8888/sibibjersey/api) I'm simply getting a 404. I suppose this is a simple configuration issue, but the solutions seems to hide away from me....
Here the main files:
web.xml looks like this.
<?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_2_5.xsd" version="2.5">
<init-param>
<param-name>com.sun.jersey.config.feature.DisableWADL</param-name>
<param-value>true</param-value>
</init-param>
<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>com.sibib.main</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
I tried variations of the url-pattern like /* and /rest/*, but none seemed to work.
The only Java class in com.sibib.main is InfoResource.java:
package com.sibib.main;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
#Path("/api")
public class InfoResource {
#GET
public String info() {
return "Hello Jersey on Google App Engine";
}
}
I tried adding #Path to the info function, but no effect. When I start the server and navigate e.g. to http://localhost:8888/sibibjersey/api I'm simply getting a 404.
Loading http://localhost:8888 loads the index.html in the war folder.
These are the lib referenced in the project:
activation-1.1.1.jar
appengine-api-1.0-sdk-1.6.1.jar
appengine-api-labs-1.6.1.jar
appengine-jsr107cache-1.6.1.jar
asm-3.3.1.jar
datanucleus-appengine-1.0.10.final.jar
datanucleus-core-1.1.5.jar
datanucleus-jpa-1.1.5.jar
geronimo-jpa_3.0_spec-1.1.1.jar
geronimo-jta_1.1_spec-1.1.1.jar
google_sql.jar
jackson-core-asl-1.9.2.jar
jackson-jaxrs-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
jaxb-api-2.2.4.jar
jaxb-impl-2.2.4-1.jar
jdo2-api-2.3-eb.jar
jersey-bundle-1.11.jar
jersey-client-1.11.jar
jersey-core-1.11.jar
jersey-json-1.11.jar
jersey-server-1.11.jar
jettison-1.1.jar
jsr107cache-1.1.jar
persistence-api-1.0.2.jar
stax-api-1.0-2.jar
Any hint is greatly appreciated!
Thanks!
Can you try removing sibibjersey from the context path, just try with http: //localhost:8888/api
In my local, I have not seen Google App Engine's application having project path along with http: //localhost:8888
I followed http://tugdualgrall.blogspot.com/2010/02/create-and-deploy-jax-rs-rest-service.html and it worked for me without any issues.