How to setup a Jersey + Tomcat RESTful Java web app? - java

Excuse me for my lack naivety of Java Web applications, but I can't seem to understand the best way to setup my Java application.
The Problem:
Currently I've been building a RESTful API application locally, using Jersey & Tomcat. I setup Tomcat via Intellij and all of my endpoint methods look like this. There's no main() method anywhere in my application and all is working fine.
#GET #Produces(MediaType.TEXT_PLAIN)
public Response getMessage() {
String message = "Please specify an endpoint.";
return Response.status(200).entity(message).build();
}
However, I'm trying to deploy my application to Heroku. Heroku is able to build it, but when I open up the URLs I get Application Errors and after checking the logs, I get the obvious:
2015-04-07T15:58:31.407658+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/gympeak/schools" host=gympeak-api.herokuapp.com request_id=02018a23-900d-4365-84a5-a54b82c4788a fwd="152.17.156.116" dyno= connect= service= status=503 bytes=
Basically telling me there's no web application running. I think part of this is because my application can launch Tomcat locally (since it's configured with Intellij), but not on Heroku. I've tried editing the Procfile to do something like this:
web: java -cp target/classes:target/dependency/* com._1834Software.endpoints.Index
But again, the problem is that it can't start anything since there's no main() method.
The Question:
How do I deploy a Java web application using Tomcat that does not have a main() method? Is this even possible? It seems to work fine locally but out in the wild, it doesn't.
I've seen people do something similar to this using Tomcat, but it doesn't make sense to me to do that when I'm able to launch Tomcat locally. http://blog.sortedset.com/embedded-tomcat-jersey/
Also, in case it helps, this is my web.xml file:
<?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>GymPeak API</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>GymPeak API</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

I think you may want to use webapp-runner to launch your WAR file. Here is an article on setting this up:
https://devcenter.heroku.com/articles/java-webapp-runner

You can start a Tomcat instance and deploy a war with your Jersey based application on it. This way you don't need a main method, just a standard servlet app.
You can check this project, that it's an example of that. It uses gradle to build the war.

Related

Serving Angular with Appengine Java Servlets

I'm wanting to serve my Angular application on Java servlets on Google Appengine. Currently, I build the angular application with ng b (have also tried ng b --prod. This builds the application into the /webapp folder.
To deploy, I use a gcloud app deploy. The app deploys successfully, though if I try to go to any routing other than /, it'll throw a 404 error. This is expected with Angular.
I tried adding a property to my web.xml file to redirect everything back to Angular's index.html this way:
<servlet>
<servlet-name>html-mapping</servlet-name>
<jsp-file>/index.html</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>html-mapping</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
This solution works locally, but doesn't work when deployed on Appengine. Any solutions to this?

What is the endpoint address of my web service deployed on Tomcat?

I am working with some code I didn't write. It is a web server deployed in Tomcat. I finally got the server up and running, but now I can't figure out what endpoint to call to get a response. Where can I find this info?
I know tomcat is running on port 9090 from inspecting the server in eclipse.
Here is the seemingly relevant portion of my sun-jaxws.xml
<endpoint name="DMS"
implementation="<path to class that implements the service>"
url-pattern="/services/ExternalDocumentService">
Here is part of my web.xml
<servlet-mapping>
<servlet-name>WebServiceServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
Here is a screenshot of the server info in eclipse
I already imported the WSDL into SoapUi, but I need to know what address to enter to actually reach my endpoint. Where can I find that address?

WebService is not "visible" in WebLogic 10.3

I am currently trying to let my application provide a webservice.
The application uses spring and is running under a Weblogic 10.3 instance.
I built the webservice following the "contract first" approach.
So what I basicaly have is a generated WS-Interface, my implementation of that interface, a web.xml defining the servlet-bindings and a sun-jaxws.xml defining the endpoint.
(more or less similar to this: http://www.mkyong.com/webservices/jax-ws/deploy-jax-ws-web-services-on-tomcat/).
Now, after deploying my application to weblogic, actualy everything is workign fine.
I can type the URL of the WebService into my browser, I see the WSDL, I can call it's methods.
If the weren't a small cosmetic fact:
In the deployments overview of WL when I click on the deployment, it shows me a list of WebServices...which is empty. So my webservice is NOT listed there.
So, can anyone tell me, what I have to do to get the webservice to show up there?
Though it's not really essential to have a webservice descriptor for JAX-WS, Weblogic at times fails to identify the WebServices(was not able to find a reason for this)
Below is what I did to get it working. Add the WebService implementation class as a Servlet in web.xml
<?xml version='1.0' encoding='UTF-8'?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" id="WebApp_ID">
<display-name>MyWebService</display-name>
<servlet>
<servlet-name>serviceServlet</servlet-name>
<servlet-class>com.aneesh.WebServiceImpl</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>serviceServlet</servlet-name>
<url-pattern>/Service</url-pattern>
</servlet-mapping>
</web-app>
and add the webservice descriptor (webservices.xml)
<?xml version='1.0' encoding='UTF-8'?>
<webservices xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.1">
<webservice-description>
<webservice-description-name>MyWebService</webservice-description-name>
<port-component>
<port-component-name>MyWebServiceSoapPort</port-component-name>
<wsdl-port xmlns:an="http://www.aneesh.com/service">an:MyWebServiceSoapPort</wsdl-port>
<service-endpoint-interface>com.aneesh.WebService</service-endpoint-interface>
<service-impl-bean>
<servlet-link>serviceServlet</servlet-link>
</service-impl-bean>
</port-component>
</webservice-description>
</webservices>
Depending on the developer that created the Web Service, deployment descriptors such as webservices.xml and weblogic-webservices.xml were added to the application. Descriptors are used for configuration, overriding default settings, and adding metadata. For Web Services this can be the endpoint, port configuration, linkage of the Web Service to EJB components, and so on. When deployed, the WSDL location of Web Services is listed in the WebLogic Console and the WSDL can be retrieved at runtime.
From the Trenches 2 | Patching OSB and SOA Suite to PS5
See also:
WebLogic Web Service Deployment Descriptor Element Reference
Developing Spring-Based Applications for Oracle WebLogic Server

Having Issues Uploading WAR File to WebSphere 6.1

I'm trying to deploy a web service onto WebSphere using a WAR file, which I have been told directly is completely possible and has been done many times before. WebSphere allows me to upload the file, specify the context root, and even start the application. However, when I try to access it by specifying my underlying URIs, WebSphere 404s on me. The relatively useless error message displayed is:
Error 404: SRVE0202E: Servlet [Jersey REST Service]: com.sun.jersey.spi.container.servlet.ServletContainer was found, but is corrupt: SRVE0227I: 1. Check that the class resides in the proper package directory. SRVE0228I: 2. Check that the classname has been defined in the server using the proper case and fully qualified package. SRVE0229I: 3. Check that the class was transferred to the filesystem using a binary transfer mode. SRVE0230I: 4. Check that the class was compiled using the proper case (as defined in the class definition). SRVE0231E: 5. Check that the class file was not renamed after it was compiled.
I have checked my naming conventions, modified my web.xml according to this blog post, attempted packaging it into an ear file (which threw out its own errors when I tried to upload it), and am trying to figure out what configurations I might have wrong. Any ideas of what I could change to make this work?
Edit
Here is the relevant part of my web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
id="WebAppId"
xmlns="http://java.sun.com/xml/ns/j2ee"
xsi="http://www.w3.org/2001/XMLSchema-instance"
schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>MYPROJECT'SDISPLAYNAME</display-name>
<servlet>
<servlet-name>Jersey REST 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>MYPROJECTNAME</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Another Edit
I'm using the newest release of Jersey- is that part of the problem?
Yet Another Edit
I'm pretty sure that's the entire problem. WebSphere 6.1 runs jdk1.5, and Jersey stopped supporting that after Jersey 1.2...
As you suspect your problem is lack for WebSphere support for Jersey (or rather JAX-RS).
I don't see JAX-RS in the list of supported APIs by WAS.
http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=/com.ibm.help.ic.WS.doc/info_sching.html
WAS 6.1 runs on J2SE 1.5 (as seen in the URL above)
Specification or API Version 6.1
Java 2 Standard Edition (J2SE) specification J2SE 5
These probably are the reasons behind the errors that you get to see in your WAS 6.1
HTH
Manglu

Java Web Service with backend stuff going on

I'm thinking about developing a Java Web Service using RESTEasy. I am going to follow this example: http://technicalmumbojumbo.wordpress.com/2009/10/13/restful-webservices-using-jboss-resteasy-tutorial/
Now, that's very well and good for getting the server to reply back with a simple response, however how do I get some "backend" things going on? For example, I want a queuing system running in the background constantly on the server, processing objects in the queue. When someone accesses a certain URL (The web service), i want the web service to Enqueue something..
The only thing I can think of in my head at the minute, which is probably totally wrong, is to make a seperate Java (J2SE) application, which runs the Queue, and connect the Web Service to it via RMI or Cajo or something..
I guess what I'm asking is that does a Java Web App running on Tomcat have any concept of a "main() method" which gets executed on server startup?
Any help is appreciated
Thanks
I guess what I'm asking is that does a
Java Web App running on Tomcat have
any concept of a "main() method" which
gets executed on server startup?
You can have a servlet get kick started on the load of the application using the appropriate load-on-startup tag in the web.xml file.
So for example:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<!-- snip stuff -->
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.lastname.firstname.YourStartupServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- snip stuff -->
</web-app>

Categories