From bb09c8675db1eaecc61ceb5cd7b2efa785bf3363 Mon Sep 17 00:00:00 2001 From: Attila Uhrin Date: Sat, 9 Apr 2022 17:15:06 +0200 Subject: [PATCH 01/11] Add examples for Java HttpClient with Basic authentication. --- .../HttpClientBasicAuthentication.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpclient/basicauthentication/HttpClientBasicAuthentication.java diff --git a/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpclient/basicauthentication/HttpClientBasicAuthentication.java b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpclient/basicauthentication/HttpClientBasicAuthentication.java new file mode 100644 index 0000000000..3ade3bd553 --- /dev/null +++ b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpclient/basicauthentication/HttpClientBasicAuthentication.java @@ -0,0 +1,67 @@ +package com.baeldung.httpclient.basicauthentication; + +import java.io.IOException; +import java.net.Authenticator; +import java.net.PasswordAuthentication; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.net.http.HttpResponse.BodyHandlers; +import java.util.Base64; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HttpClientBasicAuthentication { + + private static final Logger logger = LoggerFactory.getLogger(HttpClientBasicAuthentication.class); + + public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException { + useClientWithAuthenticator(); + useClientWithHeaders(); + } + + private static void useClientWithAuthenticator() throws URISyntaxException, IOException, InterruptedException { + HttpClient client = HttpClient.newBuilder() + .authenticator(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication("postman", "password".toCharArray()); + } + }) + .build(); + + HttpRequest request = HttpRequest.newBuilder() + .GET() + .uri(new URI("https://postman-echo.com/basic-auth")) + .build(); + + HttpResponse response = client.send(request, BodyHandlers.ofString()); + + logger.info("Status using authenticator {}", response.statusCode()); + } + + private static void useClientWithHeaders() throws IOException, InterruptedException, URISyntaxException { + HttpClient client = HttpClient.newBuilder() + .build(); + + HttpRequest request = HttpRequest.newBuilder() + .GET() + .uri(new URI("https://postman-echo.com/basic-auth")) + .header("Authorization", getBasicAuthenticationHeader("postman", "password")) + .build(); + + HttpResponse response = client.send(request, BodyHandlers.ofString()); + + logger.info("Status using headers: {}", response.statusCode()); + } + + private static final String getBasicAuthenticationHeader(String username, String password) { + String valueToEncode = username + ":" + password; + return "Basic " + Base64.getEncoder() + .encodeToString(valueToEncode.getBytes()); + } + +} From 51987ce1bcc6116d16789abfee87114fcb0c3741 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Mon, 18 Apr 2022 16:54:23 +0500 Subject: [PATCH 02/11] Updated README.md added link back to the article: https://www.baeldung.com/java-boolean-to-int --- java-numbers-4/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/java-numbers-4/README.md b/java-numbers-4/README.md index 95d46203ab..267639a829 100644 --- a/java-numbers-4/README.md +++ b/java-numbers-4/README.md @@ -6,3 +6,4 @@ - [Guide to Java BigInteger](https://www.baeldung.com/java-biginteger) - [Automorphic Numbers in Java](https://www.baeldung.com/java-automorphic-numbers) - [Convert Byte Size Into a Human-Readable Format in Java](https://www.baeldung.com/java-human-readable-byte-size) +- [Convert boolean to int in Java](https://www.baeldung.com/java-boolean-to-int) From 65e888046f6adffa7316494ec1f0cc392f3cd85c Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Mon, 18 Apr 2022 16:59:16 +0500 Subject: [PATCH 03/11] Updated README.md added link back to the article: https://www.baeldung.com/spring-cloud-disable-discovery-clients --- spring-cloud/spring-cloud-eureka/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-cloud/spring-cloud-eureka/README.md b/spring-cloud/spring-cloud-eureka/README.md index 5fc96256f4..0029ea60f6 100644 --- a/spring-cloud/spring-cloud-eureka/README.md +++ b/spring-cloud/spring-cloud-eureka/README.md @@ -1,3 +1,4 @@ ### Relevant Articles: - [Introduction to Spring Cloud Netflix – Eureka](http://www.baeldung.com/spring-cloud-netflix-eureka) - [Integration Tests With Spring Cloud Netflix and Feign](https://www.baeldung.com/spring-cloud-feign-integration-tests) +- [Spring Cloud – Disable Discovery Clients with Profiles](https://www.baeldung.com/spring-cloud-disable-discovery-clients) From 316e7b1e04a4d3704fd7303bf87a55818dcefe0b Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Mon, 18 Apr 2022 17:02:21 +0500 Subject: [PATCH 04/11] Updated README.md added link back to the article: https://www.baeldung.com/java-implements-vs-extends --- core-java-modules/core-java-lang-4/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-lang-4/README.md b/core-java-modules/core-java-lang-4/README.md index a145633d2b..befef0b6eb 100644 --- a/core-java-modules/core-java-lang-4/README.md +++ b/core-java-modules/core-java-lang-4/README.md @@ -11,3 +11,4 @@ This module contains articles about core features in the Java language - [Fixing the “Declared package does not match the expected package” Error](https://www.baeldung.com/java-declared-expected-package-error) - [Chaining Constructors in Java](https://www.baeldung.com/java-chain-constructors) - [Difference Between POJO, JavaBeans, DTO and VO](https://www.baeldung.com/java-pojo-javabeans-dto-vo) +- [Implements vs. Extends in Java](https://www.baeldung.com/java-implements-vs-extends) From 5be5a938feda5a3a492bd67d8556bf3a052c4eb5 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Mon, 18 Apr 2022 17:06:50 +0500 Subject: [PATCH 05/11] Updated README.md added link back to the article: https://www.baeldung.com/java-list-vs-arraylist --- core-java-modules/core-java-collections-list-3/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-collections-list-3/README.md b/core-java-modules/core-java-collections-list-3/README.md index 967e148a99..bcc8b3f3ed 100644 --- a/core-java-modules/core-java-collections-list-3/README.md +++ b/core-java-modules/core-java-collections-list-3/README.md @@ -11,4 +11,5 @@ This module contains articles about the Java List collection - [Filtering a Java Collection by a List](https://www.baeldung.com/java-filter-collection-by-list) - [How to Count Duplicate Elements in Arraylist](https://www.baeldung.com/java-count-duplicate-elements-arraylist) - [Finding the Differences Between Two Lists in Java](https://www.baeldung.com/java-lists-difference) +- [List vs. ArrayList in Java](https://www.baeldung.com/java-list-vs-arraylist) - [[<-- Prev]](/core-java-modules/core-java-collections-list-2) From f93ca672b4619274d94a301d593610eabe07cf7c Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Mon, 18 Apr 2022 17:09:18 +0500 Subject: [PATCH 06/11] Created/Updated README.md added link back to the article: https://www.baeldung.com/spring-boot-properties-migrator --- .../spring-boot-properties-migrator-demo/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 spring-boot-modules/spring-boot-properties-migrator-demo/README.md diff --git a/spring-boot-modules/spring-boot-properties-migrator-demo/README.md b/spring-boot-modules/spring-boot-properties-migrator-demo/README.md new file mode 100644 index 0000000000..c00eae2681 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-migrator-demo/README.md @@ -0,0 +1,3 @@ + +### Relevant Articles: +- [Spring Boot Configuration Properties Migrator](https://www.baeldung.com/spring-boot-properties-migrator) From c26826c7004ff2ac956d40ab58bbb617bb7b2822 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Mon, 18 Apr 2022 17:11:30 +0500 Subject: [PATCH 07/11] Updated README.md added link back to the article: https://www.baeldung.com/hibernate-not-null-error --- persistence-modules/hibernate-exceptions/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/persistence-modules/hibernate-exceptions/README.md b/persistence-modules/hibernate-exceptions/README.md index 2e5a98c2f8..c485de2f34 100644 --- a/persistence-modules/hibernate-exceptions/README.md +++ b/persistence-modules/hibernate-exceptions/README.md @@ -5,3 +5,4 @@ - [TransactionRequiredException Error](https://www.baeldung.com/jpa-transaction-required-exception) - [Hibernate’s “Object References an Unsaved Transient Instance” Error](https://www.baeldung.com/hibernate-unsaved-transient-instance-error) - [EntityNotFoundException in Hibernate](https://www.baeldung.com/hibernate-entitynotfoundexception) +- [Hibernate’s “Not-Null Property References a Null or Transient Value” Error](https://www.baeldung.com/hibernate-not-null-error) From bba9f4374b2b10d95be0c8768aa07ddbd5be646b Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Mon, 18 Apr 2022 17:16:03 +0500 Subject: [PATCH 08/11] Updated README.md added link back to the article: https://www.baeldung.com/jackson-deduction-based-polymorphism --- jackson-modules/jackson-annotations/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/jackson-modules/jackson-annotations/README.md b/jackson-modules/jackson-annotations/README.md index 783a06605b..3b6cd6f20b 100644 --- a/jackson-modules/jackson-annotations/README.md +++ b/jackson-modules/jackson-annotations/README.md @@ -7,3 +7,4 @@ This module contains articles about Jackson annotations. - [More Jackson Annotations](https://www.baeldung.com/jackson-advanced-annotations) - [Jackson – Bidirectional Relationships](https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion) - [Jackson JSON Views](https://www.baeldung.com/jackson-json-view-annotation) +- [Deduction-Based Polymorphism in Jackson 2.12](https://www.baeldung.com/jackson-deduction-based-polymorphism) From f4c4d890d6e22eaf66bffab3da1b46b9e4c4782f Mon Sep 17 00:00:00 2001 From: etrandafir93 <75391049+etrandafir93@users.noreply.github.com> Date: Tue, 19 Apr 2022 01:13:13 +0200 Subject: [PATCH 09/11] BAEL-5465: changed example to LocalDateTime (#12086) --- .../baeldung/subtractdays/SubtractDaysFromDateUnitTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/subtractdays/SubtractDaysFromDateUnitTest.java b/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/subtractdays/SubtractDaysFromDateUnitTest.java index 9afb6e8db0..91cd29cd54 100644 --- a/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/subtractdays/SubtractDaysFromDateUnitTest.java +++ b/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/subtractdays/SubtractDaysFromDateUnitTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals; import java.text.SimpleDateFormat; import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -15,7 +16,7 @@ public class SubtractDaysFromDateUnitTest { @Test public void givenLocalDateTime_whenSubtractingFiveDays_dateIsChangedCorrectly() { - LocalDate localDateTime = LocalDate.of(2022, 4, 20); + LocalDateTime localDateTime = LocalDateTime.of(2022, 4, 20, 0, 0); localDateTime = localDateTime.minusDays(5); From 09d6595514029345c2bdf839768d008a2ed92c13 Mon Sep 17 00:00:00 2001 From: Kapil Khandelwal Date: Tue, 19 Apr 2022 05:05:55 +0530 Subject: [PATCH 10/11] Bael 5369 new (#12073) * BAEL-5369: Checking Connection to MongoDB * BAEL-5369:- Minor Fix Checking Connection to MongoDB --- .../com/baeldung/mongo/ConnectionCheck.java | 8 ++--- .../mongo/ConnectionCheckLiveTest.java | 31 +++---------------- 2 files changed, 8 insertions(+), 31 deletions(-) diff --git a/persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/ConnectionCheck.java b/persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/ConnectionCheck.java index 240e54ab99..df17e27351 100644 --- a/persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/ConnectionCheck.java +++ b/persistence-modules/java-mongodb-2/src/main/java/com/baeldung/mongo/ConnectionCheck.java @@ -7,7 +7,7 @@ import com.mongodb.ServerAddress; public class ConnectionCheck { - public static void checkingConnection() { + public static MongoClient checkingConnection() { MongoClientOptions.Builder builder = MongoClientOptions.builder(); @@ -27,9 +27,10 @@ public class ConnectionCheck { System.out.println(db.getStats()); } catch (Exception e) { System.out.println("MongoDB Server is Down"); - mongoClient.close(); } + return mongoClient; + } public static void main(String[] args) { @@ -42,5 +43,4 @@ public class ConnectionCheck { } -} - +} \ No newline at end of file diff --git a/persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/ConnectionCheckLiveTest.java b/persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/ConnectionCheckLiveTest.java index 69f356997c..32014a4473 100644 --- a/persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/ConnectionCheckLiveTest.java +++ b/persistence-modules/java-mongodb-2/src/test/java/com/baeldung/mongo/ConnectionCheckLiveTest.java @@ -3,45 +3,22 @@ package com.baeldung.mongo; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import java.io.IOException; - -import org.junit.BeforeClass; import org.junit.Test; +import com.baeldung.ConnectionCheck; import com.mongodb.MongoClient; -import com.mongodb.MongoClientOptions; -import com.mongodb.ServerAddress; public class ConnectionCheckLiveTest { - private static MongoClient mongoClient; - private static MongoClientOptions.Builder builder; - private static ServerAddress ServerAddress; - - @BeforeClass - public static void setup() throws IOException { - if (mongoClient == null) { - - builder = MongoClientOptions.builder(); - builder.connectionsPerHost(100); - builder.maxWaitTime(60000); - builder.connectTimeout(1500); - builder.socketTimeout(60000); - builder.socketKeepAlive(true); - - ServerAddress = new ServerAddress("localhost", 27017); - mongoClient = new MongoClient(ServerAddress, builder.build()); - - } - } + ConnectionCheck ConnectionCheck = new ConnectionCheck(); @Test public void givenMongoClient_whenConnectionCheck_thenCheckingForConnectionPoint() { + MongoClient mongoClient = ConnectionCheck.checkingConnection(); String connectionPoint = mongoClient.getConnectPoint(); assertNotNull(connectionPoint); assertFalse(connectionPoint.isEmpty()); } -} - +} \ No newline at end of file From 66f829cd8d4ef029c6b5275cf9bd82dfcfd74c82 Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Mon, 18 Apr 2022 21:37:21 -0300 Subject: [PATCH 11/11] BAEL-5157 - Exception Handling With Jersey (#12082) * BAEL-5157 - Exception Handling with Jersey First draft: https://drafts.baeldung.com/wp-admin/post.php?post=131880&action=edit * BAEL-5157 * more meaningful exception messages --- .../jersey/exceptionhandling/rest/StocksResource.java | 2 +- .../rest/exceptions/ServerExceptionMapper.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/StocksResource.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/StocksResource.java index 94ce329ad0..64b645a1c6 100644 --- a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/StocksResource.java +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/StocksResource.java @@ -34,7 +34,7 @@ public class StocksResource { @Produces(MediaType.APPLICATION_JSON) public Response get(@PathParam("ticker") String id) { Optional stock = stocks.findById(id); - stock.orElseThrow(IllegalArgumentException::new); + stock.orElseThrow(() -> new IllegalArgumentException("ticker")); return Response.ok(stock.get()) .build(); diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/ServerExceptionMapper.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/ServerExceptionMapper.java index a6e9cc7f39..adfac000e8 100644 --- a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/ServerExceptionMapper.java +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/ServerExceptionMapper.java @@ -7,7 +7,7 @@ import javax.ws.rs.core.Response.Status; import javax.ws.rs.ext.ExceptionMapper; public class ServerExceptionMapper implements ExceptionMapper { - public static final String HTTP_405_MESSAGE = "METHOD_NOT_ALLOWED"; + public static final String HTTP_405_MESSAGE = "use one of"; @Override public Response toResponse(final WebApplicationException exception) { @@ -18,7 +18,7 @@ public class ServerExceptionMapper implements ExceptionMapper