In this blog series, we will develop a Spring Boot application and deploy it on SAP Cloud Platform (Cloud Foundry). For persistence, we will make use of SAP HANA service on SCP. The goal of this project is to show how we can make use of HANA database in Cloud foundry. The application itself is very simple - REST endpoints supporting CRUD operations on Employee entity.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>spring-hana-cloud-foundry</groupId>
<artifactId>spring-hana-cloud-foundry</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-hana-cloud-foundry</name>
<description>Sample Spring Boot application to use SAP HANA Service</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-cloudfoundry-connector</artifactId>
<version>1.2.2.RELEASE</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.sap.hana.cloud</groupId>
<artifactId>spring-cloud-cloudfoundry-hana-service-connector</artifactId>
<version>1.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-spring-service-connector</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.sap.db.jdbc</groupId>
<artifactId>ngdbc</artifactId>
<version>2.3.55</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<activatedProperties>local</activatedProperties>
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>cf</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<activatedProperties>cf</activatedProperties>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.sap.springhanacloudfoundry.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.Getter;
import lombok.AllArgsConstructor;
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
public class Employee {
@Id
@Column(name ="id", unique=true)
private long id;
@Column(name ="firstName")
private String firstName;
@Column(name ="lastName")
private String lastName;
@Column(name ="email", unique=true)
private String email;
@Column(name ="contact",unique=true)
private String contact;
}
package com.sap.springhanacloudfoundry.repository;
import org.springframework.data.repository.CrudRepository;
import com.sap.springhanacloudfoundry.models.Employee;
public interface EmployeeRepository extends CrudRepository<Employee, Long>{
}
package com.sap.springhanacloudfoundry.services;
import java.util.ArrayList;
import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sap.springhanacloudfoundry.models.Employee;
import com.sap.springhanacloudfoundry.repository.EmployeeRepository;
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
public long getCount() {
long count = employeeRepository.count();
return count;
}
public List<Employee> findAllEmployee(){
List<Employee> employee = new ArrayList<>();
employeeRepository.findAll().forEach(employee::add);
return employee;
}
public boolean insertEmployee(Employee employee) {
try {
employeeRepository.save(employee);
return true;
}
catch (Exception e) {
return false;
}
}
public Employee findEmployeeById(Long id) {
Employee employee = employeeRepository.findById(id).orElse(null);
return employee;
}
public boolean deleteEmployee(long id) {
Employee employee = employeeRepository.findById(id).orElse(null);
if(employee!=null) {
employeeRepository.delete(employee);
return true;
}
return false;
}
}
package com.sap.springhanacloudfoundry.controller;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.sap.springhanacloudfoundry.models.Employee;
import com.sap.springhanacloudfoundry.services.EmployeeService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EmployeeController {
Logger log = LoggerFactory.getLogger(getClass());
@Autowired
private EmployeeService employeeService;
@RequestMapping("/employee/count")
public long count() {
log.info("Search total number of employees");
return employeeService.getCount();
}
@RequestMapping("/employee/all")
public List<Employee> getAllEmployees(){
log.info("Searching all employees");
return employeeService.findAllEmployee();
}
@RequestMapping(method=RequestMethod.POST, value = "/employee/add")
public boolean addEmployee(@RequestBody Employee employee) {
log.info("Creation/Updating Employee - "+employee.toString());
return employeeService.insertEmployee(employee);
}
@RequestMapping("/employee/id/{id}" )
public Employee findById(@PathVariable long id) {
log.info("Searching employee with ID - "+ id);
return employeeService.findEmployeeById(id);
}
@RequestMapping(method=RequestMethod.DELETE, value="/employee/delete/{id}")
public boolean deleteEmployee(@PathVariable long id) {
return employeeService.deleteEmployee(id);
}
}
package com.sap.springhanacloudfoundry.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.cloud.config.java.AbstractCloudConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
@Profile("cloud")
public class CloudDatabaseConfig extends AbstractCloudConfig {
@Bean
public DataSource dataSource(@Value("${hana.url}")final String url,
@Value("${hana.user}")final String user,
@Value("${hana.password}")final String password) {
return DataSourceBuilder.create()
.type(HikariDataSource.class)
.driverClassName(com.sap.db.jdbc.Driver.class.getName())
.url(url)
.username(user)
.password(password)
.build();
}
}
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.HANAColumnStoreDialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
hana.url = ${vcap.services.hana_migration.credentials.url}
hana.user = ${vcap.services.hana_migration.credentials.user}
hana.password = ${vcap.services.hana_migration.credentials.password}
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.h2.console.enabled=true
spring.profiles.active=@activatedProperties@
{
"id":"1",
"firstName":"Boudhayan",
"lastName":"Dev",
"email":"email@example.com",
"contact":"12121212"
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
17 | |
11 | |
7 | |
7 | |
7 | |
7 | |
7 | |
6 | |
6 | |
6 |