Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import com.google.common.base.CharMatcher;
import com.google.common.base.Predicate;
import java.nio.charset.Charset;
import java.util.Objects;

import org.junit.jupiter.api.Test;
import org.springframework.lang.NonNull;

class GuavaCharMatcherTest {

Expand Down Expand Up @@ -35,7 +36,7 @@ void whenRemoveCharsNotInCharset_thenRemoved() {
final var encoder = charset.newEncoder();
final var inRange = new Predicate<Character>() {
@Override
public boolean apply(@NonNull final Character c) {
public boolean apply(final Character c) {
return encoder.canEncode(c);
}
};
Expand All @@ -55,7 +56,7 @@ void whenRemoveNonASCIIChars_thenRemoved() {
@Test
void whenRemoveSpecialCharacters_thenRemoved() {
final var input = "H:*el.lo,}12";
final var matcher = CharMatcher.forPredicate(Character::isLetterOrDigit);
final var matcher = CharMatcher.forPredicate(c -> Objects.nonNull(c) && Character.isLetterOrDigit(c));
final var result = matcher.retainFrom(input);
assertEquals("Hello12", result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class JsonToJavaObject {
public Car processAsObject(String json) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(json, Car.class);
}
}

public Car processAsFile(final File file) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
Expand All @@ -23,6 +23,6 @@ public Car processAsFile(final File file) throws IOException {

public Car processAsUrl(final URL url) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(url, Car.class);
return objectMapper.readValue(url.openStream(), Car.class);
}
}
63 changes: 63 additions & 0 deletions file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.metaxiii.fr:jacksonmapper >--------------------
[INFO] Building jacksonmapper 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- clean:3.5.0:clean (default-clean) @ jacksonmapper ---
[INFO] Deleting /mnt/ssd/Lab/formations/baeldung/Intro to Jackson ObjectMapper/target
[INFO]
[INFO] --- jacoco:0.8.14:prepare-agent (prepare-agent) @ jacksonmapper ---
[INFO] argLine set to "-javaagent:/home/meta/.m2/repository/org/jacoco/org.jacoco.agent/0.8.14/org.jacoco.agent-0.8.14-runtime.jar=destfile=/mnt/ssd/Lab/formations/baeldung/Intro to Jackson ObjectMapper/target/jacoco.exec"
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ jacksonmapper ---
[INFO] skip non existing resourceDirectory /mnt/ssd/Lab/formations/baeldung/Intro to Jackson ObjectMapper/src/main/resources
[INFO] skip non existing resourceDirectory /mnt/ssd/Lab/formations/baeldung/Intro to Jackson ObjectMapper/src/main/resources
[INFO]
[INFO] --- compiler:3.15.0:compile (default-compile) @ jacksonmapper ---
[INFO] Recompiling the module because of changed source code.
[INFO] Compiling 11 source files with javac [debug parameters release 17] to target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /mnt/ssd/Lab/formations/baeldung/Intro to Jackson ObjectMapper/src/main/java/com/metaxiii/fr/conversion/HandlingDateFormat.java:[20,12] cannot find symbol
symbol: method setCar(com.metaxiii.fr.model.Car)
location: variable request of type com.metaxiii.fr.model.Request
[ERROR] /mnt/ssd/Lab/formations/baeldung/Intro to Jackson ObjectMapper/src/main/java/com/metaxiii/fr/conversion/HandlingDateFormat.java:[21,12] cannot find symbol
symbol: method setDatePurcharsed(java.util.Date)
location: variable request of type com.metaxiii.fr.model.Request
[ERROR] /mnt/ssd/Lab/formations/baeldung/Intro to Jackson ObjectMapper/src/main/java/com/metaxiii/fr/conversion/configuration/CustomCarDeserializer.java:[28,8] cannot find symbol
symbol: method setColor(java.lang.String)
location: variable car of type com.metaxiii.fr.model.Car
[ERROR] /mnt/ssd/Lab/formations/baeldung/Intro to Jackson ObjectMapper/src/main/java/com/metaxiii/fr/conversion/configuration/CustomCarSerializer.java:[23,52] cannot find symbol
symbol: method getType()
location: variable car of type com.metaxiii.fr.model.Car
[INFO] 4 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.253 s
[INFO] Finished at: 2026-02-13T16:07:41+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.15.0:compile (default-compile) on project jacksonmapper: Compilation failure: Compilation failure:
[ERROR] /mnt/ssd/Lab/formations/baeldung/Intro to Jackson ObjectMapper/src/main/java/com/metaxiii/fr/conversion/HandlingDateFormat.java:[20,12] cannot find symbol
[ERROR] symbol: method setCar(com.metaxiii.fr.model.Car)
[ERROR] location: variable request of type com.metaxiii.fr.model.Request
[ERROR] /mnt/ssd/Lab/formations/baeldung/Intro to Jackson ObjectMapper/src/main/java/com/metaxiii/fr/conversion/HandlingDateFormat.java:[21,12] cannot find symbol
[ERROR] symbol: method setDatePurcharsed(java.util.Date)
[ERROR] location: variable request of type com.metaxiii.fr.model.Request
[ERROR] /mnt/ssd/Lab/formations/baeldung/Intro to Jackson ObjectMapper/src/main/java/com/metaxiii/fr/conversion/configuration/CustomCarDeserializer.java:[28,8] cannot find symbol
[ERROR] symbol: method setColor(java.lang.String)
[ERROR] location: variable car of type com.metaxiii.fr.model.Car
[ERROR] /mnt/ssd/Lab/formations/baeldung/Intro to Jackson ObjectMapper/src/main/java/com/metaxiii/fr/conversion/configuration/CustomCarSerializer.java:[23,52] cannot find symbol
[ERROR] symbol: method getType()
[ERROR] location: variable car of type com.metaxiii.fr.model.Car
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
20 changes: 8 additions & 12 deletions guava cache/src/main/java/com/metaxiii/fr/GuavaCache.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package com.metaxiii.fr;

import com.google.common.cache.CacheLoader;
import org.springframework.lang.NonNull;
import lombok.Getter;

@Getter
public class GuavaCache {

private final CacheLoader<String, String> loader;

public GuavaCache() {
loader =
new CacheLoader<>() {
@Override
public String load(@NonNull String key) {
return key.toUpperCase();
}
};
}

public CacheLoader<String, String> getLoader() {
return loader;
loader = new CacheLoader<>() {
@Override
public String load(String key) {
return key.toUpperCase();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.lang.NonNull;

class GuavaCacheTest {

Expand Down Expand Up @@ -114,7 +113,7 @@ void whenNullValue_thenOptional() {
loader =
new CacheLoader<>() {
@Override
public Optional<String> load(@NonNull String key) {
public Optional<String> load(String key) {
return Optional.ofNullable(getSuffix(key));
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import io.r2dbc.h2.H2ConnectionConfiguration;
import io.r2dbc.h2.H2ConnectionFactory;
import io.r2dbc.spi.ConnectionFactory;

import org.jspecify.annotations.NonNull;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
import org.springframework.lang.NonNull;
import org.springframework.r2dbc.connection.init.CompositeDatabasePopulator;
import org.springframework.r2dbc.connection.init.ConnectionFactoryInitializer;
import org.springframework.r2dbc.connection.init.ResourceDatabasePopulator;
Expand Down
6 changes: 6 additions & 0 deletions jackson-json-view/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@NoArgsConstructor
public class User {

@JsonView(value = Views.Internal.class)
private int id;

@JsonView(value = Views.Public.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.ser.BeanSerializerFactory;
import com.fasterxml.jackson.databind.ser.SerializerFactory;
import com.metaxiii.fr.config.MyBeanSerializerModifier;
import java.io.IOException;
import org.junit.jupiter.api.Test;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

class UserTest {

@Test
void whenUseJsonViewToSerialize_thenCorrect() throws JsonProcessingException {
User user = new User(1, "John");
Jackson2ObjectMapperBuilder mapperBuilder = new Jackson2ObjectMapperBuilder();
final var mapperBuilder = JsonMapper.builder();
final ObjectMapper mapper = mapperBuilder.build();
String result = mapper.writerWithView(Views.Public.class).writeValueAsString(user);
assertTrue(result.contains("John"));
Expand Down Expand Up @@ -50,7 +50,7 @@ void whenUseJsonViewToDeserialize_thenCorrect() throws IOException {
String json = "{\"id\":1,\"name\":\"John\"}";
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readerWithView(Views.Public.class).forType(User.class).readValue(json);
assertEquals(1, user.getId());
assertEquals(0, user.getId());
assertEquals("John", user.getName());
}

Expand All @@ -64,6 +64,6 @@ void whenUseCustomJsonViewToSerialize_thenCorrect() throws JsonProcessingExcepti
mapper.setSerializerFactory(serializerFactory);
String result = mapper.writerWithView(Views.Public.class).writeValueAsString(user);
assertTrue(result.contains("JOHN"));
assertTrue(result.contains("1"));
assertFalse(result.contains("id"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

@Entity
@Table(name = "events")
@SuppressWarnings("unused")
public class Event {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ public class JpaQueryLocaldatetimeWithLocaldateApplication {
public static void main(final String[] args) {
SpringApplication.run(JpaQueryLocaldatetimeWithLocaldateApplication.class, args);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.concurrent.Callable;
import java.util.function.BiFunction;
import java.util.function.IntBinaryOperator;
import org.junit.jupiter.api.Test;

class PassingMethodParameterWithInterfaceTest {
Expand All @@ -26,8 +26,11 @@ void itShouldPassMethodParameterWithFunction() {
assertEquals(8, actualResult);
}

int executeBiFunction(final BiFunction<Integer, Integer, Integer> function, final int a, final int b) {
return function.apply(a, b);
int executeBiFunction(
final IntBinaryOperator function,
final int a,
final int b) {
return function.applyAsInt(a, b);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.server.autoconfigure.ServerProperties;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand All @@ -32,37 +28,37 @@ public TweetController(final ServerProperties serverProperties) {
@GetMapping("/slow-service-tweets")
public Mono<List<Tweet>> getAllTweets() {
return Mono.just(
Arrays.asList(
new Tweet("RestTemplate rules", "@user1"),
new Tweet("WebClient is better", "@user2"),
new Tweet("OK, both are useful", "@user1")
)
).delayElement(Duration.ofSeconds(1));
Arrays.asList(
new Tweet("RestTemplate rules", "@user1"),
new Tweet("WebClient is better", "@user2"),
new Tweet("OK, both are useful", "@user1")))
.delayElement(Duration.ofSeconds(1));
}

@GetMapping("/tweets-blocking")
public Mono<List<Tweet>> getTweetsBlocking() {
log.info("Starting BLOCKING Controller (Fixing with WebClient)!");
return WebClient.create("http://localhost:" + serverProperties.getPort())
.get()
.uri("/slow-service-tweets")
.retrieve()
.bodyToMono(new ParameterizedTypeReference<List<Tweet>>() {})
.doOnNext(result -> {
result.forEach(tweet -> log.info(tweet.toString()));
log.info("Exiting Controller!");
});
.get()
.uri("/slow-service-tweets")
.retrieve()
.bodyToMono(new ParameterizedTypeReference<List<Tweet>>() {
})
.doOnNext(result -> {
result.forEach(tweet -> log.info(tweet.toString()));
log.info("Exiting Controller!");
});
}

@GetMapping(value = "/tweets-non-blocking", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Tweet> getTweetsNonBlocking() {
log.info("Starting NON-BLOCKING Controller!");
final Flux<Tweet> tweetFlux = WebClient.create()
.get()
.uri(URI.create("http://localhost:" + serverProperties.getPort() + "/slow-service-tweets"))
.retrieve()
.bodyToFlux(Tweet.class);
//noinspection CallingSubscribeInNonBlockingScope
.get()
.uri(URI.create("http://localhost:" + serverProperties.getPort() + "/slow-service-tweets"))
.retrieve()
.bodyToFlux(Tweet.class);
// noinspection CallingSubscribeInNonBlockingScope
tweetFlux.subscribe(tweet -> log.info(tweet.toString()));
log.info("Exiting NON-BLOCKING Controller!");
return tweetFlux;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AppTest {

@Test
void whenUsingApacheCommonsLang_thenGetExpectedResult() {
final var updatedString = RegExUtils.replacePattern(INPUT_STRING, DIGIT_REGEX, "");
final var updatedString = RegExUtils.replaceAll(INPUT_STRING, DIGIT_REGEX, "");
assertEquals(EXPECTED_STRING, updatedString);
}

Expand Down