The following tutorial shows how to run a simple Spring Boot RESTful service on SAP Cloud Platform.
About Spring Boot
Spring Boot allows you to quickly setup and run production-ready Spring based Java applications. With only a little configuration you are able to run different scenarios such as RESTful services, JPA-backed persistence, etc. For more on Spring Boot continue here.
Sample application
Our sample application supports only one GET operation to /test-api/products
returning a JSON array of three different products.
You may retrieve the code from the GitHub repository https://github.com/pliegl/sample-rest-product-service
Application walkthrough
You only need four little artifacts, to get your application up an running. We use Maven to take care of the project dependencies.
pom.xml
The necessary dependencies are configured in pom.xml. We are using two different profiles – one to run the application standalone on the local machine and one for running it in a Tomcat 8 on SAP Cloud platform. dev true org.springframework.boot spring-boot-starter-web prod false org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.apache.tomcat.embed tomcat-embed-el logback-classic ch.qos.logback org.springframework.boot spring-boot-starter-logging org.springframework.boot spring-boot-starter-tomcat provided
SpringRestApplication.java
Our main class. The @SpringBootApplication and @ComponentScan annotations are sufficient to let Spring Boot know, what to do. * Sample RESTful application */ @SpringBootApplication @ComponentScan(“com.ecosio”) public class SpringRestApplication extends SpringBootServletInitializer { //Make sure that everything for .war deployment is there @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringRestApplication.class); } public static void main(String[] args) throws Exception { SpringApplication.run(SpringRestApplication.class, args); } }
Product.java
Defines the structure of our products. We use Lombok to autogenerate getters and setter on a byte code level. /** * Sample product structure. Note that getters and setter are auto-generated by Lombok */ @Data @Builder public class Product { private String id; private String name; private BigDecimal price; }
ProductController.java
Defines our REST API. Currently, only a single GET method is supported. Additional operations such as POST, PUT, etc. may easily be added using the pertinent annotations and respective methods. /** * Serves a list of sample products */ @RestController @RequestMapping(“/test-api”) public class ProductController { private List products = new LinkedList(); @RequestMapping(method = RequestMethod.GET, value = “/products”) public List getProducts () { return products; } @PostConstruct private void loadDummyData() { products.add(Product.builder().id(“1”).name(“Apple”).price(new BigDecimal(“0.7”)).build()); products.add(Product.builder().id(“2”).name(“Pear”).price(new BigDecimal(“0.8”)).build()); products.add(Product.builder().id(“3”).name(“Orange”).price(new BigDecimal(“0.4”)).build()); } }
With these four little Java/XML artifacts we are ready to go.
Running the application
To run the application locally, run the following command on the command line: mvn spring-boot:run
To prepare the artifacts for deployment on SAP Cloud platform, run the following command on the command line mvn -P prod clean package
The final artifact sample-rest-product-service.war is located under the /target folder.
Deploying it on SAP Cloud Platform
* Login to SAP Cloud Platform on https://account.hanatrial.ondemand.com
* Chose JAVA Applications and then Deploy Application
* Select the previously generated sample-rest-product-service.war file and provide a name for your application. Select Java Web Tomcat 8 as runtime. Leave all other parameters unchanged.
* Check the status of your application
* In order to view the result of the GET request, click on the link as outlined below
The link shall look as follows: https://samplerestprodupXXXtria.hanatrial.ondemand.com/sample-rest-product-service/test-api/products
whereby XXX is specific to your SAP cloud platform account. Use a tool like Postman, to access the RESTful service. A regular Web browser will do as well for the simple GET request.
Summary
With only a few Java classes and clicks you are ready to deploy your first RESTful app into SAP Cloud Platform. http://bit.ly/2zbDEFf #SAP #SAPCloud #AI


