Bare minimum necessary for a single Jersey GET resource? - rest

I am missing something obvious, but I'm not sure what. I have a single "HelloWorld.java" that has a single #GET method that returns some text.
My web.xml was taken from this doc (described as "An even simpler approach is to let Jersey choose the PackagesResourceConfig implementation automatically...."):
<web-app>
<servlet>
<servlet-name>HelloWorld</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.hello.rest</param-value>
</init-param>
</servlet>
</web-app>
Here's my class (mostly taken from here):
package com.hello.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;
#Path("/helloworld")
public class HelloWorld {
#GET
#Produces("text/json")
public String getHelloWorld() {
return "{\"hello\":\"World\"}";
}
}
I use ant to build a war file, and deploy it to tomcat. The war appears correct because tomcat unzips it and I can access my static index.html that I put in it for testing. But accessing localhost:8080/helloworld gives me a 404. There must be some other piece I need in order to get Jersey working. What did I miss?
Thank you Bozho, I was missing the <servlet-mapping> section. Actually it appears I don't want a "/" as url-pattern, because that prevents serving static content (I can't get my index.html page any more!) so here's my new web.xml (I put my resource in the "/data/" path):
<web-app>
<servlet>
<servlet-name>HelloWorld</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.hello.rest</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/data/*</url-pattern>
</servlet-mapping>
</web-app>
Now I can access my index.html page as http://localhost:8080/hello/index.html, and my resource at http://localhost:8080/hello/data/helloworld.

You have to map your servlet with <servlet-mapping>, with a / as url-pattern

Related

How to deploy JAX-RS Application in WAS 7.0.23?

I am using RAD and WAS 7.0.23, and try to deploy jax-rs in it. But I am getting below error in deployment descriptor(Web.xml).
Error 404: javax.servlet.UnavailableException: SRVE0200E: Servlet
[com.ibm.websphere.jaxrs.server.IBMRestServlet]: Could not find
required class - class java.lang.ClassNotFoundException:
com.ibm.websphere.jaxrs.server.IBMRestServlet
Servlet Mapping
<servlet>
<description>JAX-RS Tools Generated - Do not modify</description>
<servlet-name>JAX-RS Servlet</servlet-name>
<servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>APPLICATION CLASS</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
JAX-RS wasn't added to WAS until 8.0. If you want to use JAX-RS on that old a server (which I generally wouldn't recommend for the reason Andy mentioned above), you'll need to bring your own provider.
First, I agree that continuing to use the well-out-of-support 7.0 is unwise.
That said, it may just be that you need to add the jar to the RAD project's "Deployment Assembly", in addition to the "Java Build Path".

Jersey REST Web Service, Tomcat, Eclipse and 404's

I've read through a number of posts, but just can't seem to solve my problem. You'll also see tons of posts very similar to this one, even the same tutorial. Even following them, I can't seem to get to the answer.
Essentially, I'm trying to follow the simple tutorial at: http://www.vogella.com/articles/REST/
I've made a few changes to make it compatible with Jersey 2.x
I'm using:
Eclipse
Tomcat 6 (Deployed/Run as within Eclipse)
jaxrs-ri-2.0
I've enabled the JAX-RS Facet in Eclipse
Everything builds fine
Tomcat starts fine within Eclipse
I can get to a static page content via:
http://localhost:8080/RestTEST2/index.html
However, when I try to access my service via:
http://localhost:8080/RestTEST2/jaxrs/hello
I receive a 404 with "message not found" and "The requested resource (Not Found) is not available."
Here is my web.xml which is located at /WebContent/WEB-INF/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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>TestREST2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<description>JAX-RS Tools Generated - Do not modify</description>
<servlet-name>JAX-RS 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>TestREST</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Servlet</servlet-name>
<url-pattern>/jaxrs/*</url-pattern>
</servlet-mapping>
</web-app>
Here is my Java class:
package TestREST;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
// Plain old Java Object it does not extend as class or implements
// an interface
// The class registers its methods for the HTTP GET request using the #GET annotation.
// Using the #Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML.
// The browser requests per default the HTML MIME type.
//Sets the path to base URL + /hello
#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";
}
// This method is called if XML is request
#GET
#Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
}
// This method is called if HTML is request
#GET
#Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
}
}
I also have a JAX-RS User Library configured and referenced that includes all the JAX-RS jars.
Thoughts on what would cause the web service to not be found?
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.
UPDATE (2020): try this link for current apidocs ServerProperties.PROVIDER_PACKAGES:
Thanks a lot.. i've been fighting for it as well.
This combination worked for me:
Tomcat 7.0.55
Eclipse Luna
Java 1.6
Jersey 1.7
web.xml:
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.trgr.cobalt.cmdb.jersey.resource;com.trgr.cobalt.cmdb.jersey.beans;com.trgr.cobalt.cmdb.elasticity</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>
I would like to add one answer in this post.I was struggling with same problem for two days and couldn't find the solution.
I tried all the possible solutions provided here but later on I realized that server was giving error of
org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException
This link answers gives the solution if somebody is stuck like me..
org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException
Actually first answer given in this post suggest the solution which is for jersey 2.x bundle and if you are using jersey 1.x then it will keep on giving error.
Kindly refer to link given in answer for further reference
Simply by modifying the configuration of Apache Tomcat v7.0 while creating Dynamic Web Project, to include REST, I solved my problem. It is not enabled by default.
Recently I found myself stuck in this problem. I have got it resolved by following these two points as the solution:
By making the src/main/java as the source directory, and keeping the Project Structure as shown below:
Changing com.sun.jersey.config.property.packages to jersey.config.server.provider.packages in the web.xml

what's the "correct" practive for designing the interface for this REST web service?

I'm building a REST web service to manage customers and customer orders.
I'm using Eclipse 3.4 with JAX-RS (Apache Wink 1.0) on WebSphere 7.
I have a web project defined in web.xml like so ...
<servlet>
<servlet-name>JAX-RS Servlet</servlet-name>
<servlet-class>org.apache....RestServlet</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.mydomain.ws.CustomerWS</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Servlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
/***/
</servlet>
... which gives me a URL like http://.../ws/customers that returns all customers.
Now what I need to do is have something like http://.../ws/orders that returns all orders.
My questions are,
I want to add a second web service -- what do I add to the web.xml so that new web service is visible? or am I supposed to create a totally new web project for my second "orders" web service?
or any other ideas? Not sure how to design this "correctly".
Thanks, Rob
I'm not familiar with Wink, but assuming it's JAX-RS-compatible, you should not need any web.xml changes.
If your container is JAX-RS aware,
then annotating your 2nd class with #Path should automatically deploy it
else you can add that new resource class to your REST Application, e.g. based on the example you linked and speculating on your resource class names:
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(CustomersResource.class);
classes.add(OrdersResource.class);
return classes;
}

