Open Liberty with Servlet, Rest, Consume rest using json-b, microprofile
This commit is contained in:
parent
16f7133f21
commit
6d517ce9fb
|
@ -9,78 +9,55 @@
|
|||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
<!-- Plugin versions -->
|
||||
<version.liberty-maven-plugin>3.1</version.liberty-maven-plugin>
|
||||
<version.maven-war-plugin>3.2.3</version.maven-war-plugin>
|
||||
<version.maven-surefire-plugin>2.22.2</version.maven-surefire-plugin>
|
||||
<version.maven-failsafe-plugin>2.22.2</version.maven-failsafe-plugin>
|
||||
<!-- Liberty configuration -->
|
||||
<liberty.var.default.http.port>9080</liberty.var.default.http.port>
|
||||
<liberty.var.default.https.port>9443</liberty.var.default.https.port>
|
||||
<liberty.var.app.context.root>openliberty</liberty.var.app.context.root>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.derby</groupId>
|
||||
<artifactId>derby</artifactId>
|
||||
<version>10.14.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.186</version>
|
||||
</dependency>
|
||||
<!-- Provided dependencies -->
|
||||
<dependency>
|
||||
<groupId>jakarta.platform</groupId>
|
||||
<artifactId>jakarta.jakartaee-web-api</artifactId>
|
||||
<version>8.0.0</version>
|
||||
<version>${version.jakarta.jakartaee-web-api}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.microprofile</groupId>
|
||||
<artifactId>microprofile</artifactId>
|
||||
<version>3.0</version>
|
||||
<version>${version.microprofile}</version>
|
||||
<type>pom</type>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.derby</groupId>
|
||||
<artifactId>derby</artifactId>
|
||||
<version>${version.derby}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- For tests -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>5.5.2</version>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${version.junit}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse</groupId>
|
||||
<artifactId>yasson</artifactId>
|
||||
<version>${version.yasson}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-rs-client</artifactId>
|
||||
<version>3.3.4</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-rs-extension-providers</artifactId>
|
||||
<version>3.3.4</version>
|
||||
<version>${version.cxf-rt-rs-client}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.json</artifactId>
|
||||
<version>1.1.4</version>
|
||||
<version>${version.javax.json}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- Support for JDK 9 and above -->
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>2.3.1</version>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-rs-mp-client</artifactId>
|
||||
<version>${version.cxf-rt-rs-mp-client}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
@ -94,27 +71,60 @@
|
|||
<artifactId>liberty-maven-plugin</artifactId>
|
||||
<version>${version.liberty-maven-plugin}</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>${version.maven-dependency-plugin}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-derby-dependency</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<includeArtifactIds>derby</includeArtifactIds>
|
||||
<outputDirectory>${project.build.directory}/liberty/wlp/usr/shared/resources/</outputDirectory>
|
||||
<systemPropertyVariables>
|
||||
<liberty.test.port>${testServerHttpPort}</liberty.test.port>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${version.maven-war-plugin}</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${version.maven-surefire-plugin}</version>
|
||||
</plugin>
|
||||
<!-- Plugin to run functional tests -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>${version.maven-failsafe-plugin}</version>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<http.port>${liberty.var.default.http.port}</http.port>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
|
||||
<!-- versions -->
|
||||
<version.jakarta.jakartaee-web-api>8.0.0</version.jakarta.jakartaee-web-api>
|
||||
<version.microprofile>3.2</version.microprofile>
|
||||
<version.derby>10.14.2.0</version.derby>
|
||||
<version.liberty-maven-plugin>3.1</version.liberty-maven-plugin>
|
||||
<version.maven-dependency-plugin>2.10</version.maven-dependency-plugin>
|
||||
<version.maven-war-plugin>3.2.3</version.maven-war-plugin>
|
||||
<version.junit>4.12</version.junit>
|
||||
<version.yasson>1.0.5</version.yasson>
|
||||
<version.cxf-rt-rs-client>3.2.6</version.cxf-rt-rs-client>
|
||||
<version.javax.json>1.0.4</version.javax.json>
|
||||
<version.cxf-rt-rs-mp-client>3.3.1</version.cxf-rt-rs-mp-client>
|
||||
|
||||
<!-- Liberty configuration -->
|
||||
<liberty.var.app.context.root>openliberty</liberty.var.app.context.root>
|
||||
<liberty.var.default.http.port>9080</liberty.var.default.http.port>
|
||||
<liberty.var.default.https.port>9443</liberty.var.default.https.port>
|
||||
<testServerHttpPort>9080</testServerHttpPort>
|
||||
</properties>
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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<ConstraintViolation<Person>> violations = validator.validate(person);
|
||||
if (violations.size() > 0) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Constraint Violation Found: ").append(System.lineSeparator());
|
||||
for (ConstraintViolation<Person> 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;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.openliberty;
|
||||
package com.baeldung.openliberty.rest;
|
||||
|
||||
import javax.ws.rs.ApplicationPath;
|
||||
import javax.ws.rs.core.Application;
|
|
@ -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;
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<UriNotFoundException> {
|
||||
Logger LOG = Logger.getLogger(UriNotFoundException.class.getName());
|
||||
|
||||
@Override
|
||||
public boolean handles(int status, MultivaluedMap<String, Object> headers) {
|
||||
LOG.info("status = " + status);
|
||||
return status == 404;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UriNotFoundException toThrowable(Response response) {
|
||||
return new UriNotFoundException();
|
||||
}
|
||||
}
|
|
@ -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("<html><head><title>Open Liberty: greet</title></head><body><h2>Hello! Welcome to Open Liberty</h2></body></html>");
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
String htmlOutput = "<html><h2>Hello! Welcome to Open Liberty</h2></html>";
|
||||
response.getWriter().append(htmlOutput);
|
||||
}
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<User> readAllUsers() {
|
||||
return em.createNamedQuery("User.findAll", User.class).getResultList();
|
||||
}
|
||||
|
||||
public List<User> findUser(String name, String location, String time) {
|
||||
return em.createNamedQuery("User.findUser", User.class)
|
||||
.setParameter("name", name)
|
||||
.setParameter("location", location)
|
||||
.setParameter("time", time).getResultList();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,51 +1,34 @@
|
|||
<server description="Sample Liberty server">
|
||||
<featureManager>
|
||||
<feature>appSecurity-2.0</feature>
|
||||
<feature>jaxrs-2.1</feature>
|
||||
<feature>jsonp-1.1</feature>
|
||||
<feature>cdi-2.0</feature>
|
||||
<feature>jpa-2.2</feature>
|
||||
<feature>mpMetrics-2.0</feature>
|
||||
<feature>mpHealth-2.0</feature>
|
||||
<feature>mpConfig-1.3</feature>
|
||||
<feature>restConnector-2.0</feature>
|
||||
<feature>jdbc-4.2</feature>
|
||||
</featureManager>
|
||||
<server description="Baeldung Open Liberty server">
|
||||
<featureManager>
|
||||
<feature>mpHealth-2.0</feature>
|
||||
<feature>servlet-4.0</feature>
|
||||
<feature>jaxrs-2.1</feature>
|
||||
<feature>jsonp-1.1</feature>
|
||||
<!-- <feature>jsonb-1.0</feature> -->
|
||||
<feature>cdi-2.0</feature>
|
||||
<feature>jpa-2.2</feature>
|
||||
<feature>beanValidation-2.0</feature>
|
||||
<feature>mpConfig-1.3</feature>
|
||||
<feature>mpRestClient-1.3</feature>
|
||||
</featureManager>
|
||||
|
||||
<keyStore id="defaultKeyStore" password="Liberty" />
|
||||
<quickStartSecurity userName="blogAdmin"
|
||||
userPassword="blogAdminPassword" />
|
||||
<keyStore id="defaultKeyStore" password="Liberty" />
|
||||
|
||||
<webApplication location="open-liberty.war" contextRoot="/" />
|
||||
|
||||
<webApplication location="open-liberty.war"
|
||||
contextRoot="/" />
|
||||
<mpMetrics authentication="false" />
|
||||
<logging traceSpecification="com.ibm.ws.microprofile.health.*=all" hideMessage="SRVE9967W" />
|
||||
|
||||
<logging
|
||||
traceSpecification="com.ibm.ws.microprofile.health.*=all" />
|
||||
<httpEndpoint host="*" httpPort="${default.http.port}" httpsPort="${default.https.port}" id="defaultHttpEndpoint" />
|
||||
|
||||
<httpEndpoint host="*" httpPort="${default.http.port}"
|
||||
httpsPort="${default.https.port}" id="defaultHttpEndpoint" />
|
||||
<library id="derbyJDBCLib">
|
||||
<!-- <fileset dir="${project.build.directory}/open-liberty/WEB-INF/lib" includes="derby*.jar" /> -->
|
||||
<fileset dir="/Users/anshulbansal/eclipse-workspace/tutorials/open-liberty/target/open-liberty/WEB-INF/lib" includes="derby*.jar" />
|
||||
</library>
|
||||
|
||||
<variable name="io_openliberty_guides_system_inMaintenance"
|
||||
value="false" />
|
||||
|
||||
<library id="derby">
|
||||
<fileset dir="${shared.resource.dir}" includes="derby*.jar" />
|
||||
</library>
|
||||
|
||||
<library id="H2JDBCLib">
|
||||
<fileset dir="/lib" includes="h2-1.4.186.jar" />
|
||||
</library>
|
||||
|
||||
<!-- <dataSource id="DefaultDataSource"> <jdbcDriver libraryRef="derby"
|
||||
/> Example properties referencing an in-memory Derby Embedded database <properties.derby.embedded
|
||||
databaseName="memory:defaultdb" createDatabase="create" user="dbuser" password="dbpass"
|
||||
/> </dataSource> -->
|
||||
<dataSource id="h2test" jndiName="jdbc/h2test">
|
||||
<!-- Define the DataSource class names on the <jdbcDriver> element -->
|
||||
<jdbcDriver libraryRef="H2JDBCLib" />
|
||||
<properties URL="jdbc:h2:mem:testdb" />
|
||||
</dataSource>
|
||||
<!-- Datasource Configuration -->
|
||||
<dataSource id="jpadatasource" jndiName="jdbc/jpadatasource">
|
||||
<jdbcDriver libraryRef="derbyJDBCLib" />
|
||||
<properties.derby.embedded databaseName="libertyDB" createDatabase="create" />
|
||||
</dataSource>
|
||||
|
||||
</server>
|
|
@ -1,24 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence version="2.2"
|
||||
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
|
||||
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
|
||||
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
|
||||
<!-- <persistence-unit name="jpa-unit"
|
||||
transaction-type="JTA">
|
||||
<jta-data-source>jdbc/libertyjpadatasource</jta-data-source>
|
||||
<properties>
|
||||
<property name="eclipselink.ddl-generation"
|
||||
value="create-tables" />
|
||||
<property name="eclipselink.ddl-generation.output-mode"
|
||||
value="both" />
|
||||
</properties>
|
||||
</persistence-unit> -->
|
||||
<persistence-unit name="h2test" transaction-type="JTA">
|
||||
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
|
||||
|
||||
<jta-data-source>jdbc/h2test</jta-data-source>
|
||||
|
||||
<exclude-unlisted-classes>false</exclude-unlisted-classes>
|
||||
<persistence-unit name="jpa-unit" transaction-type="JTA">
|
||||
<jta-data-source>jdbc/jpadatasource</jta-data-source>
|
||||
<properties>
|
||||
<property name="eclipselink.ddl-generation" value="create-tables"/>
|
||||
<property name="eclipselink.ddl-generation.output-mode" value="both" />
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
personClient/mp-rest/uri=http://localhost:9080/api/person
|
Loading…
Reference in New Issue