Java: Hello, World! at

Otavio Santana
Otavio Santana
Developer Relations
06 Jun 2019

Today, is announcing support for Java technologies. And in this post, we’ll cover how easy it is to either create or move your Java application to

A quick refresher: a second-generation Platform-as-a-Service, is built especially for continuous deployment. With, you can host web applications in the cloud while making your development and testing workflows more productive. As the premier tool for developing and delivering applications in Drupal, WordPress, Symfony, Typo3, EZ, and Magento, also supports an array of languages, including PHP, Node.js, Python, Ruby, and Go.

Now, back to Java! We’ll show a Spring-Boot application connected to a MySQL database instance using Maven to automate the build and install. In the Java world, Spring Boot makes it easy to create stand-alone, production-grade, Spring-based applications that you can just run.

Show me the code supports common Java tools, and so we’ll use the Spring Initializr to create a Spring Maven Project with Java 8, and append the MySQL driver as a dependency. There’s also a configuration library that reads details, like routes and environmental variables, and exposes them to the application. Here’s the pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""







Spring Data JPA, part of the larger Spring Data family, makes it easy to quickly implement JPA-based repositories. This module deals with enhanced support for JPA-based data access layers, making it easier to build Spring-powered applications that use data access technologies. In this example, you store Person objects annotated as a JPA entity.

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Objects;

public class Person {

	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	private String name;

	private Integer age;

 //getter and setter

Spring Data JPA focuses on using JPA to store data in a relational database. Its most compelling feature is the ability to create repository implementations automatically, at runtime, from a repository interface.


public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {

After creating the model and the repository, the last step in the Java application is the controller with a REST. The @RequestMapping annotation provides “routing” information. It tells Spring that any HTTP request with the / path should be mapped to the home method. The @RestController annotation tells Spring to render the resulting string directly back to the caller.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

public class PersonController {

	private PersonRepository repository;

	@ResponseStatus(code = HttpStatus.CREATED)
	public String save(@RequestBody Person person) {;
    	return "Saved- " + person.getName();

	@GetMapping(value = "/{id}", produces = "application/json")
	public Person get(@PathVariable("id") long id) {
    	return repository.findById(id).orElseThrow(() -> new RuntimeException("Not found"));

	@GetMapping(produces = "application/json")
	public Iterable<Person> get() {
    	return repository.findAll();

	@PutMapping(value = "/{id}", produces = "application/json")
	public Person update(@PathVariable("id") long id, @RequestBody Person person) {;
    	return person;

	@DeleteMapping(value = "/{id}", produces = "application/json")
	public Person delete(@PathVariable("id") long id) {
    	Person person = repository.findById(id).orElseThrow(() -> new RuntimeException("Not found"));
    	return person;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class App {
	public static void main(String[] args) {, args);


The final part of our application is a startup. It’s a standard method that follows the Java convention for an application entry point. SpringApplication bootstraps the app, starting Spring on a Tomcat web server. structure

The Java application is ready to go! The next step is to set the files required to manage and deploy the demo. Every application you deploy on is built as a virtual cluster, containing a set of containers. There are three types of containers within your cluster:

  • One Router (.platform/routes.yaml). allows you to define the routes.
  • Zero or more service containers (.platform/services.yaml). allows you to completely define and configure the topology and services you want to use on your project.
  • One or more application containers ( You control your application and the way it will be built and deployed on via a single configuration file.

The services file allows you to define the services such as database, search engine, cache and so on; in this project demo, it will be a MySQL instance.

  type: mysql:10.2
  disk: 1024

The routes file defines the environment’s routes; there are several options, such as cache and redirects, but we won’t cover these in this post. For more information, see the documentation.

  type: upstream
  upstream: "app:http"

The final configuration file,, specifies a single application, and defines how to build and execute it. Here’s the structure:

  • name. Sets the unique name of the application container.
  • type. Sets the container base image to use, including application language. We’re using Java version 8, but there are more Java versions that supports.
  • hooks. Controls how the application gets compiled. This example uses a Maven command to compile, but there’s support for Gradle and Ant.
  • relationships. Defines connections to other services, such as the MySQL database instance.
  • web. Controls how the web application is served. As an uber-jar, the application will execute a Spring Jar file. Also, the application must use the port that’s provided from the container through a $PORT variable.
name: app
type: "java:8"

disk: 1024

  build: mvn clean install

  database: "database:mysql"
    start:  java -jar target/spring-boot-maven-mysql.jar --server.port=$PORT and Java

For the database, we need to define where the connection comes from. provides connection credentials to services using the PLATFORM_RELATIONSHIPS environmental variable.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import sh.platform.config.Config;
import sh.platform.config.MySQL;

import javax.sql.DataSource;

public class DataSourceConfig {

	@Bean(name = "dataSource")
	public DataSource getDataSource() {
    	Config config = new Config();
    	MySQL database = config.getCredential("database", MySQL::new);
    	return database.get();

The Java Config offers classes that will read the information from the variable and return an object that represents the services, like a DataSource for MySQL, PostgreSQL, MongoClient, Redis, or any other services you’ve defined in .platform/services.yaml!

The application is now ready, so it’s time to move it to the cloud with using the following steps:

  • Create a new free trial account.
  • Sign up with a new user and password, or login using a current GitHub, Bitbucket, or Google account. If you use a third-party login, you’ll be able to set a password for your account later.
  • Select the region of the world where your site should live.
  • Select the blank template.

After this wizard, will provision the whole infrastructure to you, and will offer a Git remote repository. Before access, remember to set the SSH keys. The Git-driven infrastructure means it will automatically manage everything your application needs to push it to the master remote repository. You only need to write your code—including a few YAML files that specify your desired infrastructure—then commit it to Git and push.

git remote add platform <>
git commit -m "Initial project"
git push -u platform master

Code pushed will create both the Java application, a MySQL instance, and, when it’s done, will return an IP address to the service. Let’s test the application. To test a REST application, any HTTP client is OK.

curl -X POST -H "Content-Type: application/json"  -d '{"name": "Paulo", "age": 50}' https://<service.ip>/people
curl -X POST -H "Content-Type: application/json"  -d '{"name": "Otavio", "age": 30}' https://<service.ip>/people
curl -X POST -H "Content-Type: application/json"  -d '{"name": "Poliana", "age": 25}' https://<service.ip>/people
curl https://<service.ip>/people
#result output here

You’ve now seen how a native integration between Java and works, and the result code confirms it! As a Java developer, you can bring your application to the cloud quickly with once that application uses either Maven, Gradle, and Ant as build management.

There are templates for Java and other languages in our SaaS. We’re working to have even more facilities, templates models, and services to help grow the Java and community.