From 5f34eb2c2810d76f8b104ef7858bdf684c6088df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel=20Torres=20Font?= Date: Tue, 15 Feb 2022 11:59:52 +0100 Subject: [PATCH 1/5] WIP: HTTP Rest Service --- .../VisitormanagementRestServiceImpl.java | 16 +++++ .../files/server_application.txt | 36 ++++++++++ .../files/server_config_application.txt | 27 ++++++++ devon4j-http-rest-service/index.asciidoc | 66 +++++++++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 devon4j-http-rest-service/files/VisitormanagementRestServiceImpl.java create mode 100644 devon4j-http-rest-service/files/server_application.txt create mode 100644 devon4j-http-rest-service/files/server_config_application.txt create mode 100644 devon4j-http-rest-service/index.asciidoc diff --git a/devon4j-http-rest-service/files/VisitormanagementRestServiceImpl.java b/devon4j-http-rest-service/files/VisitormanagementRestServiceImpl.java new file mode 100644 index 00000000..87624a28 --- /dev/null +++ b/devon4j-http-rest-service/files/VisitormanagementRestServiceImpl.java @@ -0,0 +1,16 @@ +package com.example.application.httprestserver.visitormanagement.service.impl.rest; + +import javax.inject.Named; + +import com.example.application.httprestserver.visitormanagement.service.api.rest.VisitormanagementRestService; + +@Named("VisitormanagementRestService") +public class VisitormanagementRestServiceImpl implements VisitormanagementRestService { + + @Override + public String returnResponseToClient() { + + return "Welcome to REST API world"; + } + +} diff --git a/devon4j-http-rest-service/files/server_application.txt b/devon4j-http-rest-service/files/server_application.txt new file mode 100644 index 00000000..6140b0cf --- /dev/null +++ b/devon4j-http-rest-service/files/server_application.txt @@ -0,0 +1,36 @@ +# This is the configuration file shipped with the application that contains reasonable defaults. +# Environment specific configurations are configured in config/application.properties. +# If you are running in a servlet container you may add this to lib/config/application.properties in case you do not +# want to touch the WAR file. + +server.port=8081 +server.servlet.context-path=/httprestservice +spring.application.name=httprestservice + +security.expose.error.details=false + +spring.jpa.hibernate.ddl-auto=validate + +# Datasource for accessing the database +# https://github.com/spring-projects/spring-boot/blob/d3c34ee3d1bfd3db4a98678c524e145ef9bca51c/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DatabaseDriver.java +spring.jpa.database=h2 +# spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +# spring.datasource.driver-class-name=org.h2.Driver +spring.datasource.username=sa + +# Hibernate NamingStrategy has been deprecated and then removed in favor of two step naming strategy ImplicitNamingStrategy and PhysicalNamingStrategy +spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl +spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl + +# https://github.com/devonfw/devon4j/issues/65 +# https://vladmihalcea.com/the-open-session-in-view-anti-pattern/ +spring.jpa.open-in-view=false + +# to prevent that Spring Boot launches batch jobs on startup +# might otherwise lead to errors if job parameters are needed (or lead to unwanted modifications and longer startup times) +# see http://stackoverflow.com/questions/22318907/how-to-stop-spring-batch-scheduled-jobs-from-running-at-first-time-when-executin +spring.batch.job.enabled=false + +# Flyway for Database Setup and Migrations +spring.flyway.locations=classpath:db/migration + diff --git a/devon4j-http-rest-service/files/server_config_application.txt b/devon4j-http-rest-service/files/server_config_application.txt new file mode 100644 index 00000000..329ad8b8 --- /dev/null +++ b/devon4j-http-rest-service/files/server_config_application.txt @@ -0,0 +1,27 @@ +# This is the spring boot configuration file for development. It will not be included into the application. +# In order to set specific configurations in a regular installed environment create an according file +# config/application.properties in the server. If you are deploying the application to a servlet container as untouched +# WAR file you can locate this config folder in ${symbol_dollar}{CATALINA_BASE}/lib. If you want to deploy multiple applications to +# the same container (not recommended by default) you need to ensure the WARs are extracted in webapps folder and locate +# the config folder inside the WEB-INF/classes folder of the webapplication. + +server.port=8081 +server.servlet.context-path=/httprestservice + +# Datasource for accessing the database +# See https://github.com/devonfw/devon4j/blob/develop/documentation/guide-configuration.asciidoc#security-configuration +#jasypt.encryptor.password=none +#spring.datasource.password=ENC(7CnHiadYc0Wh2FnWADNjJg==) +spring.datasource.password= +spring.datasource.url=jdbc:h2:./.httprestservice; + +# print SQL to console for debugging (e.g. detect N+1 issues) +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true + +# Enable JSON pretty printing +spring.jackson.serialization.INDENT_OUTPUT=true + +# Flyway for Database Setup and Migrations +spring.flyway.enabled=true +spring.flyway.clean-on-validation-error=true diff --git a/devon4j-http-rest-service/index.asciidoc b/devon4j-http-rest-service/index.asciidoc new file mode 100644 index 00000000..6ee5e631 --- /dev/null +++ b/devon4j-http-rest-service/index.asciidoc @@ -0,0 +1,66 @@ += Develop a Devon4j HTTP REST Service +==== +## Prerequisites +* User should have development experience in Java programming. +* Basic knowledge of link:https://www.redhat.com/en/topics/api/what-is-a-rest-api[REST]. +* Get the link:https://github.com/devonfw/ide/blob/master/documentation/setup.asciidoc[devonfw-ide] + +## Learning Goal + +In this tutorial, you will learn how to implement a simple REST service. + +REST (REpresentational State Transfer) is an inter-operable protocol for services that is more lightweight than SOAP. + +First of all, we should get the devonfw-ide loaded. + +==== +[step] +-- +restoreDevonfwIde(["java", "vscode"]) +-- +==== + +## Create the devon4j REST Service + +In this step, we will see how to implement a REST service in a devon4j project. + +First of all, we must create a devon4j project to work with. + +==== +[step] +-- +createDevon4jProject("com.example.application.httprestservice") +-- +==== + +Once the project is created, we will start building the application. + +In the files folder, there are some Java files that will be needed in the application development. + +We need to copy them into the project as follows: + +==== +[step] +-- +createFile("httprestservice/core/src/main/java/com/example/application/httprestservice/visitormanagement/service/impl/rest/VisitormanagementRestServiceImpl.java", "files/VisitormanagementRestServiceImpl.java") +-- +==== + +Here , you can see "VisitormanagementRestServiceImpl.java" is annotated with @Named to make it a spring-bean. To get return response to client "returnResponseToClient()" can be accessed via HTTP GET under the URL path "/visitormanagement/v1/clientrequest". It will return its result (String) as JSON (see @Produces in VisitormanagementRestService). + +Then, we can launch the application and do GET requests. + +==== +[step] +-- +changeFile("httprestservice/core/src/main/resources/application.properties", { "file": "files/server_application.txt" }) changeFile("httprestservice/core/src/main/resources/config/application.properties", { "file": "files/server_config_application.txt" }) +buildJava("httprestservice", false) +-- +==== + +==== +[step] +-- +runServerJava("httprestservice/server", { "startupTime": 1000, "port": 8081, "path": "httprestservice" }) +-- +==== \ No newline at end of file From 94e5aaaa6160ba176d0eee36bad3e92023b3a681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel=20Torres=20Font?= Date: Tue, 15 Feb 2022 12:06:35 +0100 Subject: [PATCH 2/5] WIP: HTTP Rest Service. Tags added --- devon4j-http-rest-service/index.asciidoc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/devon4j-http-rest-service/index.asciidoc b/devon4j-http-rest-service/index.asciidoc index 6ee5e631..aadd1ff0 100644 --- a/devon4j-http-rest-service/index.asciidoc +++ b/devon4j-http-rest-service/index.asciidoc @@ -1,5 +1,18 @@ = Develop a Devon4j HTTP REST Service ==== +[tags] + +-- + +technology=java;http;spring data;jpa;flyway;cxf;jax-rs + +difficulty=intermediate + +topic=rest;rest api;authentication;api;client + +asset=devon4j;mythaystar + +-- ## Prerequisites * User should have development experience in Java programming. * Basic knowledge of link:https://www.redhat.com/en/topics/api/what-is-a-rest-api[REST]. From e952925f1f9f4fbddcfdf923f10d4f7727d2f502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel=20Torres=20Font?= Date: Wed, 16 Feb 2022 15:37:33 +0100 Subject: [PATCH 3/5] Fixed file. Added service --- .../files/VisitormanagementRestService.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 devon4j-http-rest-service/files/VisitormanagementRestService.java diff --git a/devon4j-http-rest-service/files/VisitormanagementRestService.java b/devon4j-http-rest-service/files/VisitormanagementRestService.java new file mode 100644 index 00000000..da71d2c9 --- /dev/null +++ b/devon4j-http-rest-service/files/VisitormanagementRestService.java @@ -0,0 +1,20 @@ +package com.example.application.httprestserver.visitormanagement.service.api.rest; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Path("/visitormanagement/v1") +@Consumes(MediaType.APPLICATION_JSON) +@Produces(MediaType.APPLICATION_JSON) +public interface VisitormanagementRestService { + + @GET + @Path("/clientrequest/") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public String returnResponseToClient(); + +} \ No newline at end of file From 02b24f293bda1efb7ab2f85521fb52b76d7f17a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel=20Torres=20Font?= Date: Wed, 16 Feb 2022 16:24:12 +0100 Subject: [PATCH 4/5] Fixed missing file --- devon4j-http-rest-service/index.asciidoc | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/devon4j-http-rest-service/index.asciidoc b/devon4j-http-rest-service/index.asciidoc index aadd1ff0..f294bd19 100644 --- a/devon4j-http-rest-service/index.asciidoc +++ b/devon4j-http-rest-service/index.asciidoc @@ -59,6 +59,13 @@ createFile("httprestservice/core/src/main/java/com/example/application/httprests -- ==== +==== +[step] +-- +createFile("httprestservice/core/src/main/java/com/example/application/httprestservice/visitormanagement/service/impl/rest/VisitormanagementRestService.java", "files/VisitormanagementRestService.java") +-- +==== + Here , you can see "VisitormanagementRestServiceImpl.java" is annotated with @Named to make it a spring-bean. To get return response to client "returnResponseToClient()" can be accessed via HTTP GET under the URL path "/visitormanagement/v1/clientrequest". It will return its result (String) as JSON (see @Produces in VisitormanagementRestService). Then, we can launch the application and do GET requests. From 0e4337b11c4b0c5c60a63cbcd7984d88990f9756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel=20Torres=20Font?= Date: Wed, 16 Feb 2022 16:27:39 +0100 Subject: [PATCH 5/5] more fixes in file ascii --- devon4j-http-rest-service/index.asciidoc | 7 ------- 1 file changed, 7 deletions(-) diff --git a/devon4j-http-rest-service/index.asciidoc b/devon4j-http-rest-service/index.asciidoc index f294bd19..80e2cea3 100644 --- a/devon4j-http-rest-service/index.asciidoc +++ b/devon4j-http-rest-service/index.asciidoc @@ -56,16 +56,9 @@ We need to copy them into the project as follows: [step] -- createFile("httprestservice/core/src/main/java/com/example/application/httprestservice/visitormanagement/service/impl/rest/VisitormanagementRestServiceImpl.java", "files/VisitormanagementRestServiceImpl.java") --- -==== - -==== -[step] --- createFile("httprestservice/core/src/main/java/com/example/application/httprestservice/visitormanagement/service/impl/rest/VisitormanagementRestService.java", "files/VisitormanagementRestService.java") -- ==== - Here , you can see "VisitormanagementRestServiceImpl.java" is annotated with @Named to make it a spring-bean. To get return response to client "returnResponseToClient()" can be accessed via HTTP GET under the URL path "/visitormanagement/v1/clientrequest". It will return its result (String) as JSON (see @Produces in VisitormanagementRestService). Then, we can launch the application and do GET requests.