Configuring a Jetty server with Glassfish servlet - java

I have a working web-application that I built, successfully deployed to Heroku, and is functioning well. However, I'm trying to tune the server/servlet config, and that's when I realized that I don't know what my application is actually doing.
For glassfish, this is the config that's needed.
However, for Jetty, this is the config that's needed.
I realized I have no idea which of the above my application is actually using, so I started digging in my code and found the following:
The main method being called by Heroku is instantiating the following Jetty server/webappcontext.
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
However, the jetty config seems to rely on a number of files (such as etc/jetty.xml, webapps folder or war files) which my project does not have at all.
In addition, my web.xml file defines the following servlet:
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
The fact that my application is defining a Jetty server but Glassfish servlet concerned me quite a bit. In an effort to standardize everything around Jetty, I tried adding the jetty servlet to my pom.xml dependencies and changed the above to:
<servlet-class>org.eclipse.jetty.servlet.DefaultServlet</servlet-class>
However, this change caused my application to break. It still compiles successfully and all my jerset-test based integration tests still succeed locally. But when I actually run the server, none of the routes work.
Some questions:
Is it a ill-advised to use a Jetty server along with a Glassfish servlet (container)?
If yes, what servlet (container?) should I replace Glassfish with, and what do I need to do to get the new Jetty servlet working?
If no, what config should I be using for my current setup? Should I be implementing the Glassfish config or the Jersey config?
I've spent many hours trying to read through various documentations, tutorials and stack-overflow threads, but they all either assume prior knowledge about servlets, JavaEE and related topics (none of which I'm familiar with), or they are oriented towards building brand new hello-world apps from scratch (as opposed to porting an existing working app over, which is what I'm trying to do). Any explanations you could give, without assuming prior knowledge, relevant to the context described above, would be much appreciated.
Edit: I think I'm starting to understand now that a Servlet is the code that generates the response for a request, and the ServletContainer is what provides the infrastructure for the Servlet. I've never had to deal with Servlets directly in building my web-app. Here's an example of what a route looks like in my app:
#Path(Ping.REST_PREFIX)
public class Ping {
static final String REST_PREFIX = "/ping";
#GET
public static Response get(#DefaultValue("getPing") #QueryParam("param") String param) {
return Response.ok().entity(param).build();
}
#Path("/pong")
#GET
public static Response getPong(#DefaultValue("getPong") #QueryParam("param") String param) {
return Response.ok().entity(param).build();
}
}
How can I port code like the above into a Jetty ServletContainer, without rewriting vast sections of my application?

You are not using a "Glassfish Servlet Container", you are using a "Jersey Servlet Container".
Jersey is the project you are using.
Glassfish was the umbrella organization (sometimes called "a forge") that helps manage/maintain the Jersey project (along with dozens of other projects).
Difference com.sun.jersey and org.glassfish.jersey
The Jersey project can now be found at the java.net organization.
https://jersey.java.net/

Related

Java service using Jersey won't deploy to Jboss

I'm trying to build a Java service that other services could call.
This service is not a WS, but is calling a RestfulWS.
I'm really just building a wrapper around this call. This would find the correct data it needs, set up the JSON for the call. Get a response and send it back up.
Was told to use Jersey for this. Trying to set up all the pom.xml to use Jersey.
Building code works fine, it is when the deploy to the server happens that things fail.
I get the error -- "JBAS011232: Only one JAX-RS Application Class allowed. "
I don't have a web.xml, which I guess is used to skip some ResetEasy files.
I do have exclusions in pom.xml and jboss-deployment-structure.xml.
I still get the error when deploy happens. Not really sure what else to check.
It looks like you have a problem with JAX-RS dependencies. JBoss already has its own implementation of JAX-RS and probably that’s causing the issue. Some solutions are already suggested here Jboss error: Only one JAX-RS Application Class allowed

java servlet context listener does not start

Under tomcat7, I used both #WebListener on the class and
<listener>
<listener-class>helpers.MyListener</listener-class>
</listener>
in web.xml. It starts on my localhost tomcat and runs fine, but not on tomcat7 on another server. No error message in the logs I added a static initializer to log it when it starts, but there's no logged message, so the listener is just not starting. Any tips?
I think that you are trying to use tomcat 7 for Servlet 3.0 which includes annotations support. I am no servlet expert, I have just started learning servlets 3.0(especially). For annotations I came across this resource called caucho technology. This website helped me a lot to learn annotations. I encountered the same problem as yours.

serving static content on a Jersey web app with descriptor-less deployment

I am creating a REST based web application and using the latest version of Jersey and embedded Jetty. Descriptor-less deployment for Servlet 3.0 allows the exclusion of web.xml. However, I am having trouble understanding how you can serve static content in this case (.html, .css, .js and image files)? As the application is using embedded Jetty, I am not deploying this in a standalone servlet container, so the resulting file is a shaded .jar. Perhaps, I need to use the WAR plugin to make this possible? If so, where exactly do I have to place the aforementioned files? Is it still possible with packaging into .jar only as well?
The only way I have been able to serve static content so far, was with direct filesystem access, like this (Jersey's JAX-RS part):
#GET
#Path("manager/preview/img/loader.gif")
#Produces("image/gif")
public final Response preview() throws Exception
{
final byte[] b = Files.readAllBytes(Paths.get("manager/preview/img/loader.gif"));
return Response.ok().entity(b).build();
}
This definitely does not seem like the appropriate way. I'd be thankful, if someone could point me in the right direction.

Jersey 2.5: How can a resource access external classes instances

I have embedded Jetty container inside my main server and I also use Jersey 2.5 for handling REST resources.
everything seems to work well and now I would like to pass my server's context class into each of my REST resources.
I'm having hard time understanding how to do that...
If someone can provide full code example - it could be awesome!
Many thanks
What exactly do you mean when you say you have a Jetty container inside your "main server"? Are you programmatically executing Jetty within the application? Also, when you say "context" are you referring to the ServletContext?

Change JAX-WS listening url

Alright, this might look like a duplicate question but I have struggled a lot but haven't found a proper solution. Closest match I found is this: JaxWS application URL in Tomcat
But this is using something different.
I am using Glassfish 3 and developing in Netbeans 7.4 along with Spring, Spring MVC. I have developed my webservice using following code:
#WebService(serviceName = "FooService")
public class FooWS {
#WebMethod(operationName="getHelloWorld")
public String getHelloWorld() {
return "Hi";
}
}
Now the problem is that, it start listening at this address http://localhost:8080/myapp/FooService
But I want it to listen at http://localhost:8080/myapp/ws/FooService.
I do not have any xml in my project for configuring web services. I only have following xmls in WEB-INF
applicationContext.xml
dispatcherServlet.xml
glassfish-web.xml
spring-security.xml
web.xml
Except glassfish-web.xml netbeans haven't generated any code for my webservice in any other xml file. And also in this file there is no source, just there is a section for Web Services which shows this web service, I am attaching image for the same.
Does anybody knows where should I make changes to change listening url? Or any annotation that might help?
I found this answer https://stackoverflow.com/a/8975619/601168 but I guess this is for clients only.

Categories