Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a JNoSQL with Bean Validation sample code #9

Merged
merged 3 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ The primary goal of this repository is exploring the Jakarta NoSQL on several da
|Full Profile - Wildfly
|MongoDB

|jnosql-bean-validation
|MicroProfile - OpenLiberty
|MongoDB

|===
57 changes: 57 additions & 0 deletions jnosql-bean-validation/README.md
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.

20 changes: 20 additions & 0 deletions jnosql-bean-validation/docker-compose.yaml
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/
114 changes: 114 additions & 0 deletions jnosql-bean-validation/pom.xml
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>
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 {
}
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;
}
}
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;
}
}
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);
}


}
Loading