Merge pull request #33 from eugenp/master

update
This commit is contained in:
Maiklins 2020-02-07 10:16:37 +01:00 committed by GitHub
commit 307205ce04
727 changed files with 1965 additions and 1535 deletions

View File

@ -67,7 +67,12 @@
<groupId>org.cactoos</groupId>
<artifactId>cactoos</artifactId>
<version>${cactoos.version}</version>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-base-bom</artifactId>
<version>${cache2k.version}</version>
<type>pom</type>
</dependency>
</dependencies>
@ -88,5 +93,6 @@
<cactoos.version>0.43</cactoos.version>
<airline.version>2.7.2</airline.version>
<cache2k.version>1.2.3.Final</cache2k.version>
</properties>
</project>

View File

@ -0,0 +1,41 @@
package com.baeldung.cache2k;
import java.util.Objects;
import org.cache2k.Cache;
import org.cache2k.Cache2kBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ProductHelper {
final Logger LOGGER = LoggerFactory.getLogger(ProductHelper.class);
private Cache<String, Integer> cachedDiscounts;
public ProductHelper() {
cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class)
.name("discount")
.eternal(true)
.entryCapacity(100)
.build();
initDiscountCache("Sports", 20);
}
public void initDiscountCache(String productType, Integer value) {
cachedDiscounts.put(productType, value);
}
public Integer getDiscount(String productType) {
Integer discount = cachedDiscounts.get(productType);
if (Objects.isNull(discount)) {
LOGGER.info("Discount for {} not found.", productType);
discount = 0;
} else {
LOGGER.info("Discount for {} found.", productType);
}
return discount;
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.cache2k;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import org.cache2k.Cache;
import org.cache2k.Cache2kBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ProductHelperUsingLoader {
final Logger LOGGER = LoggerFactory.getLogger(ProductHelperUsingLoader.class);
private Cache<String, Integer> cachedDiscounts;
public ProductHelperUsingLoader() {
cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class)
.name("discount-loader")
.eternal(false)
.expireAfterWrite(10, TimeUnit.MILLISECONDS)
.entryCapacity(100)
.loader((key) -> {
LOGGER.info("Calculating discount for {}.", key);
return "Sports".equalsIgnoreCase(key) ? 20 : 10;
})
.build();
}
public Integer getDiscount(String productType) {
Integer discount = cachedDiscounts.get(productType);
if (Objects.isNull(discount)) {
LOGGER.info("Discount for {} not found.", productType);
discount = 0;
} else {
LOGGER.info("Discount for {} found.", productType);
}
return discount;
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.cache2k;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import org.cache2k.Cache;
import org.cache2k.Cache2kBuilder;
import org.cache2k.CacheEntry;
import org.cache2k.event.CacheEntryCreatedListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ProductHelperWithEventListener {
final Logger LOGGER = LoggerFactory.getLogger(ProductHelperWithEventListener.class);
private Cache<String, Integer> cachedDiscounts;
public ProductHelperWithEventListener() {
cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class)
.name("discount-listener")
.eternal(false)
.expireAfterWrite(10, TimeUnit.MILLISECONDS)
.entryCapacity(100)
.loader((key) -> {
LOGGER.info("Calculating discount for {}.", key);
return "Sports".equalsIgnoreCase(key) ? 20 : 10;
})
.addListener(new CacheEntryCreatedListener<String, Integer>() {
@Override
public void onEntryCreated(Cache<String, Integer> cache, CacheEntry<String, Integer> entry) {
LOGGER.info("Entry created: [{}, {}].", entry.getKey(), entry.getValue());
}
})
.build();
}
public Integer getDiscount(String productType) {
Integer discount = cachedDiscounts.get(productType);
if (Objects.isNull(discount)) {
LOGGER.info("Discount for {} not found.", productType);
discount = 0;
} else {
LOGGER.info("Discount for {} found.", productType);
}
return discount;
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.cache2k;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import org.cache2k.Cache;
import org.cache2k.Cache2kBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ProductHelperWithExpiry {
final Logger LOGGER = LoggerFactory.getLogger(ProductHelperWithExpiry.class);
private Cache<String, Integer> cachedDiscounts;
public ProductHelperWithExpiry() {
cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class)
.name("discount-expiry")
.eternal(false)
.expireAfterWrite(5, TimeUnit.MILLISECONDS)
.entryCapacity(100)
.build();
initDiscountCache("Sports", 20);
}
public void initDiscountCache(String productType, Integer value) {
cachedDiscounts.put(productType, value);
}
public Integer getDiscount(String productType) {
Integer discount = cachedDiscounts.get(productType);
if (Objects.isNull(discount)) {
LOGGER.info("Discount for {} not found.", productType);
discount = 0;
} else {
LOGGER.info("Discount for {} found.", productType);
}
return discount;
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.cache2k;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class ProductHelperUnitTest {
ProductHelper productHelper = new ProductHelper();
@Test
public void whenInvokedGetDiscount_thenGetItFromCache() {
assertTrue(productHelper.getDiscount("Sports") == 20);
assertTrue(productHelper.getDiscount("Electronics") == 0);
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.cache2k;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class ProductHelperUsingLoaderUnitTest {
ProductHelperUsingLoader productHelper = new ProductHelperUsingLoader();
@Test
public void whenInvokedGetDiscount_thenPopulateCache() {
assertTrue(productHelper.getDiscount("Sports") == 20);
assertTrue(productHelper.getDiscount("Electronics") == 10);
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.cache2k;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class ProductHelperWithEventListenerUnitTest {
ProductHelperWithEventListener productHelper = new ProductHelperWithEventListener();
@Test
public void whenEntryAddedInCache_thenEventListenerCalled() {
assertTrue(productHelper.getDiscount("Sports") == 20);
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.cache2k;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class ProductHelperWithExpiryUnitTest {
ProductHelperWithExpiry productHelper = new ProductHelperWithExpiry();
@Test
public void whenInvokedGetDiscountForExpiredProduct_thenNoDiscount() throws InterruptedException {
assertTrue(productHelper.getDiscount("Sports") == 20);
Thread.sleep(20);
assertTrue(productHelper.getDiscount("Sports") == 0);
}
}

130
open-liberty/pom.xml Normal file
View File

@ -0,0 +1,130 @@
<?xml version='1.0' encoding='utf-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>open-liberty</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-web-api</artifactId>
<version>${version.jakarta.jakartaee-web-api}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile</groupId>
<artifactId>microprofile</artifactId>
<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>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>${version.cxf-rt-rs-client}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>${version.javax.json}</version>
<scope>test</scope>
</dependency>
<dependency>
<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>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- Enable liberty-maven plugin -->
<plugin>
<groupId>io.openliberty.tools</groupId>
<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>
</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>7070</testServerHttpPort>
</properties>
</project>

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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 {
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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>

View File

@ -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>

View File

@ -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 RestClientTest {
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());
}
}

47
pom.xml
View File

@ -538,6 +538,7 @@
<module>netflix-modules</module>
<module>ninja</module>
<module>open-liberty</module>
<module>oauth2-framework-impl</module>
<module>optaplanner</module>
@ -637,30 +638,8 @@
<module>spring-batch</module>
<module>spring-bom</module>
<module>spring-boot</module>
<module>spring-boot-modules</module>
<module>spring-boot-angular</module>
<module>spring-boot-bootstrap</module>
<!-- <module>spring-boot-cli</module> --> <!-- Not a maven project -->
<module>spring-boot-client</module>
<module>spring-boot-config-jpa-error</module>
<module>spring-boot-deployment</module>
<module>spring-boot-di</module>
<module>spring-boot-environment</module>
<module>spring-boot-flowable</module>
<module>spring-boot-jasypt</module>
<module>spring-boot-libraries</module>
<module>spring-boot-mvc-2</module>
<module>spring-boot-parent</module>
<module>spring-boot-performance</module>
<module>spring-boot-property-exp</module>
<module>spring-boot-rest</module>
<module>spring-boot-runtime</module>
<module>spring-boot-runtime/disabling-console-jul</module>
<module>spring-boot-runtime/disabling-console-log4j2</module>
<module>spring-boot-runtime/disabling-console-logback</module>
<module>spring-boot-security</module>
<module>spring-caching</module>
@ -1068,6 +1047,7 @@
<module>netflix-modules</module>
<module>ninja</module>
<module>open-liberty</module>
<module>oauth2-framework-impl</module>
<module>optaplanner</module>
@ -1159,31 +1139,8 @@
<module>spring-batch</module>
<module>spring-bom</module>
<module>spring-boot</module>
<module>spring-boot-modules</module>
<module>spring-boot-angular</module>
<module>spring-boot-bootstrap</module>
<!-- <module>spring-boot-cli</module> --> <!-- Not a maven project -->
<module>spring-boot-client</module>
<module>spring-boot-config-jpa-error</module>
<module>spring-boot-deployment</module>
<module>spring-boot-di</module>
<module>spring-boot-environment</module>
<module>spring-boot-flowable</module>
<module>spring-boot-jasypt</module>
<module>spring-boot-libraries</module>
<module>spring-boot-mvc</module>
<module>spring-boot-mvc-2</module>
<module>spring-boot-parent</module>
<module>spring-boot-performance</module>
<module>spring-boot-property-exp</module>
<module>spring-boot-rest</module>
<module>spring-boot-runtime</module>
<module>spring-boot-runtime/disabling-console-jul</module>
<module>spring-boot-runtime/disabling-console-log4j2</module>
<module>spring-boot-runtime/disabling-console-logback</module>
<module>spring-boot-security</module>
<module>spring-caching</module>

View File

@ -14,21 +14,36 @@
</parent>
<modules>
<module>spring-boot</module>
<module>spring-boot-admin</module>
<module>spring-boot-angular</module>
<module>spring-boot-artifacts</module>
<module>spring-boot-ctx-fluent</module>
<module>spring-boot-autoconfiguration</module>
<module>spring-boot-bootstrap</module>
<module>spring-boot-client</module>
<module>spring-boot-config-jpa-error</module>
<module>spring-boot-ctx-fluent</module>
<module>spring-boot-deployment</module>
<module>spring-boot-camel</module>
<!-- <module>spring-boot-cli</module> --> <!-- Not a maven project -->
<module>spring-boot-custom-starter</module>
<module>spring-boot-crud</module>
<module>spring-boot-data</module>
<module>spring-boot-environment</module>
<module>spring-boot-flowable</module>
<!-- <module>spring-boot-gradle</module> --> <!-- Not a maven project -->
<module>spring-boot-jasypt</module>
<module>spring-boot-keycloak</module>
<module>spring-boot-libraries</module>
<module>spring-boot-logging-log4j2</module>
<module>spring-boot-kotlin</module>
<module>spring-boot-mvc</module>
<module>spring-boot-mvc-2</module>
<module>spring-boot-mvc-birt</module>
<module>spring-boot-nashorn</module>
<module>spring-boot-properties</module>
<module>spring-boot-property-exp</module>
<module>spring-boot-runtime</module>
<module>spring-boot-springdoc</module>
<module>spring-boot-testing</module>
<module>spring-boot-vue</module>

View File

@ -19,8 +19,4 @@
<module>spring-boot-admin-client</module>
</modules>
<properties>
<spring-boot.version>2.1.9.RELEASE</spring-boot.version>
</properties>
</project>

View File

@ -58,7 +58,7 @@
</build>
<properties>
<spring-boot-admin-starter-client.version>2.1.6</spring-boot-admin-starter-client.version>
<spring-boot-admin-starter-client.version>2.2.2</spring-boot-admin-starter-client.version>
<spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version>
</properties>

View File

@ -79,8 +79,8 @@
</build>
<properties>
<spring-boot-admin-server.version>2.1.6</spring-boot-admin-server.version>
<spring-boot-admin-starter-client.version>2.1.6</spring-boot-admin-starter-client.version>
<spring-boot-admin-server.version>2.2.2</spring-boot-admin-server.version>
<spring-boot-admin-starter-client.version>2.2.2</spring-boot-admin-starter-client.version>
<spring-boot-admin-server-ui-login.version>1.5.7</spring-boot-admin-server-ui-login.version>
<spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version>
</properties>

View File

@ -12,7 +12,7 @@
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>

View File

@ -1,28 +1,28 @@
package com.baeldung.application;
import com.baeldung.application.entities.User;
import com.baeldung.application.repositories.UserRepository;
import java.util.stream.Stream;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
CommandLineRunner init(UserRepository userRepository) {
return args -> {
Stream.of("John", "Julie", "Jennifer", "Helen", "Rachel").forEach(name -> {
User user = new User(name, name.toLowerCase() + "@domain.com");
userRepository.save(user);
});
userRepository.findAll().forEach(System.out::println);
};
}
}
package com.baeldung.application;
import com.baeldung.application.entities.User;
import com.baeldung.application.repositories.UserRepository;
import java.util.stream.Stream;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
CommandLineRunner init(UserRepository userRepository) {
return args -> {
Stream.of("John", "Julie", "Jennifer", "Helen", "Rachel").forEach(name -> {
User user = new User(name, name.toLowerCase() + "@domain.com");
userRepository.save(user);
});
userRepository.findAll().forEach(System.out::println);
};
}
}

View File

@ -1,31 +1,31 @@
package com.baeldung.application.controllers;
import com.baeldung.application.entities.User;
import com.baeldung.application.repositories.UserRepository;
import java.util.List;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin(origins = "http://localhost:4200")
public class UserController {
private final UserRepository userRepository;
public UserController(UserRepository userRepository) {
this.userRepository = userRepository;
}
@GetMapping("/users")
public List<User> getUsers() {
return (List<User>) userRepository.findAll();
}
@PostMapping("/users")
void addUser(@RequestBody User user) {
userRepository.save(user);
}
}
package com.baeldung.application.controllers;
import com.baeldung.application.entities.User;
import com.baeldung.application.repositories.UserRepository;
import java.util.List;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin(origins = "http://localhost:4200")
public class UserController {
private final UserRepository userRepository;
public UserController(UserRepository userRepository) {
this.userRepository = userRepository;
}
@GetMapping("/users")
public List<User> getUsers() {
return (List<User>) userRepository.findAll();
}
@PostMapping("/users")
void addUser(@RequestBody User user) {
userRepository.save(user);
}
}

View File

@ -1,43 +1,43 @@
package com.baeldung.application.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private final String name;
private final String email;
public User() {
this.name = "";
this.email = "";
}
public User(String name, String email) {
this.name = name;
this.email = email;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
@Override
public String toString() {
return "User{" + "id=" + id + ", name=" + name + ", email=" + email + '}';
}
}
package com.baeldung.application.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private final String name;
private final String email;
public User() {
this.name = "";
this.email = "";
}
public User(String name, String email) {
this.name = name;
this.email = email;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
@Override
public String toString() {
return "User{" + "id=" + id + ", name=" + name + ", email=" + email + '}';
}
}

View File

@ -1,9 +1,9 @@
package com.baeldung.application.repositories;
import com.baeldung.application.entities.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.CrossOrigin;
@Repository
public interface UserRepository extends CrudRepository<User, Long>{}
package com.baeldung.application.repositories;
import com.baeldung.application.entities.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.CrossOrigin;
@Repository
public interface UserRepository extends CrudRepository<User, Long>{}

View File

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 5.3 KiB

Some files were not shown because too many files have changed in this diff Show More