-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from dearrudam/jnosql-bean-validation
Adding a JNoSQL with Bean Validation sample code
- Loading branch information
Showing
18 changed files
with
547 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# MicroProfile generated Application | ||
|
||
## Introduction | ||
|
||
MicroProfile Starter has generated this MicroProfile application for you. | ||
|
||
The generation of the executable jar file can be performed by issuing the following command | ||
|
||
|
||
mvn clean package | ||
|
||
This will create an executable jar file **jnosql-bean-validation.jar** within the _target_ maven folder. This can be started by executing the following command | ||
|
||
java -jar target/jnosql-bean-validation.jar | ||
|
||
|
||
|
||
### Liberty Dev Mode | ||
|
||
During development, you can use Liberty's development mode (dev mode) to code while observing and testing your changes on the fly. | ||
With the dev mode, you can code along and watch the change reflected in the running server right away; | ||
unit and integration tests are run on pressing Enter in the command terminal; you can attach a debugger to the running server at any time to step through your code. | ||
|
||
|
||
mvn liberty:dev | ||
|
||
|
||
|
||
|
||
|
||
To launch the test page, open your browser at the following URL | ||
|
||
http://localhost:9080/index.html | ||
|
||
|
||
|
||
## Specification examples | ||
|
||
By default, there is always the creation of a JAX-RS application class to define the path on which the JAX-RS endpoints are available. | ||
|
||
Also, a simple Hello world endpoint is created, have a look at the class **HelloController**. | ||
|
||
More information on MicroProfile can be found [here](https://microprofile.io/) | ||
|
||
|
||
### Config | ||
|
||
Configuration of your application parameters. Specification [here](https://microprofile.io/project/eclipse/microprofile-config) | ||
|
||
The example class **ConfigTestController** shows you how to inject a configuration parameter and how you can retrieve it programmatically. | ||
|
||
### Open API | ||
|
||
Exposes the information about your endpoints in the format of the OpenAPI v3 specification. Specification [here](https://microprofile.io/project/eclipse/microprofile-open-api) | ||
|
||
The index page contains a link to the OpenAPI information of your endpoints. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
services: | ||
|
||
mongo: | ||
image: mongo | ||
restart: always | ||
environment: | ||
MONGO_INITDB_ROOT_USERNAME: root | ||
MONGO_INITDB_ROOT_PASSWORD: example | ||
ports: | ||
- 27017:27017 | ||
|
||
mongo-express: | ||
image: mongo-express | ||
restart: always | ||
ports: | ||
- 8081:8081 | ||
environment: | ||
ME_CONFIG_MONGODB_ADMINUSERNAME: root | ||
ME_CONFIG_MONGODB_ADMINPASSWORD: example | ||
ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>org.eclipse.jnosql.demoee</groupId> | ||
<artifactId>jnosql-bean-validation</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>war</packaging> | ||
<properties> | ||
<openliberty.maven.version>3.5.1</openliberty.maven.version> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
<failOnMissingWebXml>false</failOnMissingWebXml> | ||
<jaeger.client.version>1.5.0</jaeger.client.version> | ||
<slf4j.jdk.version>1.7.30</slf4j.jdk.version> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<final.name>jnosql-bean-validation</final.name> | ||
<slf4j.api.version>1.7.30</slf4j.api.version> | ||
<jnosql.version>1.0.0</jnosql.version> | ||
<jakarta.validation-api.version>3.0.2</jakarta.validation-api.version> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.eclipse.microprofile</groupId> | ||
<artifactId>microprofile</artifactId> | ||
<version>5.0</version> | ||
<type>pom</type> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.jnosql.databases</groupId> | ||
<artifactId>jnosql-mongodb</artifactId> | ||
<version>${jnosql.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.jnosql.mapping</groupId> | ||
<artifactId>jnosql-mapping-validation</artifactId> | ||
<version>${jnosql.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>jakarta.validation</groupId> | ||
<artifactId>jakarta.validation-api</artifactId> | ||
<version>${jakarta.validation-api.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<finalName>jnosql-bean-validation</finalName> | ||
</build> | ||
<profiles> | ||
<profile> | ||
<id>liberty</id> | ||
<activation> | ||
<activeByDefault>true</activeByDefault> | ||
</activation> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-war-plugin</artifactId> | ||
<version>3.3.2</version> | ||
<configuration> | ||
<failOnMissingWebXml>false</failOnMissingWebXml> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>io.openliberty.tools</groupId> | ||
<artifactId>liberty-maven-plugin</artifactId> | ||
<version>${openliberty.maven.version}</version> | ||
<executions> | ||
<execution> | ||
<id>package-server</id> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>create</goal> | ||
<goal>install-feature</goal> | ||
<goal>deploy</goal> | ||
<goal>package</goal> | ||
</goals> | ||
<configuration> | ||
<outputDirectory>target/wlp-package</outputDirectory> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<include>runnable</include> | ||
<serverName>${final.name}</serverName> | ||
<bootstrapProperties> | ||
<project.name>${final.name}</project.name> | ||
<jwt.issuer>https://server.example.com</jwt.issuer> | ||
<app.context.root>/</app.context.root> | ||
</bootstrapProperties> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<dependencies> | ||
<dependency> | ||
<groupId>io.jaegertracing</groupId> | ||
<artifactId>jaeger-client</artifactId> | ||
<version>${jaeger.client.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>${slf4j.api.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-jdk14</artifactId> | ||
<version>${slf4j.jdk.version}</version> | ||
</dependency> | ||
</dependencies> | ||
</profile> | ||
</profiles> | ||
</project> |
11 changes: 11 additions & 0 deletions
11
...org/eclipse/jnosql/demoee/jnosql/bean/validation/JNoSQLBeanValidationRestApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.eclipse.jnosql.demoee.jnosql.bean.validation; | ||
|
||
import jakarta.ws.rs.ApplicationPath; | ||
import jakarta.ws.rs.core.Application; | ||
|
||
/** | ||
* | ||
*/ | ||
@ApplicationPath("/api") | ||
public class JNoSQLBeanValidationRestApplication extends Application { | ||
} |
47 changes: 47 additions & 0 deletions
47
...validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/NewStudent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.eclipse.jnosql.demoee.jnosql.bean.validation; | ||
|
||
import jakarta.validation.constraints.Min; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Positive; | ||
import org.eclipse.microprofile.openapi.annotations.media.Schema; | ||
|
||
import java.util.UUID; | ||
@Schema(name = "NewStudent") | ||
public class NewStudent { | ||
|
||
@NotBlank | ||
@Schema(description = "Student name") | ||
private String name; | ||
|
||
@Positive | ||
@Min(18) | ||
@Schema(description = "Student age") | ||
private int age; | ||
|
||
public NewStudent() { | ||
} | ||
|
||
Student toModel() { | ||
Student student = new Student(); | ||
student.setId(UUID.randomUUID().toString()); | ||
student.setName(this.getName()); | ||
student.setAge(this.getAge()); | ||
return student; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public void setAge(int age) { | ||
this.age = age; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...an-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/Student.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.eclipse.jnosql.demoee.jnosql.bean.validation; | ||
|
||
import jakarta.nosql.Column; | ||
import jakarta.nosql.Entity; | ||
import jakarta.nosql.Id; | ||
import jakarta.validation.constraints.Min; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Positive; | ||
import org.eclipse.microprofile.openapi.annotations.media.Schema; | ||
|
||
@Schema(name = "Student") | ||
@Entity | ||
public class Student { | ||
|
||
@Id | ||
@Schema(description = "Student id") | ||
private String id; | ||
|
||
@Column | ||
@Schema(description = "Student name") | ||
@NotBlank | ||
private String name; | ||
|
||
@Positive | ||
@Min(18) | ||
@Column | ||
@Schema(description = "Student age") | ||
private int age; | ||
|
||
public Student() { | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void setAge(int age) { | ||
this.age = age; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...tion/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/StudentsResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.eclipse.jnosql.demoee.jnosql.bean.validation; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import jakarta.nosql.Template; | ||
import jakarta.validation.Valid; | ||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import org.eclipse.microprofile.openapi.annotations.OpenAPIDefinition; | ||
import org.eclipse.microprofile.openapi.annotations.info.Info; | ||
import org.eclipse.microprofile.openapi.annotations.media.Content; | ||
import org.eclipse.microprofile.openapi.annotations.media.Schema; | ||
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; | ||
import org.eclipse.microprofile.openapi.annotations.responses.APIResponses; | ||
|
||
|
||
@ApplicationScoped | ||
@OpenAPIDefinition(info = @Info(title = "Students endpoint", version = "1.0")) | ||
|
||
@Path("students") | ||
@Consumes({MediaType.APPLICATION_JSON}) | ||
@Produces({MediaType.APPLICATION_JSON}) | ||
public class StudentsResource { | ||
|
||
|
||
@Inject | ||
Template template; | ||
|
||
|
||
@APIResponses(value = { | ||
@APIResponse( | ||
responseCode = "201", | ||
description = "Student", | ||
content = @Content( | ||
mediaType = MediaType.APPLICATION_JSON, | ||
schema = @Schema( | ||
ref = "NewStudent")) | ||
)}) | ||
@POST | ||
public Student add(@Valid NewStudent newStudent){ | ||
|
||
var student = newStudent.toModel(); | ||
|
||
return template.insert(student); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.