/rest_springboot-tomcat

Quickstart to expose a REST Greeting endpoint using SpringBoot and Apache Tomcat in embedded mode

Primary LanguageJavaApache License 2.0Apache-2.0

Introduction

This Mission showcases a basic mapping of a business operation to a remote endpoint. By taking this approach, clients leverage the HTTP protocol as a transport mechanism to call upon services. Application engineers define their APIs using a broad interpretation of REST fundamentals, encouraging freedom in design and quick prototyping.

As an application or service matures, this approach may not scale as desired to properly support clean API design or use cases involving database interactions. Any operations involving shared, mutable state will have to be integrated with an appropriate backing datastore; all requests here will be scoped only to the container servicing the request, and there is no guarantee that subsequent requests will be served by the same container.

This is recommended as an introduction to the mechanics of opening a service to be called upon by remote processes.

Prerequisites

To get started with this booster you'll need the following prerequisites:

Name Description Version
java Java JDK 8
maven Apache Maven 3.2.x
oc OpenShift Client v3.3.x
git Git version management 2.x

In order to build and deploy this project, you must have an account on an OpenShift Online (OSO): https://console.dev-preview-int.openshift.com/ instance.

Build the Project

Execute the following maven command to build the project:

mvn clean install

Run Locally

  1. Execute the following maven command to start the application:

    mvn spring-boot:run
    
  2. If the application launched without errors, use one of the following commands to access the HTTP endpoint using curl or httpie:

    http http://localhost:8080/api/greeting
    curl http://localhost:8080/api/greeting
    
  3. To pass a parameter for the Greeting Service, use one of the following commands:

    http http://localhost:8080/api/greeting name==Charles
    curl http://localhost:8080/api/greeting -d name=Bruno
    

Run on OpenShift Online

  1. Go to OpenShift Online to get the token used by the oc client for authentication and project access.

  2. On the oc client execute the following command to replace MYTOKEN with the one from the Web Console:

    oc login https://api.dev-preview-int.openshift.com --token=MYTOKEN
    
  3. Use Fabric8 Maven Plugin to launch an S2I process on the OpenShift Online machine & start the pod:

    mvn fabric8:deploy -Popenshift -DskipTests
    
  4. Get a route url to access the service:

    oc get route/springboot-rest
    
    NAME HOST/PORT PATH SERVICES PORT TERMINATION
    springboot-rest <HOST_PORT_ADDRESS> springboot-rest 8080
  5. Use host address to access the service HTTP endpoint:

    http http://<HOST_PORT_ADDRESS>/api/greeting
    http http://<HOST_PORT_ADDRESS>/api/greeting name==Bruno
    
    or 
    
    curl http://<HOST_PORT_ADDRESS>/api/greeting
    curl http://<HOST_PORT_ADDRESS>/api/greeting -d name=Bruno