diff --git a/open-liberty/pom.xml b/open-liberty/pom.xml
index b2ed213af6..157029330b 100644
--- a/open-liberty/pom.xml
+++ b/open-liberty/pom.xml
@@ -9,78 +9,55 @@
1.0-SNAPSHOT
war
-
- 1.8
- 1.8
- UTF-8
- UTF-8
- false
-
- 3.1
- 3.2.3
- 2.22.2
- 2.22.2
-
- 9080
- 9443
- openliberty
-
-
-
- org.apache.derby
- derby
- 10.14.2.0
-
-
- com.h2database
- h2
- 1.4.186
-
-
jakarta.platform
jakarta.jakartaee-web-api
- 8.0.0
+ ${version.jakarta.jakartaee-web-api}
provided
org.eclipse.microprofile
microprofile
- 3.0
+ ${version.microprofile}
pom
provided
+
+ org.apache.derby
+ derby
+ ${version.derby}
+
+
- org.junit.jupiter
- junit-jupiter-engine
- 5.5.2
+ junit
+ junit
+ ${version.junit}
+ test
+
+
+ org.eclipse
+ yasson
+ ${version.yasson}
test
org.apache.cxf
cxf-rt-rs-client
- 3.3.4
- test
-
-
- org.apache.cxf
- cxf-rt-rs-extension-providers
- 3.3.4
+ ${version.cxf-rt-rs-client}
test
org.glassfish
javax.json
- 1.1.4
+ ${version.javax.json}
test
-
- javax.xml.bind
- jaxb-api
- 2.3.1
+ org.apache.cxf
+ cxf-rt-rs-mp-client
+ ${version.cxf-rt-rs-mp-client}
test
@@ -94,27 +71,60 @@
liberty-maven-plugin
${version.liberty-maven-plugin}
+
+ org.apache.maven.plugins
+ maven-dependency-plugin
+ ${version.maven-dependency-plugin}
+
+
+ copy-derby-dependency
+ package
+
+ copy-dependencies
+
+
+ derby
+ ${project.build.directory}/liberty/wlp/usr/shared/resources/
+
+ ${testServerHttpPort}
+
+
+
+
+
org.apache.maven.plugins
maven-war-plugin
${version.maven-war-plugin}
-
- org.apache.maven.plugins
- maven-surefire-plugin
- ${version.maven-surefire-plugin}
-
-
-
- org.apache.maven.plugins
- maven-failsafe-plugin
- ${version.maven-failsafe-plugin}
-
-
- ${liberty.var.default.http.port}
-
-
-
+
+
+
+ 1.8
+ 1.8
+ UTF-8
+ UTF-8
+ false
+
+
+ 8.0.0
+ 3.2
+ 10.14.2.0
+ 3.1
+ 2.10
+ 3.2.3
+ 4.12
+ 1.0.5
+ 3.2.6
+ 1.0.4
+ 3.3.1
+
+
+ openliberty
+ 9080
+ 9443
+ 9080
+
\ No newline at end of file
diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/person/dao/PersonDao.java b/open-liberty/src/main/java/com/baeldung/openliberty/person/dao/PersonDao.java
new file mode 100644
index 0000000000..e2d408d8b0
--- /dev/null
+++ b/open-liberty/src/main/java/com/baeldung/openliberty/person/dao/PersonDao.java
@@ -0,0 +1,24 @@
+package com.baeldung.openliberty.person.dao;
+
+import javax.enterprise.context.RequestScoped;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+import com.baeldung.openliberty.person.model.Person;
+
+@RequestScoped
+public class PersonDao {
+
+ @PersistenceContext(name = "jpa-unit")
+ private EntityManager em;
+
+ public Person createPerson(Person person) {
+ em.persist(person);
+ return person;
+ }
+
+ public Person readPerson(int personId) {
+ return em.find(Person.class, personId);
+ }
+
+}
\ No newline at end of file
diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/person/model/Person.java b/open-liberty/src/main/java/com/baeldung/openliberty/person/model/Person.java
new file mode 100644
index 0000000000..e506fd005b
--- /dev/null
+++ b/open-liberty/src/main/java/com/baeldung/openliberty/person/model/Person.java
@@ -0,0 +1,61 @@
+package com.baeldung.openliberty.person.model;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.validation.constraints.Email;
+import javax.validation.constraints.NotBlank;
+
+@Entity
+public class Person {
+
+ @GeneratedValue(strategy = GenerationType.AUTO)
+ @Id
+ private int id;
+
+ @NotBlank
+ private String username;
+
+ @Email
+ private String email;
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String getUsername() {
+ return username;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ public String getEmail() {
+ return email;
+ }
+
+ public void setEmail(String email) {
+ this.email = email;
+ }
+
+ public Person(int id, @NotBlank String username, @Email String email) {
+ super();
+ this.id = id;
+ this.username = username;
+ this.email = email;
+ }
+
+ public Person() {
+ super();
+ }
+
+ public String toString() {
+ return this.id + ":" +this.username;
+ }
+}
diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/person/resource/PersonResource.java b/open-liberty/src/main/java/com/baeldung/openliberty/person/resource/PersonResource.java
new file mode 100644
index 0000000000..a729805e13
--- /dev/null
+++ b/open-liberty/src/main/java/com/baeldung/openliberty/person/resource/PersonResource.java
@@ -0,0 +1,68 @@
+package com.baeldung.openliberty.person.resource;
+
+import java.util.Set;
+
+import javax.enterprise.context.RequestScoped;
+import javax.inject.Inject;
+import javax.validation.Validator;
+import javax.validation.ConstraintViolation;
+import javax.transaction.Transactional;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import com.baeldung.openliberty.person.dao.PersonDao;
+import com.baeldung.openliberty.person.model.Person;
+
+@RequestScoped
+@Path("person")
+public class PersonResource {
+
+ @Inject
+ private PersonDao personDao;
+
+ @Inject
+ Validator validator;
+
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ public Person getPerson() {
+ Person person = new Person(1, "normanlewis", "normanlewis@email.com");
+ return person;
+ }
+
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Transactional
+ public Response addPerson(Person person) {
+ Set> violations = validator.validate(person);
+ if (violations.size() > 0) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("Constraint Violation Found: ").append(System.lineSeparator());
+ for (ConstraintViolation violation : violations) {
+ sb.append(violation.getPropertyPath())
+ .append(" ")
+ .append(violation.getMessage())
+ .append(System.lineSeparator());
+ }
+ return Response.status(Response.Status.BAD_REQUEST).entity(sb.toString()).build();
+ }
+ personDao.createPerson(person);
+ String respMessage = "Person #" + person.getId() + " created successfully.";
+ return Response.status(Response.Status.OK).entity(respMessage).build();
+ }
+
+ @GET
+ @Path("{id}")
+ @Produces(MediaType.APPLICATION_JSON)
+ @Transactional
+ public Person getPerson(@PathParam("id") int id) {
+ Person person = personDao.readPerson(id);
+ return person;
+ }
+}
diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/ApiApplication.java b/open-liberty/src/main/java/com/baeldung/openliberty/rest/ApiApplication.java
similarity index 79%
rename from open-liberty/src/main/java/com/baeldung/openliberty/ApiApplication.java
rename to open-liberty/src/main/java/com/baeldung/openliberty/rest/ApiApplication.java
index 92855b5876..176eaccaed 100644
--- a/open-liberty/src/main/java/com/baeldung/openliberty/ApiApplication.java
+++ b/open-liberty/src/main/java/com/baeldung/openliberty/rest/ApiApplication.java
@@ -1,4 +1,4 @@
-package com.baeldung.openliberty;
+package com.baeldung.openliberty.rest;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/PersonClient.java b/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/PersonClient.java
new file mode 100644
index 0000000000..205df8e24b
--- /dev/null
+++ b/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/PersonClient.java
@@ -0,0 +1,20 @@
+package com.baeldung.openliberty.rest.consumes;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.ProcessingException;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
+import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
+
+@RegisterRestClient(configKey = "personClient", baseUri = "http://localhost:9080/")
+@RegisterProvider(UriNotFoundExceptionMapper.class)
+@Path("/api/person/1")
+public interface PersonClient extends AutoCloseable {
+
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ public String getPerson() throws UriNotFoundException, ProcessingException;
+}
diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/RestConsumer.java b/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/RestConsumer.java
new file mode 100644
index 0000000000..63496fb4e4
--- /dev/null
+++ b/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/RestConsumer.java
@@ -0,0 +1,32 @@
+package com.baeldung.openliberty.rest.consumes;
+
+import java.net.URI;
+
+import javax.ws.rs.ProcessingException;
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.core.Response;
+
+import org.eclipse.microprofile.rest.client.RestClientBuilder;
+
+public class RestConsumer {
+
+ public static String consumeWithJsonb(String targetUrl) {
+ Client client = ClientBuilder.newClient();
+ Response response = client.target(targetUrl).request().get();
+ String result = response.readEntity(String.class);
+ response.close();
+ client.close();
+ return result;
+ }
+
+ public static String consumeWithRestBuilder(String targetUrl) throws ProcessingException, UriNotFoundException {
+ URI target = URI.create(targetUrl);;
+ PersonClient person = RestClientBuilder.newBuilder()
+ .baseUri(target)
+ .register(UriNotFoundExceptionMapper.class)
+ .build(PersonClient.class);
+ return person.getPerson();
+ }
+
+}
diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/UriNotFoundException.java b/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/UriNotFoundException.java
new file mode 100644
index 0000000000..a803b43bb4
--- /dev/null
+++ b/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/UriNotFoundException.java
@@ -0,0 +1,14 @@
+package com.baeldung.openliberty.rest.consumes;
+
+public class UriNotFoundException extends Exception {
+
+ private static final long serialVersionUID = 1L;
+
+ public UriNotFoundException() {
+ super();
+ }
+
+ public UriNotFoundException(String message) {
+ super(message);
+ }
+}
diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/UriNotFoundExceptionMapper.java b/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/UriNotFoundExceptionMapper.java
new file mode 100644
index 0000000000..728c4415b3
--- /dev/null
+++ b/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/UriNotFoundExceptionMapper.java
@@ -0,0 +1,23 @@
+package com.baeldung.openliberty.rest.consumes;
+
+import java.util.logging.Logger;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.Provider;
+import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper;
+
+@Provider
+public class UriNotFoundExceptionMapper implements ResponseExceptionMapper {
+ Logger LOG = Logger.getLogger(UriNotFoundException.class.getName());
+
+ @Override
+ public boolean handles(int status, MultivaluedMap headers) {
+ LOG.info("status = " + status);
+ return status == 404;
+ }
+
+ @Override
+ public UriNotFoundException toThrowable(Response response) {
+ return new UriNotFoundException();
+ }
+}
diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/app/AppServlet.java b/open-liberty/src/main/java/com/baeldung/openliberty/servlet/AppServlet.java
similarity index 70%
rename from open-liberty/src/main/java/com/baeldung/openliberty/app/AppServlet.java
rename to open-liberty/src/main/java/com/baeldung/openliberty/servlet/AppServlet.java
index 526e13c0e9..ad61604b32 100644
--- a/open-liberty/src/main/java/com/baeldung/openliberty/app/AppServlet.java
+++ b/open-liberty/src/main/java/com/baeldung/openliberty/servlet/AppServlet.java
@@ -1,4 +1,4 @@
-package com.baeldung.openliberty.app;
+package com.baeldung.openliberty.servlet;
import java.io.IOException;
@@ -10,11 +10,12 @@ import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns="/app")
public class AppServlet extends HttpServlet {
+
private static final long serialVersionUID = 1L;
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.getWriter().append("Open Liberty: greetHello! Welcome to Open Liberty
");
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ String htmlOutput = "Hello! Welcome to Open Liberty
";
+ response.getWriter().append(htmlOutput);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/user/UserResource.java b/open-liberty/src/main/java/com/baeldung/openliberty/user/UserResource.java
deleted file mode 100644
index 86f2709f4d..0000000000
--- a/open-liberty/src/main/java/com/baeldung/openliberty/user/UserResource.java
+++ /dev/null
@@ -1,50 +0,0 @@
-package com.baeldung.openliberty.user;
-
-import javax.enterprise.context.RequestScoped;
-import javax.inject.Inject;
-import javax.transaction.Transactional;
-import javax.ws.rs.Consumes;
-import javax.ws.rs.FormParam;
-import javax.ws.rs.GET;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-
-import com.baeldung.openliberty.user.dao.UserDao;
-import com.baeldung.openliberty.user.model.User;
-
-@RequestScoped
-@Path("user")
-public class UserResource {
-
- @Inject
- UserService userService;
-
- @Inject
- private UserDao userDAO;
-
- @GET
- @Produces(MediaType.APPLICATION_JSON)
- public User getUser() {
- return userService.getUser();
- }
-
- /**
- * This method creates a new user from the submitted data (firstName, lastName and
- * email)
- */
- @POST
- @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
- @Transactional
- public Response addNewUser(@FormParam("firstName") String firstName,
- @FormParam("lastName") String lastName, @FormParam("email") String email) {
- System.out.println(firstName);
- System.out.println(lastName);
- System.out.println(email);
- User newUser = new User(firstName, lastName, email);
- userDAO.createUser(newUser);
- return Response.status(Response.Status.NO_CONTENT).build();
- }
-}
diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/user/UserService.java b/open-liberty/src/main/java/com/baeldung/openliberty/user/UserService.java
deleted file mode 100644
index 4c79b9057f..0000000000
--- a/open-liberty/src/main/java/com/baeldung/openliberty/user/UserService.java
+++ /dev/null
@@ -1,18 +0,0 @@
-package com.baeldung.openliberty.user;
-
-import javax.enterprise.context.ApplicationScoped;
-
-import com.baeldung.openliberty.user.model.User;
-
-@ApplicationScoped
-public class UserService {
-
- public User getUser() {
- User user = new User();
- user.setFirstName("Norman");
- user.setLastName("Lewis");
- user.setEmail("normanlewis@email.com");
- return user;
- }
-
-}
diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/user/dao/UserDao.java b/open-liberty/src/main/java/com/baeldung/openliberty/user/dao/UserDao.java
deleted file mode 100644
index 4c006db4d7..0000000000
--- a/open-liberty/src/main/java/com/baeldung/openliberty/user/dao/UserDao.java
+++ /dev/null
@@ -1,42 +0,0 @@
-package com.baeldung.openliberty.user.dao;
-import java.util.List;
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceContext;
-
-import com.baeldung.openliberty.user.model.User;
-
-import javax.enterprise.context.RequestScoped;
-
-@RequestScoped
-public class UserDao {
-
- @PersistenceContext(name = "jpa-unit")
- private EntityManager em;
-
- public void createUser(User user) {
- em.persist(user);
- }
-
- public User readUser(int userId) {
- return em.find(User.class, userId);
- }
-
- public void updateUser(User user) {
- em.merge(user);
- }
-
- public void deleteUser(User user) {
- em.remove(user);
- }
-
- public List readAllUsers() {
- return em.createNamedQuery("User.findAll", User.class).getResultList();
- }
-
- public List findUser(String name, String location, String time) {
- return em.createNamedQuery("User.findUser", User.class)
- .setParameter("name", name)
- .setParameter("location", location)
- .setParameter("time", time).getResultList();
- }
-}
\ No newline at end of file
diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/user/model/User.java b/open-liberty/src/main/java/com/baeldung/openliberty/user/model/User.java
deleted file mode 100644
index 6c49c95bf7..0000000000
--- a/open-liberty/src/main/java/com/baeldung/openliberty/user/model/User.java
+++ /dev/null
@@ -1,63 +0,0 @@
-package com.baeldung.openliberty.user.model;
-
-import javax.persistence.Column;
-import javax.persistence.Entity;
-import javax.persistence.GeneratedValue;
-import javax.persistence.GenerationType;
-import javax.persistence.Id;
-import javax.persistence.Table;
-
-@Entity
-@Table(name = "Event")
-public class User {
-
- @GeneratedValue(strategy = GenerationType.AUTO)
- @Id
- @Column(name = "id")
- private int id;
-
- @Column(name = "first_name")
- private String firstName;
-
- @Column(name = "last_name")
- private String lastName;
-
- @Column(name = "email")
- private String email;
-
- public String getFirstName() {
- return firstName;
- }
-
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
-
- public String getLastName() {
- return lastName;
- }
-
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
-
- public String getEmail() {
- return email;
- }
-
- public void setEmail(String email) {
- this.email = email;
- }
-
- public User(String firstName, String lastName, String email) {
- super();
- this.firstName = firstName;
- this.lastName = lastName;
- this.email = email;
- }
-
- public User() {
- super();
- }
-
-}
diff --git a/open-liberty/src/main/liberty/config/server.xml b/open-liberty/src/main/liberty/config/server.xml
index a83c0b462c..567d6f3e33 100644
--- a/open-liberty/src/main/liberty/config/server.xml
+++ b/open-liberty/src/main/liberty/config/server.xml
@@ -1,51 +1,34 @@
-
-
- appSecurity-2.0
- jaxrs-2.1
- jsonp-1.1
- cdi-2.0
- jpa-2.2
- mpMetrics-2.0
- mpHealth-2.0
- mpConfig-1.3
- restConnector-2.0
- jdbc-4.2
-
+
+
+ mpHealth-2.0
+ servlet-4.0
+ jaxrs-2.1
+ jsonp-1.1
+
+ cdi-2.0
+ jpa-2.2
+ beanValidation-2.0
+ mpConfig-1.3
+ mpRestClient-1.3
+
-
-
+
+
-
-
+
-
+
-
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
\ No newline at end of file
diff --git a/open-liberty/src/main/resources/META-INF/persistence.xml b/open-liberty/src/main/resources/META-INF/persistence.xml
index 1ad7dd2bd8..ca8ad1a5c9 100644
--- a/open-liberty/src/main/resources/META-INF/persistence.xml
+++ b/open-liberty/src/main/resources/META-INF/persistence.xml
@@ -1,24 +1,14 @@
-
-
- org.eclipse.persistence.jpa.PersistenceProvider
-
- jdbc/h2test
-
- false
+
+ jdbc/jpadatasource
+
+
+
+
\ No newline at end of file
diff --git a/open-liberty/src/test/java/com/baeldung/openliberty/RestClientTest.java b/open-liberty/src/test/java/com/baeldung/openliberty/RestClientTest.java
new file mode 100644
index 0000000000..9f7b4cd05d
--- /dev/null
+++ b/open-liberty/src/test/java/com/baeldung/openliberty/RestClientTest.java
@@ -0,0 +1,51 @@
+package com.baeldung.openliberty;
+
+import static org.junit.Assert.assertEquals;
+
+import javax.json.bind.JsonbBuilder;
+import javax.ws.rs.ProcessingException;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.baeldung.openliberty.person.model.Person;
+import com.baeldung.openliberty.rest.consumes.RestConsumer;
+import com.baeldung.openliberty.rest.consumes.UriNotFoundException;
+
+public class RestClientTest {
+
+ private static String BASE_URL;
+
+ private final String PERSON = "api/person";
+
+ @BeforeClass
+ public static void oneTimeSetup() {
+ BASE_URL = "http://localhost:9080";
+ }
+
+ @Test
+ public void testSuite() throws ProcessingException, UriNotFoundException {
+ this.testJsonBClientBuilder();
+ this.testRestClientBuilder();
+ }
+
+ public void testJsonBClientBuilder() {
+ String url = BASE_URL + "/" + PERSON + "/1";
+ String result = RestConsumer.consumeWithJsonb(url);
+
+ Person person = JsonbBuilder.create().fromJson(result, Person.class);
+ assert person.getId() == 1;
+ assertEquals(person.getUsername(), "normanlewis");
+ assertEquals(person.getEmail(), "normanlewis@email.com");
+ }
+
+ public void testRestClientBuilder() throws ProcessingException, UriNotFoundException {
+ String result = RestConsumer.consumeWithRestBuilder(BASE_URL);
+
+ Person person = JsonbBuilder.create().fromJson(result, Person.class);
+ assert person.getId() == 1;
+ assertEquals(person.getUsername(), "normanlewis");
+ assertEquals(person.getEmail(), "normanlewis@email.com");
+ }
+
+}
diff --git a/open-liberty/src/webapp/META-INF/microprofile-config.properties b/open-liberty/src/webapp/META-INF/microprofile-config.properties
index e69de29bb2..5488d9a4f6 100644
--- a/open-liberty/src/webapp/META-INF/microprofile-config.properties
+++ b/open-liberty/src/webapp/META-INF/microprofile-config.properties
@@ -0,0 +1 @@
+personClient/mp-rest/uri=http://localhost:9080/api/person
\ No newline at end of file