Skip to content

Commit 509a2b2

Browse files
author
Tomáš Kraus
committed
Helidon Data SE Declarative example
Signed-off-by: Tomáš Kraus <[email protected]>
1 parent ce36aba commit 509a2b2

File tree

26 files changed

+1554
-15
lines changed

26 files changed

+1554
-15
lines changed

examples/data/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
<modules>
3838
<module>basics</module>
39+
<module>se-declarative</module>
3940
<module>se-imperative</module>
4041
</modules>
4142
</project>
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
Helidon Data SE Declarative Example
2+
----
3+
4+
This example demonstrates a Java SE declarative application that utilizes Helidon Data, WebServer,
5+
and a MySQL database.
6+
7+
> **NOTE:** Database tables are initialized with ID auto increment to supply primary key values
8+
> by the database. MySQL database default String comparisons are case-insensitive.
9+
10+
## Start the Database
11+
12+
To run the application, a MySQL database is required. You can start the database with the necessary
13+
configuration using the following Docker command:
14+
15+
```shell
16+
docker run --name mysql \
17+
-p 3306:3306 \
18+
-e MYSQL_DATABASE='pets' \
19+
-e MYSQL_RANDOM_ROOT_PASSWORD='yes' \
20+
-e MYSQL_USER='user' \
21+
-e MYSQL_PASSWORD='P4ssw0rd' \
22+
-d mysql
23+
```
24+
25+
### Database Schema and Content
26+
27+
The application's Jakarta Persistence API implementation automatically creates the database schema
28+
using the `resources/init.sql` script. The schema consists of three main entities: `Pet`, `Owner`
29+
and `Breed`. The initialization script populates the database with a basic set of records.
30+
31+
## Build and Run
32+
33+
1. Build the application using Maven:
34+
35+
```shell
36+
mvn package
37+
```
38+
39+
2. Run the application:
40+
41+
```shell
42+
java -jar target/helidon-examples-data-se-declarative.jar
43+
```
44+
45+
> **NOTE:** The default username and password from this example should never be used in a production environment!
46+
47+
## Test Example
48+
49+
The application provides the following endpoints:
50+
- http://localhost:8080/pet - Pet entity endpoint
51+
- http://localhost:8080/owner - Owner entity endpoint
52+
- http://localhost:8080/breed - Breed entity endpoint
53+
54+
### Pet Endpoint
55+
56+
**Retrieve Pet entity endpoint information:**
57+
```shell
58+
curl http://localhost:8080/pet
59+
```
60+
61+
**List all pets:**
62+
```shell
63+
curl http://localhost:8080/pet/all
64+
```
65+
66+
**List all dogs:**
67+
```shell
68+
curl http://localhost:8080/pet/breed/Dog
69+
```
70+
71+
**Retrieve a Pet by name (`Max`):**
72+
```shell
73+
curl http://localhost:8080/pet/get/Max
74+
```
75+
76+
**Insert new pet:**
77+
```shell
78+
curl -i -X POST -H 'Content-type: application/json' -d '{"name":"Ken","weight":3.5,"birth":"2023-05-10","owner":"Betty","breed":"Dog"}' http://localhost:8080/pet
79+
```
80+
81+
**Delete existing pet by ID (`20`):**
82+
```shell
83+
curl -i -X DELETE http://localhost:8080/pet/20
84+
```
85+
86+
### Owner Endpoint
87+
88+
**Retrieve Owner entity endpoint information:**
89+
```shell
90+
curl http://localhost:8080/owner
91+
```
92+
93+
**List all owners:**
94+
```shell
95+
curl http://localhost:8080/owner/all
96+
```
97+
98+
**List all names of owners who own a cat:**
99+
```shell
100+
curl http://localhost:8080/owner/names/Cat
101+
```
102+
103+
**Insert new owner:**
104+
```shell
105+
curl -i -X POST http://localhost:8080/owner/Alice
106+
```
107+
108+
**Delete existing owner by ID (`10`):**
109+
```shell
110+
curl -i -X DELETE http://localhost:8080/owner/10
111+
```
112+
113+
### Breed Endpoint
114+
115+
**Retrieve Breed entity endpoint information:**
116+
```shell
117+
curl http://localhost:8080/breed
118+
```
119+
120+
**List all breeds:**
121+
```shell
122+
curl http://localhost:8080/breed/all
123+
```
124+
125+
**Insert new breed:**
126+
```shell
127+
curl -i -X POST http://localhost:8080/breed/Hamster
128+
```
129+
130+
**Delete existing breed by ID (`10`):**
131+
```shell
132+
curl -i -X DELETE http://localhost:8080/breed/10
133+
```
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (c) 2025 Oracle and/or its affiliates.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
<parent>
24+
<groupId>io.helidon.applications</groupId>
25+
<artifactId>helidon-se</artifactId>
26+
<version>4.3.0-SNAPSHOT</version>
27+
<relativePath/>
28+
</parent>
29+
30+
<groupId>io.helidon.examples.data</groupId>
31+
<artifactId>helidon-examples-data-se-declarative</artifactId>
32+
<version>1.0.0-SNAPSHOT</version>
33+
<name>Helidon Examples Data Repository SE Declarative</name>
34+
35+
<description>
36+
The simplest example shows how to use Data Repository.
37+
</description>
38+
39+
<properties>
40+
<mainClass>io.helidon.examples.data.se.declarative.Main</mainClass>
41+
</properties>
42+
43+
<dependencies>
44+
<dependency>
45+
<groupId>io.helidon.config</groupId>
46+
<artifactId>helidon-config-yaml</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>io.helidon.data</groupId>
50+
<artifactId>helidon-data</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>io.helidon.data.jakarta.persistence</groupId>
54+
<artifactId>helidon-data-jakarta-persistence</artifactId>
55+
</dependency>
56+
<dependency>
57+
<groupId>io.helidon.webserver</groupId>
58+
<artifactId>helidon-webserver</artifactId>
59+
</dependency>
60+
<dependency>
61+
<groupId>io.helidon.http.media</groupId>
62+
<artifactId>helidon-http-media-jsonb</artifactId>
63+
<scope>runtime</scope>
64+
</dependency>
65+
<dependency>
66+
<groupId>io.helidon.logging</groupId>
67+
<artifactId>helidon-logging-jul</artifactId>
68+
<scope>runtime</scope>
69+
</dependency>
70+
<dependency>
71+
<groupId>jakarta.persistence</groupId>
72+
<artifactId>jakarta.persistence-api</artifactId>
73+
</dependency>
74+
<dependency>
75+
<groupId>org.eclipse.persistence</groupId>
76+
<artifactId>org.eclipse.persistence.jpa</artifactId>
77+
<scope>runtime</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.eclipse.persistence</groupId>
81+
<artifactId>org.eclipse.persistence.core</artifactId>
82+
<scope>runtime</scope>
83+
</dependency>
84+
<dependency>
85+
<groupId>com.mysql</groupId>
86+
<artifactId>mysql-connector-j</artifactId>
87+
<scope>runtime</scope>
88+
</dependency>
89+
</dependencies>
90+
91+
<build>
92+
<plugins>
93+
<plugin>
94+
<groupId>org.apache.maven.plugins</groupId>
95+
<artifactId>maven-compiler-plugin</artifactId>
96+
<configuration>
97+
<annotationProcessorPaths>
98+
<path>
99+
<groupId>io.helidon.bundles</groupId>
100+
<artifactId>helidon-bundles-apt</artifactId>
101+
<version>${helidon.version}</version>
102+
</path>
103+
<path>
104+
<groupId>io.helidon.data.jakarta.persistence</groupId>
105+
<artifactId>helidon-data-jakarta-persistence-codegen</artifactId>
106+
<version>${helidon.version}</version>
107+
</path>
108+
</annotationProcessorPaths>
109+
</configuration>
110+
</plugin>
111+
<plugin>
112+
<groupId>org.apache.maven.plugins</groupId>
113+
<artifactId>maven-dependency-plugin</artifactId>
114+
<executions>
115+
<execution>
116+
<id>copy-libs</id>
117+
</execution>
118+
</executions>
119+
</plugin>
120+
<plugin>
121+
<groupId>io.helidon.service</groupId>
122+
<artifactId>helidon-service-maven-plugin</artifactId>
123+
<version>${helidon.version}</version>
124+
<executions>
125+
<execution>
126+
<id>create-application</id>
127+
<goals>
128+
<goal>create-application</goal>
129+
</goals>
130+
</execution>
131+
</executions>
132+
</plugin>
133+
</plugins>
134+
</build>
135+
136+
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.helidon.examples.data.se.declarative;
17+
18+
import io.helidon.logging.common.LogConfig;
19+
import io.helidon.service.registry.Service;
20+
import io.helidon.service.registry.ServiceRegistryManager;
21+
import io.helidon.service.registry.Services;
22+
import io.helidon.webserver.WebServer;
23+
24+
/**
25+
* The Main class serves as the entry point for the application.
26+
* It demonstrates the usage of Helidon Data in an SE imperative application.
27+
*/
28+
@Service.GenerateBinding
29+
public class Main {
30+
31+
static {
32+
LogConfig.initClass();
33+
}
34+
35+
private Main() {
36+
throw new UnsupportedOperationException("Instances of Main class are not allowed");
37+
}
38+
39+
/**
40+
* Entry point for the SE application.
41+
*
42+
* @param args command-line arguments passed to the application
43+
*/
44+
public static void main(String... args) {
45+
46+
// load logging configuration
47+
LogConfig.configureRuntime();
48+
49+
ServiceRegistryManager.start(ApplicationBinding.create());
50+
51+
WebServer webServer = Services.get(WebServer.class);
52+
System.out.println("Server started on: http://localhost:" + webServer.port());
53+
54+
}
55+
56+
}

0 commit comments

Comments
 (0)