Upload
jan-vosecky
View
828
Download
4
Embed Size (px)
DESCRIPTION
Tutorial for OpenShift beginners: Use Eclipse IDE to develop and deploy a Java based HelloWorld API function in 8 steps.
Citation preview
How to build & deploy a HelloWorld API function
using Java on OpenShiftin 8 steps
Jan Voseckytwitter: @jvosecky
In this tutorial
• For OpenShift beginners• IDE: Eclipse
– no command line used
• Language: Java• Goal: build and deploy a HelloWorld API function
Step 1: In Eclipse…
• Install Eclipse plugin for OpenShift– Menu Help > Eclipse Marketplace– In the search box, type: jboss tools– Choose the plugin depending on your version of
Eclipse
Step 2: OpenShift.com
• https://www.openshift.com/ – Register account– Login to your account, where you can:
• Create, manage, delete apps• Find Quickstarts and deploy them
• Documentation:– https://www.openshift.com/developers
Step 3: In Eclipse
• Create a new OpenShift application– New > Project… > OpenShift > OpenShift Application
• Login with your OpenShift account• Add your SSH keys
– If you don’t have SSH keys, the wizard can generate them for you
• Add 2 new windows in Eclipse:– Window > Show View > Other >
1. Git > Git staging
2. Jboss Tools > OpenShift Explorer
Structure of a Java web app
Your Java code (*.java)
Any other files needed by Java (config, txt, xml, etc)
Frontend (html, css, jsp, etc)
Step 4: HelloWord API function
package api;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = -3950937333898038206L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
PrintWriter out = response.getWriter();
out.println("{\"status\":\"Hello " + name + "!\"");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Under folder: src/main/java/api/
Step 5: Add a dependency
– Similar to “include a library” in a desktop Java application– Here we don’t include any JAR file, instead we point to a
central repository that contains *most* libraries– Open pom.xml:
• Tab: Dependencies• Seach for: javaee-web-api
– Wanna add other dependencies?• Search for them: e.g., “mysql”
Step 6: Update web.xml
<web-app version="3.0”
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false">
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>api.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/api/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
Under folder: src/main/webapp/WEB-INF/
Step 7: Commit and Push your code
1 2
3
Step 8: See the result!
• Frontend:http://[APP NAME]-[YOUR DOMAIN].rhcloud.com
• API:http://[APP NAME]-[YOUR DOMAIN].rhcloud.com/api/HelloWorld?name=Joe
Useful Links
• Presentations:– http://www.slideshare.net/XavierCoulon/devoxx2013-fr-jbdsopenshift?qid=15557
a00-3f56-4899-9572-dc03301e7bd6&v=default&b=&from_search=14
– http://www.slideshare.net/anoochit/using-openshift-paas?qid=15557a00-3f56-4899-9572-dc03301e7bd6&v=default&b=&from_search=13
• Getting started:– https://www.openshift.com/get-started
• Tutorial for Java & Eclipse:– https://
www.openshift.com/blogs/day-28-openshift-eclipse-integration-for-java-developers
• Lots of tutorials:– https://www.openshift.com/author/shekhar-gulati