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

WIP: HTTP Rest Service #199

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions devon4j-http-rest-service/files/VisitormanagementRestService.java
Original file line number Diff line number Diff line change
@@ -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();

}
Original file line number Diff line number Diff line change
@@ -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";
}

}
36 changes: 36 additions & 0 deletions devon4j-http-rest-service/files/server_application.txt
Original file line number Diff line number Diff line change
@@ -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

27 changes: 27 additions & 0 deletions devon4j-http-rest-service/files/server_config_application.txt
Original file line number Diff line number Diff line change
@@ -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
79 changes: 79 additions & 0 deletions devon4j-http-rest-service/index.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
= 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].
* 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")
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.

====
[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" })
--
====