Jetty and GWT (Google Web Toolkit)

As I understand it, GWT uses an embedded Jetty server. Can
anyone please tell me where I can find the Jetty .xml configuration files
used by GWT? I have a webapp which makes uses of Jetty's
ContinuationFilter and ProxyServlet. The app works fine under GWT but
fails when run in a separate Jetty instance outside of GWT. If I can
replicate the GWT Jetty config then I think I'll be okay.
Edit for more info:
My webapp's web.xml reads as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<filter>
<filter-name>JettyContinuationFilter</filter-name>
<filter-class>org.eclipse.jetty.continuation.ContinuationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>JettyContinuationFilter</filter-name>
<url-pattern>/bugzilla/*</url-pattern>
</filter-mapping>
<!-- Servlets -->
<servlet>
<servlet-name>greetServlet</servlet-name>
<servlet-class>com.searchsystem.gwt.server.GreetingServiceImpl</servlet-class>
</servlet>
<servlet>
<servlet-name>jetty-proxy-servlet</servlet-name>
<servlet-class>org.eclipse.jetty.servlets.ProxyServlet$Transparent</servlet-class>
<init-param>
<param-name>ProxyTo</param-name>
<param-value>http://localhost/</param-value>
</init-param>
<init-param>
<param-name>Prefix</param-name>
<param-value>/</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>greetServlet</servlet-name>
<url-pattern>/dashboard/greet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jetty-proxy-servlet</servlet-name>
<url-pattern>/bugzilla/*</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>Dashboard.html</welcome-file>
</welcome-file-list>
</web-app>
and the link to my Bugzilla installation is in this form:
com.google.gwt.user.client.ui.Frame bugFrame = new Frame("/bugzilla/");
Running under Jetty 6.1.26, I get this output:
Request Attributes
Attribute: Value:
javax.servlet.forward.request_uri /bugzilla/
org.mortbay.jetty.error_page /jspsnoop/ERROR/404
javax.servlet.forward.servlet_path /bugzilla/
testFilter 1
javax.servlet.error.message NOT_FOUND
requestInitialized ''
javax.servlet.forward.context_path
javax.servlet.error.status_code 404
javax.servlet.error.servlet_name default
org.mortbay.jetty.newSessionId 47deq3eo5kblxfrvtc5rljrg
javax.servlet.error.request_uri /bugzi
lla/
there is no jetty.xml. GWT sets up the Server programmatically.
You can find the setup in
com.google.gwt.dev.shell.jetty.JettyLauncher
contained in the gwt-dev.jar
See: Serving a GWT Application with an Embedded Jetty Server by Brandon Tilley (code extract shown below). He seems to have achieved it quite seamlessly, a process which I will be confirming myself tomorrow.
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.webapp.WebAppContext;
public class EmbeddedGwt {
public static void main(String[] args) throws Throwable {
// Create an embedded Jetty server on port 8080
Server server = new Server(8080);
// Create a handler for processing our GWT app
WebAppContext handler = new WebAppContext();
handler.setContextPath("/");
handler.setWar("./apps/GwtApplication.war");
// If your app isn't packaged into a WAR, you can do this instead
WebAppContext altHandler = new WebAppContext();
altHandler.setResourceBase("./apps/GwtApplication");
altHandler.setDescriptor("./apps/GwtApplication/WEB-INF/web.xml");
altHandler.setContextPath("/");
altHandler.setParentLoaderPriority(true);
// Add it to the server
server.setHandler(handler);
// Other misc. options
server.setThreadPool(new QueuedThreadPool(20));
// And start it up
server.start();
server.join();
}
}

gwt - problem accessing servlet in inherited module

I'm trying to divide my app into modules and I'm stuck with this problem:
I have a widget MapServiceWidget in one module called "webvisualisation" that uses the RPC to get the data from MapService Rpc interface. I'm inheriting this module in another GWT module called "led" (I packed "webvis..." into jar with sources, added in module "led" deffinition). Then I try to create this widget in the second ("led") module and get message
"Problem accessing /led/mapservice reason NOT FOUND".
And sure it can't find it cause mapservice is defined in inherited "webvisualisation" module.
The question is why it's looking for this servler implementation in "led" module not in "webvisualisation" where it's defined? I checked all module definitions and web.xml files several times and consulted documentations, it seems ok.. but it's not. If my description is not clear I can post some config/source files.
This is web.xml for webvisualisation module
<!-- Servlets -->
<servlet>
<servlet-name>mapservice</servlet-name>
<servlet-class>pl.gmike.webvis.server.MapServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mapservice</servlet-name>
<url-pattern>/webvisualisation/mapservice</url-pattern>
</servlet-mapping>
And for led it's just ordinary generated sample file
<!-- Servlets -->
<servlet>
<servlet-name>greetServlet</servlet-name>
<servlet-class>pl.led.server.GreetingServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>greetServlet</servlet-name>
<url-pattern>/led/greet</url-pattern>
</servlet-mapping>
Seems that you're bumping into a classpath problem. Maybe check that your webvisualisation.jar is in the WEB-INF/lib directory of your web application.
I got it working. I just added servlet and servlet mapping entries to "led" modules web.xml so it look like this now:
<!-- Servlets -->
<servlet>
<servlet-name>greetServlet</servlet-name>
<servlet-class>pl.led.server.GreetingServiceImpl</servlet-class>
</servlet>
<servlet>
<servlet-name>mapservice</servlet-name>
<servlet-class>pl.gmike.webvis.server.MapServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>greetServlet</servlet-name>
<url-pattern>/led/greet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mapservice</servlet-name>
<url-pattern>/led/mapservice</url-pattern>
</servlet-mapping>
As You can see the mapservice servlet is mapped here to /led/mapservice URL where GWT seems to look for it, unlike in original "webvisualisation" module web.xml where it was mapped to /wevisualisation/mapservice .
I'm not very satisfied with this solution, it works but it requires adding a servlet mapping in WebApps web.xml for every servlet in inherited module that I want to use or that is used somewhere in this inherited module.
Still I would like to know why servlet definitions and mappings from inherited modules are not included in WebApps web.xml during compilation/linking... I think it should work without such hacks, so there's something I'm doing wrong.