Injected ServetContext is null on Grizzly, not null in TomCat 7 - java

I'm working on a Jax-RS RESTful web-service. While I'm still coding (in Eclipse), I'd like to be able to debug easily, so I decided to have a Maven project and I use dependencies to the Grizzly web server, allowing me to start up a server with merely 2 lines of code (not even having to build the WAR file).
In my web-service implementation class (the one with #Path) I have injected the context as member variable:
#javax.ws.rs.core.Context
ServletContext context;
When I check the context variable for null, it will not be null if I build the WAR file and deploy it in TomCat, but it will be null when I start my Grizzly server and check it then. Starting the Grizzly v1.9 server is done as follows:
String url = "http://localhost:1234";
SelectorThread srv = GrizzlyServerFactory.create(url);
I've tried to find solutions to this with Google, someone suggested to enable "load-on-startup" in the web.xml, but this didn't help either.
Any ideas?
Cheers!

My guess is that you use Grizzly without the Servlet Container extension. Theoretically JAX-RS may run not only on servlet environment, but then you cannot get the ServletContext or HttpServletRequest or Response.

While the other answer here made some sense, I wasn't too sure, because according to the maven dependency tree, the servlet package was also included.
I could fix the problem by migrating to a different maven dependency. I'm now using
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-grizzly2</artifactId>
<version>1.9.1</version>
</dependency>
and followed the instructions of http://blogs.oracle.com/PavelBucek/entry/jersey_grizzly_2_x_integration (Sample) to make it work. The servlet context is now successfully injected.

Related

Can a JBOSS 7+ servlet listen/take over another subdeployment's requests?

Currently working an application that is moving from JBOSS 4 (java 6) to JBOSS 7.1 (Java 8). It had no REST WS - only SOAP and worked with Servlets.
I added my WAR containing a basic REST Service (ApplicationPath class with 1 resource class and a web.xml (empty since I'm using RESTEasy implementation which should scan my classes)) to the main EAR.
During deployment, I see no errors in my logs and it says my .war has been deployed and my web context registered.
However, whenever I call one of my resource's endpoint I get the default JBOSS response for Error 404 Not Found (the url is correct though) and when I try to call the same URL but by using PUT or POST I get the message "Http Method POST is not supported by this URL" which is typically an error from Custom Servlets.
Is it possible that another Servlet is listening to my path or taking over things? Even if outside of my web context? How could I investigate/debug this?
The problem came from the old RESTeasy Version - 3.0.7-Final that was being used, which has some known bugs with the #ApplicationPath annotation and web.xml configuration. After using the provided version of RESTeasy from the JBOSS 7 (version 3.6.1-SP2), everything worked like a charm.

Double slash in context path in Websphere Application Server

I would like to know if it is possible to put a context path like this when deploying a WAR to Websphere Application Server: /api/v3.1, and another one with the same context path but for another version of the API to be launched in the same WAS.
In my team we want to version the API we are exposing to the clients, we have a monolithic application created with Spring Boot that is deployed into a WAS v8.5.
For that we have in mind the approach of versioning the API putting the version in the URI:
https://example.com/api/v2.1
https://example.com/api/v3.0
It looks like it deployed correctly but when consuming it says that there is no file mapped to those URIs.
Yes. It is possible (and helpful for API versioning with the URI). The issue was to putting '/' (slash) at the end of the context path. It is valid only at the beginning (or when it is empty).

Apache Karaf Rest Service Issue

I am having a WAB application which is just having only one html file, and its working fine. the code is available on the below git link
https://github.com/vineethvnair0/Karaf/tree/master/first-wab
Now I want to define a rest service in the wab, with the same context root as my web app for example like below.
http://localhost:8181/first-wab/rest/hello
Please let me know how can i do it?
Simply add a CXF servlet to the web.xml. The exact solution depends on how you expose your Rest endpoints. Do you plan to use spring?

Embedded Tomcat, Jersey, and WebApplicationInitializer not loading

I am attempting to embed tomcat in my existing Jersey web application.
I am able to get Tomcat running no problem, and even loading my JSP's from the webapp dir. The issue is getting it to see my WebApplicationInitializer class like it does in the full container. I'm fairly certain this is a class path issue, if I add a VirtualDirResource and set the context to it, my Jersey app loads. The issue there is, Jersey can no longer load my JSP Viewables. It returns 404. Again, I have no web.xml anywhere. Not sure what code would be helpful, so ask for code samples if you think you have an answer.

Load webapp on container startup without web.xml in Tomcat+Jersey?

I currently have a Jersey webapp without a web.xml. It deploys nicely, but doesn't start up until it receives its first web request.
To get the webapp to load at startup, I could create a web.xml for the webapp and give a load-on-startup tag. However, I'd strongly prefer not to make a web.xml.
Is there a way to get a JAX-RS application to load at startup without web.xml? I'll even accept a solution that is specific to Jersey and/or Tomcat.
EDIT: I would also accept a solution that loads all apps in a Tomcat instance eagerly.
EDIT: Let me give a little more information on how the app is being deployed, per a comment.
The deployment process is not sophisticated.
The App will live on an EC2 instance running Ubuntu 12.04. I'm setting up one instance of the App by hand; once it works, I will make an AMI of the app and create additional copies of it as needed.
To deploy the app on the initial instance, I'm simply copying the WAR file to /var/lib/tomcat7/webapps and restarting Tomcat. No other webapps will be running on this Tomcat instance.
If any additional information would be useful, let me know! I'll happily add it.
EDIT: For clarity's sake, this is how my webapp Application class looks, at a high level:
#ApplicationPath("/")
public class App extends ResourceConfig {
// ...
}
I'm using the Jersey-specific ResourceConfig class instead of the more general JAX-RS Application class because I'm using Jersey's built-in HK-2 to do some dependency injection.
The only way I can think of to do that is to switch to setting up the Jersey ServletContainer yourself and set its "load on startup" value to something greater than zero. You might use a ServletContainerInitializer (no relation--the naming is just a coincidence) to do it. If you happen to be using Spring, its WebApplicationInitializer offers the same mechanism with a slightly more convenient interface.
Another, rather hacky, way would be to write a class that extends ServletContainer and give it an appropriate Servlet 3.0 annotation, something like #WebServlet(value="/", loadOnStartup=1)
One solution would be to force a first request to the app by simply adding a call to curl or wget to your deployment script. It has the additional advantage of warming up any caches. And it can be used for testing if the deployment and the app really work. (Just check HTTP status code or some text on the response page...)

Categories

Resources