JAVA-13855 Create new microservices-modules (#12612)

This commit is contained in:
anuragkumawat
2022-08-20 14:20:24 +05:30
committed by GitHub
parent 14c998c3ac
commit 6bc1d484ca
95 changed files with 661 additions and 639 deletions
@@ -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,58 @@
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;
private String username;
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,52 @@
package com.baeldung.openliberty.person.resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
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("persons")
public class PersonResource {
@Inject
private PersonDao personDao;
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Person> getAllPersons() {
return Arrays.asList(new Person(1, "normanlewis", "normanlewis@email.com"));
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Transactional
public Response addPerson(Person person) {
personDao.createPerson(person);
String respMessage = "Person #" + person.getId() + " created successfully.";
return Response.status(Response.Status.CREATED).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;
}
}
@@ -0,0 +1,9 @@
package com.baeldung.openliberty.rest;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/api")
public class ApiApplication extends Application {
}
@@ -0,0 +1,18 @@
package com.baeldung.openliberty.rest.consumes;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;
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;
}
}
@@ -0,0 +1,27 @@
package com.baeldung.openliberty.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns="/app")
public class AppServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
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);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
@@ -0,0 +1,28 @@
<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>
</featureManager>
<webApplication location="open-liberty.war" contextRoot="/" />
<logging traceSpecification="com.ibm.ws.microprofile.health.*=all" hideMessage="SRVE9967W" />
<httpEndpoint host="*" httpPort="${default.http.port}" httpsPort="${default.https.port}" id="defaultHttpEndpoint" />
<library id="derbyJDBCLib">
<fileset dir="${shared.resource.dir}" includes="derby*.jar"/>
</library>
<!-- Datasource Configuration -->
<dataSource id="jpadatasource" jndiName="jdbc/jpadatasource">
<jdbcDriver libraryRef="derbyJDBCLib" />
<properties.derby.embedded databaseName="libertyDB" createDatabase="create" />
</dataSource>
</server>
@@ -0,0 +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
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<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,40 @@
package com.baeldung.openliberty;
import static org.junit.Assert.assertEquals;
import javax.json.bind.JsonbBuilder;
import org.junit.BeforeClass;
import org.junit.Test;
import com.baeldung.openliberty.person.model.Person;
import com.baeldung.openliberty.rest.consumes.RestConsumer;
public class RestClientLiveTest {
private static String BASE_URL;
private final String API_PERSON = "api/persons";
@BeforeClass
public static void oneTimeSetup() {
BASE_URL = "http://localhost:9080/";
}
@Test
public void testSuite() {
//run the test only when liberty server is started
//this.whenConsumeWithJsonb_thenGetPerson();
}
public void whenConsumeWithJsonb_thenGetPerson() {
String url = BASE_URL + API_PERSON + "/1";
String result = RestConsumer.consumeWithJsonb(url);
Person person = JsonbBuilder.create().fromJson(result, Person.class);
assertEquals(1, person.getId());
assertEquals("normanlewis", person.getUsername());
assertEquals("normanlewis@email.com", person.getEmail());
}
}