Deploy Jersey web services in referenced .jar - java

I've created a .jar with some Jersey web services in it, and I want to import that .jar in other projects. Those web services work like a handler/proxy for local code to link with a remote service provider.
So, I import the .jar document in a project and configure web.xml like this:
<servlet>
<servlet-name>Services</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.package.for.services.in.current.project, com.package.for.services.in.jar</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Services</servlet-name>
<url-pattern>/s/*</url-pattern>
</servlet-mapping>
But when I run the project, the web services specified in com.package.for.services.in.jar aren't found.
The .jar document is in build path. What am I doing wrong? Isn't possible to run web services from an external library?
Thanks in advance.

I found the answer from this post: Jersey REST The ResourceConfig instance does not contain any root resource classes
The PackagesResourceConfig class depends on having a directory with the name of the package in order to find the classes within it. I went back to Eclipse, and found an option at the bottom of the "Export ... > Jar" dialog for "Add directory entries." I turned that on, exported the jar again, and then the scanning found my resource classes. You'll need to find some comparable option in your build environment.

Related

How to expose custom jar in lib folder of java web application?

I have an existing jetty application that i need to modify. Basically i just need one access point that can handle POST requests. I have access to the WEB-INF directory of the project. There i have web.xml file and a lib folder. As i understand from jetty documentation jars under lib will be loaded automatically. Is it possible to add a custom jar under lib folder and the somehow configure web.xml so that my jar will handle HTTP requests from a certain URL? If so how would i start with that?
You can simply create a jar with your Servlet class inside (a class that extends HttpServlet) and add the servlet declaration inside the web.xml
<servlet>
<servlet-name>A_Name</servlet-name>
<servlet-class>your.new.ServletClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>A_Name</servlet-name>
<url-pattern>/the/path/you/want</url-pattern>
</servlet-mapping>

how to access PDF/image/video file located in java server from a browser URL?

I have an mp4 video on my GlassFish java application server. What URL do I use to access it in a browser?
The video path in the application on the server is:
myapp/WEB-INF/videos/myvideo.mp4
I tried adding this to my web.xml file:
<servlet>
<servlet-name>myvideo</servlet-name>
<servlet-class>videos.myvideo.mp4</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myvideo</servlet-name>
<url-pattern>/myvideo</url-pattern>
</servlet-mapping>
and restart the server, then go to the URL: https://www.example.com/myapp/myvideo but that just gave this error:
java.lang.ClassNotFoundException: videos.myvideo.mp4
Obviously it's not a servlet, but not sure what else to try.
Why are you trying to access the video as a servlet and not as a static asset, like you would an image? Using a servlet mapping is obviously wrong.
Instead if you have a running WebApp in GlassFish you should be able to access it via https://www.example.com/myapp/videos/myvideo.mp4 assuming you've put the videos folder under your webapp's j2ee root.
The j2ee root would be configured in your application.xml as the web-uri. In your case it is likely that the myapp folder is your j2ee root. So move your videos up a folder.
(Converted comment 'discussion' into an answer).

How to support variable interpolation in web.xml?

I'm packaging a web application and tomcat into a zip file. This zip should be able to use in any location or path.
This web application require to load an additional folder (response-folder) to be able to startup.
Due to the zip can be unzip anywhere, I need to find out my current path when the server is started and set the path in the startup.bat.
The following is my zip folder structure:
my.zip
- apache-tomcat
- bin
- startup.bat
- webapp
- mywebapp
- WEB-INF
- web.xml
- response-folder
In my startup.bat, I had the following line
set CATALINA_OPTS=-Dapache-tomcat-current-folder="%TOMCAT_CURRENT_FOLDER%"
I would like achieve something like below:
<servlet>
<description>MyService</description>
<display-name>MyService</display-name>
<servlet-name>MyService</servlet-name>
<servlet-class>com.my.package.MyService</servlet-class>
<init-param>
<description>Folder that contains response files</description>
<param-name>RESPONSE_FOLDER</param-name>
<param-value>${apache-tomcat-current-folder}\response-folder</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Note: I'm not able to the change the web application java code, as the web application is not belong to me.
May I know is there anyway to make web.xml to support variable interpolation or achieve behavior such as above?
Any help would be great.
To use environment variables in web.xml, you can add
set "CATALINA_OPTS=-Dapache-tomcat-current-folder=somevalue"
or if you want to reference a System environment variable
set "CATALINA_OPTS=-Dapache-tomcat-current-folder=%apache-tomcat-current-folder%"
in bin/setenv.bat (bin/setenv.sh for *nix).
You need to create this file.

REST API conflicting with Hadoop

This is the error I am getting.
com.sun.jersey.api.container.ContainerException: A root resource, class
org.apache.hadoop.hdfs.server.datanode.web.resources.DatanodeWebHdfsMethods,
has a non-unique URI template /
I am using Hadoop and REST API.
This is how I am initialising REST API in web.xml
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>
com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/RESTMOB/CALL/*</url-pattern>
</servlet-mapping>
I have tried removing the Hadoop JARs and it works, but I need those Hadoop JARs so what's the alternative. Thanks in advance
what is likely happening is some .class file(s) in the jar you removed are also present in the other jars - this is common in java and can create issue restarting your app and it might load different versions on restarts. I would open up the jars and search for duplicate class files and remove them from one jar and test. You can extract the jars to file system and search for all .class files and sort by name. Its always a good idea to eliminate duplicate .class files when possible even though it requires some tedious work

Manually adding file to classpath

I'm trying to test a war that I deployed in Tomcat 7, however it seems its missing a xml configuration:
web.xml
<servlet>
<servlet-name>StorageEngine</servlet-name>
<servlet-class>com.jpeterson.littles3.StorageEngine</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:StorageEngine-servlet.xml</param-value>
</init-param>
</servlet>
I have downloaded the StorageEngine-servlet.xml separately, to be able to satisfy this init-param where do I need put the
xml file in the \webapps\littleS3-2.3.0\WEB-INF folder, just relative to the web.xml? Or?
No, it belongs in WEB-INF/classes of your WAR file. That is always in the CLASSPATH of a Tomcat app, loaded by the app class loader.
Given the mapping it would need to be placed in the root of your classpath. Try placing it within the root of any source folder

Categories