vimeo
https://vimeo.com/39796236
source code https://github.com/DoctusHartwald/ticket-monster
sample pom.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- Copyright 2016-2017 Red Hat, Inc, and individual contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <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> <groupId>com.example</groupId> <artifactId>fruits</artifactId> <version>15-SNAPSHOT</version> <name>Simple Fruits Application</name> <description>Spring Boot - CRUD Booster</description> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.min.version>3.3.9</maven.min.version> <postgresql.version>9.4.1212</postgresql.version> <openjdk18-openshift.version>1.3</openjdk18-openshift.version> <spring-boot.version>2.1.3.RELEASE</spring-boot.version> <spring-boot-bom.version>2.1.3.Final-redhat-00001</spring-boot-bom.version> <maven-surefire-plugin.version>2.20</maven-surefire-plugin.version> <fabric8-maven-plugin.version>3.5.40</fabric8-maven-plugin.version> <fabric8.openshift.trimImageInContainerSpec>true</fabric8.openshift.trimImageInContainerSpec> <fabric8.skip.build.pom>true</fabric8.skip.build.pom> <fabric8.generator.from> registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift:${openjdk18-openshift.version} </fabric8.generator.from> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>me.snowdrop</groupId> <artifactId>spring-boot-bom</artifactId> <version>${spring-boot-bom.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- TODO: ADD Actuator dependency here --> <!-- Testing --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
<!--/actuator/health
management.endpoints.web.exposure.include=*/actuator/metrics/acutuator/metrics/[metric-name]/actuator/beansJaeger and Tracing--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency><dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>${postgresql.version}</version> <scope>runtime</scope> </dependency></dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <configuration> <profiles> <profile>local</profile> </profiles> <classifier>exec</classifier> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <profiles> <profile> <id>local</id> <activation> <activeByDefault>true</activeByDefault> </activation> <dependencies> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> </dependencies> </profile> <profile> <id>openshift</id> <dependencies> <!-- TODO: ADD PostgreSQL database dependency here --> </dependencies> <build> <plugins> <plugin> <groupId>io.fabric8</groupId> <artifactId>fabric8-maven-plugin</artifactId> <version>${fabric8-maven-plugin.version}</version> <executions> <execution> <id>fmp</id> <goals> <goal>resource</goal> <goal>build</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
test class sample
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class ApplicationTest { @Autowired private FruitRepository fruitRepository; @Before public void beforeTest() { } @Test public void testGetAll() { assertTrue(fruitRepository.findAll().spliterator().getExactSizeIfKnown()==3); } @Test public void getOne() { assertTrue(fruitRepository.findById(1).orElse(null)!=null); }
Spring REst services
package com.example.service; import java.util.List; import java.util.Objects; import java.util.Spliterator; import java.util.stream.Collectors; import java.util.stream.StreamSupport; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; 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.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; @Controller @RequestMapping(value = "/api/fruits") public class FruitController { private final FruitRepository repository; @Autowired public FruitController(FruitRepository repository) { this.repository = repository; } @ResponseBody @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) public List getAll() { return StreamSupport .stream(repository.findAll().spliterator(), false) .collect(Collectors.toList()); }@ResponseBody @ResponseStatus(HttpStatus.CREATED) @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public Fruit post(@RequestBody(required = false) Fruit fruit) { verifyCorrectPayload(fruit); return repository.save(fruit); } @ResponseBody @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) public Fruit get(@PathVariable("id") Integer id) { verifyFruitExists(id); return repository.findById(id).orElse(null); }}
exemple fabric 8
apiVersion: v1 kind: Deployment metadata: name: ${project.artifactId} spec: template: spec: containers: - env: - name: DB_USERNAME valueFrom: secretKeyRef: name: my-database-secret key: user - name: DB_PASSWORD valueFrom: secretKeyRef: name: my-database-secret key: password - name: JAVA_OPTIONS value: "-Dspring.profiles.active=openshift"
Properties openShift
spring.datasource.url=jdbc:postgresql://${MY_DATABASE_SERVICE_HOST}:${MY_DATABASE_SERVICE_PORT}/my_data spring.datasource.username=${DB_USERNAME} spring.datasource.password=${DB_PASSWORD} spring.datasource.driver-class-name=org.postgresql.Driver spring.jpa.hibernate.ddl-auto=create