Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
5890c64b55
|
@ -11,4 +11,5 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
|
|||
- [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings)
|
||||
- [Find the Longest Substring without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters)
|
||||
- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
|
||||
- [Find the Smallest Missing Integer in an Array](https://www.baeldung.com/java-smallest-missing-integer-in-array)
|
||||
- More articles: [[<-- prev]](/algorithms-miscellaneous-3) [[next -->]](/algorithms-miscellaneous-5)
|
||||
|
|
|
@ -9,4 +9,5 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
|
|||
- [Reversing a Binary Tree in Java](https://www.baeldung.com/java-reversing-a-binary-tree)
|
||||
- [Find If Two Numbers Are Relatively Prime in Java](https://www.baeldung.com/java-two-relatively-prime-numbers)
|
||||
- [Knapsack Problem Implementation in Java](https://www.baeldung.com/java-knapsack)
|
||||
- [How to Determine if a Binary Tree is Balanced](https://www.baeldung.com/java-balanced-binary-tree)
|
||||
- More articles: [[<-- prev]](/../algorithms-miscellaneous-4)
|
||||
|
|
|
@ -15,7 +15,7 @@ public class LocalDbCreationRule extends ExternalResource {
|
|||
@Override
|
||||
protected void before() throws Exception {
|
||||
String port = "8000";
|
||||
this.server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory", "-port", port});
|
||||
this.server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory","-sharedDb" ,"-port", port});
|
||||
server.start();
|
||||
}
|
||||
|
||||
|
|
|
@ -7,3 +7,4 @@ This module contains articles about automatic code generation
|
|||
- [Introduction to AutoValue](https://www.baeldung.com/introduction-to-autovalue)
|
||||
- [Introduction to AutoFactory](https://www.baeldung.com/autofactory)
|
||||
- [Google AutoService](https://www.baeldung.com/google-autoservice)
|
||||
- [Defensive Copies for Collections Using AutoValue](https://www.baeldung.com/autovalue-defensive-copies)
|
||||
|
|
|
@ -9,4 +9,5 @@ This module contains articles about the Java List collection
|
|||
- [List of Primitive Integer Values in Java](https://www.baeldung.com/java-list-primitive-int)
|
||||
- [Performance Comparison of Primitive Lists in Java](https://www.baeldung.com/java-list-primitive-performance)
|
||||
- [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)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-collections-list-2)
|
||||
|
|
|
@ -14,11 +14,18 @@
|
|||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-concurrency-advanced-3</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
|
@ -28,6 +35,8 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
package com.baeldung.commonissues;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class CollectionsConcurrencyIssues {
|
||||
|
||||
private void putIfAbsentList_NonAtomicOperation_ProneToConcurrencyIssues() {
|
||||
List<String> list = Collections.synchronizedList(new ArrayList<>());
|
||||
if (!list.contains("foo")) {
|
||||
list.add("foo");
|
||||
}
|
||||
}
|
||||
|
||||
private void putIfAbsentList_AtomicOperation_ThreadSafe() {
|
||||
List<String> list = Collections.synchronizedList(new ArrayList<>());
|
||||
synchronized (list) {
|
||||
if (!list.contains("foo")) {
|
||||
list.add("foo");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void putIfAbsentMap_NonAtomicOperation_ProneToConcurrencyIssues() {
|
||||
Map<String, String> map = new ConcurrentHashMap<>();
|
||||
if (!map.containsKey("foo")) {
|
||||
map.put("foo", "bar");
|
||||
}
|
||||
}
|
||||
|
||||
private void putIfAbsentMap_AtomicOperation_BetterApproach() {
|
||||
Map<String, String> map = new ConcurrentHashMap<>();
|
||||
synchronized (map) {
|
||||
if (!map.containsKey("foo")) {
|
||||
map.put("foo", "bar");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void putIfAbsentMap_AtomicOperation_BestApproach() {
|
||||
Map<String, String> map = new ConcurrentHashMap<>();
|
||||
map.putIfAbsent("foo", "bar");
|
||||
}
|
||||
|
||||
private void computeIfAbsentMap_AtomicOperation() {
|
||||
Map<String, String> map = new ConcurrentHashMap<>();
|
||||
map.computeIfAbsent("foo", key -> key + "bar");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.commonissues;
|
||||
|
||||
class Counter {
|
||||
private int counter = 0;
|
||||
|
||||
public void increment() {
|
||||
counter++;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return counter;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.commonissues;
|
||||
|
||||
public class DeadlockExample {
|
||||
|
||||
public static Object lock1 = new Object();
|
||||
public static Object lock2 = new Object();
|
||||
|
||||
public static void main(String args[]) {
|
||||
Thread threadA = new Thread(() -> {
|
||||
synchronized (lock1) {
|
||||
System.out.println("ThreadA: Holding lock 1...");
|
||||
sleep();
|
||||
System.out.println("ThreadA: Waiting for lock 2...");
|
||||
|
||||
synchronized (lock2) {
|
||||
System.out.println("ThreadA: Holding lock 1 & 2...");
|
||||
}
|
||||
}
|
||||
});
|
||||
Thread threadB = new Thread(() -> {
|
||||
synchronized (lock2) {
|
||||
System.out.println("ThreadB: Holding lock 2...");
|
||||
sleep();
|
||||
System.out.println("ThreadB: Waiting for lock 1...");
|
||||
|
||||
synchronized (lock1) {
|
||||
System.out.println("ThreadB: Holding lock 1 & 2...");
|
||||
}
|
||||
}
|
||||
});
|
||||
threadA.start();
|
||||
threadB.start();
|
||||
}
|
||||
|
||||
private static void sleep() {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.commonissues;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class SimpleDateFormatThreadUnsafetyExample {
|
||||
|
||||
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
|
||||
|
||||
public static void main(String[] args) {
|
||||
String dateStr = "2019-10-29T11:12:21";
|
||||
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(10);
|
||||
|
||||
for (int i = 0; i < 20; i++) {
|
||||
executorService.submit(() -> parseDate(dateStr));
|
||||
}
|
||||
|
||||
executorService.shutdown();
|
||||
}
|
||||
|
||||
private static void parseDate(String dateStr) {
|
||||
try {
|
||||
Date date = simpleDateFormat.parse(dateStr);
|
||||
System.out.println("Successfully Parsed Date " + date);
|
||||
} catch (ParseException e) {
|
||||
System.out.println("ParseError " + e.getMessage());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.commonissues;
|
||||
|
||||
class SynchronizedCounter {
|
||||
private int counter = 0;
|
||||
|
||||
public synchronized void increment() {
|
||||
counter++;
|
||||
}
|
||||
|
||||
public synchronized int getValue() {
|
||||
return counter;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.commonissues;
|
||||
|
||||
class SynchronizedVolatileCounter {
|
||||
private volatile int counter = 0;
|
||||
|
||||
public synchronized void increment() {
|
||||
counter++;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return counter;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
### Relevant Articles:
|
||||
- [Get the Current Date Prior to Java 8](https://www.baeldung.com/java-get-the-current-date-legacy)
|
||||
- [Skipping Weekends While Adding Days to LocalDate in Java 8](https://www.baeldung.com/java-localdate-add-days-skip-weekends)
|
|
@ -13,3 +13,4 @@ This module contains articles about the Date and Time API introduced with Java 8
|
|||
- [How to Get the Start and the End of a Day using Java](http://www.baeldung.com/java-day-start-end)
|
||||
- [Set the Time Zone of a Date in Java](https://www.baeldung.com/java-set-date-time-zone)
|
||||
- [Comparing Dates in Java](https://www.baeldung.com/java-comparing-dates)
|
||||
- [Generating Random Dates in Java](https://www.baeldung.com/java-random-dates)
|
||||
|
|
|
@ -17,3 +17,4 @@ This module contains articles about core java exceptions
|
|||
- [Common Java Exceptions](https://www.baeldung.com/java-common-exceptions)
|
||||
- [Throw Exception in Optional in Java 8](https://www.baeldung.com/java-optional-throw-exception)
|
||||
- [How to Find an Exception’s Root Cause in Java](https://www.baeldung.com/java-exception-root-cause)
|
||||
- [Is It a Bad Practice to Catch Throwable?](https://www.baeldung.com/java-catch-throwable-bad-practice)
|
||||
|
|
|
@ -5,4 +5,5 @@ This module contains articles about core features in the Java language
|
|||
### Relevant Articles:
|
||||
- [Java Primitives versus Objects](https://www.baeldung.com/java-primitives-vs-objects)
|
||||
- [Command-Line Arguments in Java](https://www.baeldung.com/java-command-line-arguments)
|
||||
- [What is a POJO Class?](https://www.baeldung.com/java-pojo-class)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-lang)
|
||||
|
|
|
@ -6,3 +6,4 @@
|
|||
- [Java 8 Math New Methods](https://www.baeldung.com/java-8-math)
|
||||
- [Java 8 Unsigned Arithmetic Support](https://www.baeldung.com/java-unsigned-arithmetic)
|
||||
- [How to Separate Double into Integer and Decimal Parts](https://www.baeldung.com/java-separate-double-into-integer-decimal-parts)
|
||||
- [The strictfp Keyword in Java](https://www.baeldung.com/java-strictfp)
|
||||
|
|
|
@ -8,4 +8,5 @@ This module contains articles about the Stream API in Java.
|
|||
- [The Difference Between Collection.stream().forEach() and Collection.forEach()](https://www.baeldung.com/java-collection-stream-foreach)
|
||||
- [Guide to Java 8’s Collectors](https://www.baeldung.com/java-8-collectors)
|
||||
- [Primitive Type Streams in Java 8](https://www.baeldung.com/java-8-primitive-streams)
|
||||
- More articles: [[<-- prev>]](/../core-java-streams-2)
|
||||
- [Debugging Java 8 Streams with IntelliJ](https://www.baeldung.com/intellij-debugging-java-streams)
|
||||
- More articles: [[<-- prev>]](/../core-java-streams-2)
|
||||
|
|
|
@ -58,7 +58,7 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<commons-lang3.version>3.8.1</commons-lang3.version>
|
||||
<commons-lang3.version>3.9</commons-lang3.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
</properties>
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package com.baeldung.isnumeric;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
|
@ -14,6 +17,8 @@ import org.openjdk.jmh.runner.options.Options;
|
|||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
public class Benchmarking {
|
||||
private static final TestMode MODE = TestMode.DIVERS;
|
||||
|
||||
public static void main(String[] args) throws RunnerException {
|
||||
Options opt = new OptionsBuilder().include(Benchmarking.class.getSimpleName())
|
||||
.forks(1)
|
||||
|
@ -22,52 +27,89 @@ public class Benchmarking {
|
|||
new Runner(opt).run();
|
||||
}
|
||||
|
||||
private static final IsNumeric subject = new IsNumeric();
|
||||
|
||||
@State(Scope.Thread)
|
||||
public static class ExecutionPlan {
|
||||
public String number = Integer.toString(Integer.MAX_VALUE);
|
||||
public boolean isNumber = false;
|
||||
public IsNumeric isNumeric = new IsNumeric();
|
||||
private final Map<String, Boolean> testPlan = testPlan();
|
||||
|
||||
void validate(Function<String, Boolean> functionUnderTest) {
|
||||
testPlan.forEach((key, value) -> {
|
||||
Boolean result = functionUnderTest.apply(key);
|
||||
|
||||
assertEquals(value, result, key);
|
||||
});
|
||||
}
|
||||
|
||||
private void assertEquals(Boolean expectedResult, Boolean result, String input) {
|
||||
if (result != expectedResult) {
|
||||
throw new IllegalStateException("The output does not match the expected output, for input: " + input);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Boolean> testPlan() {
|
||||
HashMap<String, Boolean> plan = new HashMap<>();
|
||||
plan.put(Integer.toString(Integer.MAX_VALUE), true);
|
||||
|
||||
if (MODE == TestMode.SIMPLE) {
|
||||
return plan;
|
||||
}
|
||||
|
||||
plan.put("x0", false);
|
||||
plan.put("0..005", false);
|
||||
plan.put("--11", false);
|
||||
plan.put("test", false);
|
||||
plan.put(null, false);
|
||||
for (int i = 0; i < 94; i++) {
|
||||
plan.put(Integer.toString(i), true);
|
||||
}
|
||||
return plan;
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingCoreJava(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingCoreJava(plan.number);
|
||||
plan.validate(subject::usingCoreJava);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingRegularExpressions(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingRegularExpressions(plan.number);
|
||||
plan.validate(subject::usingPreCompiledRegularExpressions);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingNumberUtils_isCreatable(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingNumberUtils_isCreatable(plan.number);
|
||||
plan.validate(subject::usingNumberUtils_isCreatable);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingNumberUtils_isParsable(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingNumberUtils_isParsable(plan.number);
|
||||
plan.validate(subject::usingNumberUtils_isParsable);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingStringUtils_isNumeric(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingStringUtils_isNumeric(plan.number);
|
||||
plan.validate(subject::usingStringUtils_isNumeric);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public void usingStringUtils_isNumericSpace(ExecutionPlan plan) {
|
||||
plan.isNumber = plan.isNumeric.usingStringUtils_isNumericSpace(plan.number);
|
||||
plan.validate(subject::usingStringUtils_isNumericSpace);
|
||||
}
|
||||
|
||||
private enum TestMode {
|
||||
SIMPLE, DIVERS
|
||||
}
|
||||
}
|
|
@ -1,20 +1,33 @@
|
|||
package com.baeldung.isnumeric;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
|
||||
public class IsNumeric {
|
||||
private final Pattern pattern = Pattern.compile("-?\\d+(\\.\\d+)?");
|
||||
|
||||
public boolean usingCoreJava(String strNum) {
|
||||
if (strNum == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
Double.parseDouble(strNum);
|
||||
} catch (NumberFormatException | NullPointerException nfe) {
|
||||
} catch (NumberFormatException nfe) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean usingRegularExpressions(String strNum) {
|
||||
return strNum.matches("-?\\d+(\\.\\d+)?");
|
||||
public boolean usingPreCompiledRegularExpressions(String strNum) {
|
||||
if (strNum == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return pattern.matcher(strNum)
|
||||
.matches();
|
||||
}
|
||||
|
||||
public boolean usingNumberUtils_isCreatable(String strNum) {
|
||||
|
|
|
@ -13,8 +13,8 @@ public class IsNumericDriver {
|
|||
boolean res = isNumeric.usingCoreJava("1001");
|
||||
LOG.info("Using Core Java : " + res);
|
||||
|
||||
res = isNumeric.usingRegularExpressions("1001");
|
||||
LOG.info("Using Regular Expressions : " + res);
|
||||
res = isNumeric.usingPreCompiledRegularExpressions("1001");
|
||||
LOG.info("Using Pre-compiled Regular Expressions : " + res);
|
||||
|
||||
res = isNumeric.usingNumberUtils_isCreatable("1001");
|
||||
LOG.info("Using NumberUtils.isCreatable : " + res);
|
||||
|
|
|
@ -6,9 +6,13 @@ import org.junit.Test;
|
|||
|
||||
public class CoreJavaIsNumericUnitTest {
|
||||
public static boolean isNumeric(String strNum) {
|
||||
if (strNum == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
double d = Double.parseDouble(strNum);
|
||||
} catch (NumberFormatException | NullPointerException nfe) {
|
||||
Double.parseDouble(strNum);
|
||||
} catch (NumberFormatException nfe) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
|
|
@ -2,11 +2,19 @@ package com.baeldung.isnumeric;
|
|||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RegularExpressionsUnitTest {
|
||||
public static boolean isNumeric(String strNum) {
|
||||
return strNum.matches("-?\\d+(\\.\\d+)?");
|
||||
private final Pattern pattern = Pattern.compile("-?\\d+(\\.\\d+)?");
|
||||
|
||||
public boolean isNumeric(String strNum) {
|
||||
if (strNum == null) {
|
||||
return false;
|
||||
}
|
||||
return pattern.matcher(strNum)
|
||||
.matches();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -17,6 +25,7 @@ public class RegularExpressionsUnitTest {
|
|||
assertThat(isNumeric("-200")).isTrue();
|
||||
|
||||
// Invalid Numbers
|
||||
assertThat(isNumeric(null)).isFalse();
|
||||
assertThat(isNumeric("abc")).isFalse();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,14 +14,6 @@
|
|||
|
||||
<dependencies>
|
||||
|
||||
<!-- marshalling -->
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- YAML -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
## Jackson Annotations
|
||||
|
||||
This module contains articles about Jackson annotations.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Guide to @JsonFormat in Jackson](https://www.baeldung.com/jackson-jsonformat)
|
||||
- [More Jackson Annotations](https://www.baeldung.com/jackson-advanced-annotations)
|
||||
- [Jackson Annotation Examples](https://www.baeldung.com/jackson-annotations)
|
||||
- [Jackson – Bidirectional Relationships](https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion)
|
||||
- [Jackson – Change Name of Field](https://www.baeldung.com/jackson-name-of-property)
|
||||
- [Jackson Ignore Properties on Marshalling](https://www.baeldung.com/jackson-ignore-properties-on-serialization)
|
|
@ -0,0 +1,60 @@
|
|||
<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>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>jackson-annotations</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-xml</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.module</groupId>
|
||||
<artifactId>jackson-module-jsonSchema</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>json-path</artifactId>
|
||||
<version>${rest-assured.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>jackson-annotations</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<assertj.version>3.11.0</assertj.version>
|
||||
<rest-assured.version>3.1.1</rest-assured.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.bidirection;
|
||||
package com.baeldung.jackson.annotation.bidirection;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
||||
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.bidirection;
|
||||
package com.baeldung.jackson.annotation.bidirection;
|
||||
|
||||
public class ItemWithIgnore {
|
||||
public int id;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.bidirection;
|
||||
package com.baeldung.jackson.annotation.bidirection;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.bidirection;
|
||||
package com.baeldung.jackson.annotation.bidirection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.bidirection;
|
||||
package com.baeldung.jackson.annotation.bidirection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.bidirection;
|
||||
package com.baeldung.jackson.annotation.bidirection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.date;
|
||||
package com.baeldung.jackson.annotation.date;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.date;
|
||||
package com.baeldung.jackson.annotation.date;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.date;
|
||||
package com.baeldung.jackson.annotation.date;
|
||||
|
||||
import java.util.Date;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.date;
|
||||
package com.baeldung.jackson.annotation.date;
|
||||
|
||||
import java.util.Date;
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.jackson.annotation.deserialization;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.annotation.dtos.ItemWithSerializer;
|
||||
import com.baeldung.jackson.annotation.dtos.User;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
import com.fasterxml.jackson.databind.node.IntNode;
|
||||
|
||||
public class ItemDeserializerOnClass extends StdDeserializer<ItemWithSerializer> {
|
||||
|
||||
private static final long serialVersionUID = 5579141241817332594L;
|
||||
|
||||
public ItemDeserializerOnClass() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public ItemDeserializerOnClass(final Class<?> vc) {
|
||||
super(vc);
|
||||
}
|
||||
|
||||
/**
|
||||
* {"id":1,"itemNr":"theItem","owner":2}
|
||||
*/
|
||||
@Override
|
||||
public ItemWithSerializer deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
final JsonNode node = jp.getCodec()
|
||||
.readTree(jp);
|
||||
final int id = (Integer) ((IntNode) node.get("id")).numberValue();
|
||||
final String itemName = node.get("itemName")
|
||||
.asText();
|
||||
final int userId = (Integer) ((IntNode) node.get("owner")).numberValue();
|
||||
|
||||
return new ItemWithSerializer(id, itemName, new User(userId, null));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.jackson.annotation.dtos;
|
||||
|
||||
public class Item {
|
||||
public int id;
|
||||
public String itemName;
|
||||
public User owner;
|
||||
|
||||
public Item() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Item(final int id, final String itemName, final User owner) {
|
||||
this.id = id;
|
||||
this.itemName = itemName;
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getItemName() {
|
||||
return itemName;
|
||||
}
|
||||
|
||||
public User getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.jackson.annotation.dtos;
|
||||
|
||||
import com.baeldung.jackson.annotation.deserialization.ItemDeserializerOnClass;
|
||||
import com.baeldung.jackson.annotation.serialization.ItemSerializerOnClass;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
@JsonSerialize(using = ItemSerializerOnClass.class)
|
||||
@JsonDeserialize(using = ItemDeserializerOnClass.class)
|
||||
public class ItemWithSerializer {
|
||||
public final int id;
|
||||
public final String itemName;
|
||||
public final User owner;
|
||||
|
||||
public ItemWithSerializer(final int id, final String itemName, final User owner) {
|
||||
this.id = id;
|
||||
this.itemName = itemName;
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getItemName() {
|
||||
return itemName;
|
||||
}
|
||||
|
||||
public User getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.jackson.annotation.dtos;
|
||||
|
||||
public class User {
|
||||
public int id;
|
||||
public String name;
|
||||
|
||||
public User() {
|
||||
super();
|
||||
}
|
||||
|
||||
public User(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.jackson.annotation.dtos.withEnum;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
public enum DistanceEnumWithValue {
|
||||
KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001);
|
||||
|
||||
private String unit;
|
||||
private final double meters;
|
||||
|
||||
private DistanceEnumWithValue(String unit, double meters) {
|
||||
this.unit = unit;
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
@JsonValue
|
||||
public double getMeters() {
|
||||
return meters;
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.exception;
|
||||
package com.baeldung.jackson.annotation.exception;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.exception;
|
||||
package com.baeldung.jackson.annotation.exception;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.jackson.annotation.jsonview;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
|
||||
public class Item {
|
||||
@JsonView(Views.Public.class)
|
||||
public int id;
|
||||
|
||||
@JsonView(Views.Public.class)
|
||||
public String itemName;
|
||||
|
||||
@JsonView(Views.Internal.class)
|
||||
public String ownerName;
|
||||
|
||||
public Item() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Item(final int id, final String itemName, final String ownerName) {
|
||||
this.id = id;
|
||||
this.itemName = itemName;
|
||||
this.ownerName = ownerName;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getItemName() {
|
||||
return itemName;
|
||||
}
|
||||
|
||||
public String getOwnerName() {
|
||||
return ownerName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.jackson.annotation.jsonview;
|
||||
|
||||
public class Views {
|
||||
public static class Public {
|
||||
}
|
||||
|
||||
public static class Internal extends Public {
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.jackson.annotation.serialization;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.annotation.dtos.Item;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class ItemSerializer extends StdSerializer<Item> {
|
||||
|
||||
private static final long serialVersionUID = 6739170890621978901L;
|
||||
|
||||
public ItemSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public ItemSerializer(final Class<Item> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void serialize(final Item value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
|
||||
jgen.writeStartObject();
|
||||
jgen.writeNumberField("id", value.id);
|
||||
jgen.writeStringField("itemName", value.itemName);
|
||||
jgen.writeNumberField("owner", value.owner.id);
|
||||
jgen.writeEndObject();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.jackson.annotation.serialization;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.annotation.dtos.ItemWithSerializer;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class ItemSerializerOnClass extends StdSerializer<ItemWithSerializer> {
|
||||
|
||||
private static final long serialVersionUID = -1760959597313610409L;
|
||||
|
||||
public ItemSerializerOnClass() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public ItemSerializerOnClass(final Class<ItemWithSerializer> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void serialize(final ItemWithSerializer value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
|
||||
jgen.writeStartObject();
|
||||
jgen.writeNumberField("id", value.id);
|
||||
jgen.writeStringField("itemName", value.itemName);
|
||||
jgen.writeNumberField("owner", value.owner.id);
|
||||
jgen.writeEndObject();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
package com.baeldung.jackson.bidirection;
|
||||
|
||||
import com.baeldung.jackson.jsonview.Views;
|
||||
import com.baeldung.jackson.bidirection.jsonview.Views;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
|
|
@ -3,7 +3,7 @@ package com.baeldung.jackson.bidirection;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.jackson.jsonview.Views;
|
||||
import com.baeldung.jackson.bidirection.jsonview.Views;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.jackson.bidirection.jsonview;
|
||||
|
||||
public class Views {
|
||||
public static class Public {
|
||||
}
|
||||
|
||||
public static class Internal extends Public {
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.jackson.domain;
|
||||
|
||||
public class Person {
|
||||
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Person(String firstName, String lastName) {
|
||||
super();
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.annotation.extra;
|
||||
package com.baeldung.jackson.advancedannotations;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
|
@ -12,10 +12,10 @@ import java.io.IOException;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.jackson.annotation.extra.AppendBeans.BeanWithAppend;
|
||||
import com.baeldung.jackson.annotation.extra.AppendBeans.BeanWithoutAppend;
|
||||
import com.baeldung.jackson.annotation.extra.IdentityReferenceBeans.BeanWithIdentityReference;
|
||||
import com.baeldung.jackson.annotation.extra.IdentityReferenceBeans.BeanWithoutIdentityReference;
|
||||
import com.baeldung.jackson.advancedannotations.AppendBeans.BeanWithAppend;
|
||||
import com.baeldung.jackson.advancedannotations.AppendBeans.BeanWithoutAppend;
|
||||
import com.baeldung.jackson.advancedannotations.IdentityReferenceBeans.BeanWithIdentityReference;
|
||||
import com.baeldung.jackson.advancedannotations.IdentityReferenceBeans.BeanWithoutIdentityReference;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
|
||||
|
@ -23,7 +23,7 @@ import com.fasterxml.jackson.databind.ObjectWriter;
|
|||
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
|
||||
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;
|
||||
|
||||
public class ExtraAnnotationUnitTest {
|
||||
public class AdvancedAnnotationsUnitTest {
|
||||
@Test
|
||||
public void whenNotUsingJsonIdentityReferenceAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.annotation.extra;
|
||||
package com.baeldung.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonAppend;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.annotation.extra;
|
||||
package com.baeldung.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
||||
import com.fasterxml.jackson.annotation.JsonIdentityReference;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.annotation.extra;
|
||||
package com.baeldung.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.annotation.extra;
|
||||
package com.baeldung.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.annotation.extra;
|
||||
package com.baeldung.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.annotation.extra;
|
||||
package com.baeldung.jackson.advancedannotations;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeId;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.annotation.extra;
|
||||
package com.baeldung.jackson.advancedannotations;
|
||||
|
||||
import java.util.List;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.test;
|
||||
package com.baeldung.jackson.annotation;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
@ -14,32 +14,18 @@ import java.util.TimeZone;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jackson.annotation.AliasBean;
|
||||
import com.baeldung.jackson.annotation.BeanWithCreator;
|
||||
import com.baeldung.jackson.annotation.BeanWithCustomAnnotation;
|
||||
import com.baeldung.jackson.annotation.BeanWithFilter;
|
||||
import com.baeldung.jackson.annotation.BeanWithGetter;
|
||||
import com.baeldung.jackson.annotation.BeanWithIgnore;
|
||||
import com.baeldung.jackson.annotation.BeanWithInject;
|
||||
import com.baeldung.jackson.annotation.ExtendableBean;
|
||||
import com.baeldung.jackson.annotation.MyBean;
|
||||
import com.baeldung.jackson.annotation.PrivateBean;
|
||||
import com.baeldung.jackson.annotation.RawBean;
|
||||
import com.baeldung.jackson.annotation.UnwrappedUser;
|
||||
import com.baeldung.jackson.annotation.UserWithIgnoreType;
|
||||
import com.baeldung.jackson.annotation.Zoo;
|
||||
import com.baeldung.jackson.bidirection.ItemWithIdentity;
|
||||
import com.baeldung.jackson.bidirection.ItemWithRef;
|
||||
import com.baeldung.jackson.bidirection.UserWithIdentity;
|
||||
import com.baeldung.jackson.bidirection.UserWithRef;
|
||||
import com.baeldung.jackson.date.EventWithFormat;
|
||||
import com.baeldung.jackson.date.EventWithSerializer;
|
||||
import com.baeldung.jackson.dtos.MyMixInForIgnoreType;
|
||||
import com.baeldung.jackson.dtos.withEnum.DistanceEnumWithValue;
|
||||
import com.baeldung.jackson.exception.UserWithRoot;
|
||||
import com.baeldung.jackson.exception.UserWithRootNamespace;
|
||||
import com.baeldung.jackson.jsonview.Item;
|
||||
import com.baeldung.jackson.jsonview.Views;
|
||||
import com.baeldung.jackson.annotation.bidirection.ItemWithIdentity;
|
||||
import com.baeldung.jackson.annotation.bidirection.ItemWithRef;
|
||||
import com.baeldung.jackson.annotation.bidirection.UserWithIdentity;
|
||||
import com.baeldung.jackson.annotation.bidirection.UserWithRef;
|
||||
import com.baeldung.jackson.annotation.date.EventWithFormat;
|
||||
import com.baeldung.jackson.annotation.date.EventWithSerializer;
|
||||
import com.baeldung.jackson.ignore.dtos.MyMixInForIgnoreType;
|
||||
import com.baeldung.jackson.annotation.dtos.withEnum.DistanceEnumWithValue;
|
||||
import com.baeldung.jackson.annotation.exception.UserWithRoot;
|
||||
import com.baeldung.jackson.annotation.exception.UserWithRootNamespace;
|
||||
import com.baeldung.jackson.annotation.jsonview.Item;
|
||||
import com.baeldung.jackson.annotation.jsonview.Views;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.InjectableValues;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
|
@ -352,13 +338,13 @@ public class JacksonAnnotationUnitTest {
|
|||
// @Ignore("Jackson 2.7.1-1 seems to have changed the API regarding mixins")
|
||||
@Test
|
||||
public void whenSerializingUsingMixInAnnotation_thenCorrect() throws JsonProcessingException {
|
||||
final com.baeldung.jackson.dtos.Item item = new com.baeldung.jackson.dtos.Item(1, "book", null);
|
||||
final com.baeldung.jackson.annotation.dtos.Item item = new com.baeldung.jackson.annotation.dtos.Item(1, "book", null);
|
||||
|
||||
String result = new ObjectMapper().writeValueAsString(item);
|
||||
assertThat(result, containsString("owner"));
|
||||
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.addMixIn(com.baeldung.jackson.dtos.User.class, MyMixInForIgnoreType.class);
|
||||
mapper.addMixIn(com.baeldung.jackson.annotation.dtos.User.class, MyMixInForIgnoreType.class);
|
||||
|
||||
result = mapper.writeValueAsString(item);
|
||||
assertThat(result, not(containsString("owner")));
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.jackson.test;
|
||||
package com.baeldung.jackson.bidirection;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
@ -9,19 +9,7 @@ import java.io.IOException;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jackson.bidirection.Item;
|
||||
import com.baeldung.jackson.bidirection.ItemWithIdentity;
|
||||
import com.baeldung.jackson.bidirection.ItemWithIgnore;
|
||||
import com.baeldung.jackson.bidirection.ItemWithRef;
|
||||
import com.baeldung.jackson.bidirection.ItemWithSerializer;
|
||||
import com.baeldung.jackson.bidirection.ItemWithView;
|
||||
import com.baeldung.jackson.bidirection.User;
|
||||
import com.baeldung.jackson.bidirection.UserWithIdentity;
|
||||
import com.baeldung.jackson.bidirection.UserWithIgnore;
|
||||
import com.baeldung.jackson.bidirection.UserWithRef;
|
||||
import com.baeldung.jackson.bidirection.UserWithSerializer;
|
||||
import com.baeldung.jackson.bidirection.UserWithView;
|
||||
import com.baeldung.jackson.jsonview.Views;
|
||||
import com.baeldung.jackson.bidirection.jsonview.Views;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
@ -10,15 +10,15 @@ import java.util.Map;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jackson.dtos.MyDto;
|
||||
import com.baeldung.jackson.dtos.MyDtoIncludeNonDefault;
|
||||
import com.baeldung.jackson.dtos.MyDtoWithFilter;
|
||||
import com.baeldung.jackson.dtos.MyDtoWithSpecialField;
|
||||
import com.baeldung.jackson.dtos.MyMixInForIgnoreType;
|
||||
import com.baeldung.jackson.ignore.dtos.MyDto;
|
||||
import com.baeldung.jackson.ignore.dtos.MyDtoIncludeNonDefault;
|
||||
import com.baeldung.jackson.ignore.dtos.MyDtoWithFilter;
|
||||
import com.baeldung.jackson.ignore.dtos.MyDtoWithSpecialField;
|
||||
import com.baeldung.jackson.ignore.dtos.MyMixInForIgnoreType;
|
||||
import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreField;
|
||||
import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreFieldByName;
|
||||
import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreNull;
|
||||
import com.baeldung.jackson.serialization.MyDtoNullKeySerializer;
|
||||
import com.baeldung.jackson.ignore.dtos.MyDtoNullKeySerializer;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.jackson.ignore.dtos;
|
||||
|
||||
public class MyDto {
|
||||
|
||||
private String stringValue;
|
||||
private int intValue;
|
||||
private boolean booleanValue;
|
||||
|
||||
public MyDto() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MyDto(final String stringValue, final int intValue, final boolean booleanValue) {
|
||||
super();
|
||||
|
||||
this.stringValue = stringValue;
|
||||
this.intValue = intValue;
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public String getStringValue() {
|
||||
return stringValue;
|
||||
}
|
||||
|
||||
public void setStringValue(final String stringValue) {
|
||||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
public int getIntValue() {
|
||||
return intValue;
|
||||
}
|
||||
|
||||
public void setIntValue(final int intValue) {
|
||||
this.intValue = intValue;
|
||||
}
|
||||
|
||||
public boolean isBooleanValue() {
|
||||
return booleanValue;
|
||||
}
|
||||
|
||||
public void setBooleanValue(final boolean booleanValue) {
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.baeldung.jackson.ignore.dtos;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
|
||||
@JsonInclude(Include.NON_DEFAULT)
|
||||
public class MyDtoIncludeNonDefault {
|
||||
|
||||
private String stringValue;
|
||||
private int intValue;
|
||||
private boolean booleanValue;
|
||||
|
||||
public MyDtoIncludeNonDefault() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public String getStringValue() {
|
||||
return stringValue;
|
||||
}
|
||||
|
||||
public void setStringValue(final String stringValue) {
|
||||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
public int getIntValue() {
|
||||
return intValue;
|
||||
}
|
||||
|
||||
public void setIntValue(final int intValue) {
|
||||
this.intValue = intValue;
|
||||
}
|
||||
|
||||
public boolean isBooleanValue() {
|
||||
return booleanValue;
|
||||
}
|
||||
|
||||
public void setBooleanValue(final boolean booleanValue) {
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,27 +1,27 @@
|
|||
package com.baeldung.jackson.serialization;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class MyDtoNullKeySerializer extends StdSerializer<Object> {
|
||||
|
||||
private static final long serialVersionUID = -4478531309177369056L;
|
||||
|
||||
public MyDtoNullKeySerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public MyDtoNullKeySerializer(final Class<Object> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(final Object value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
|
||||
jgen.writeFieldName("");
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.jackson.ignore.dtos;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class MyDtoNullKeySerializer extends StdSerializer<Object> {
|
||||
|
||||
private static final long serialVersionUID = -4478531309177369056L;
|
||||
|
||||
public MyDtoNullKeySerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public MyDtoNullKeySerializer(final Class<Object> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(final Object value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
|
||||
jgen.writeFieldName("");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.jackson.ignore.dtos;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||
|
||||
@JsonFilter("myFilter")
|
||||
public class MyDtoWithFilter {
|
||||
|
||||
private String stringValue;
|
||||
private int intValue;
|
||||
private boolean booleanValue;
|
||||
|
||||
public MyDtoWithFilter() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MyDtoWithFilter(final String stringValue, final int intValue, final boolean booleanValue) {
|
||||
super();
|
||||
|
||||
this.stringValue = stringValue;
|
||||
this.intValue = intValue;
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public String getStringValue() {
|
||||
return stringValue;
|
||||
}
|
||||
|
||||
public void setStringValue(final String stringValue) {
|
||||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
public int getIntValue() {
|
||||
return intValue;
|
||||
}
|
||||
|
||||
public void setIntValue(final int intValue) {
|
||||
this.intValue = intValue;
|
||||
}
|
||||
|
||||
public boolean isBooleanValue() {
|
||||
return booleanValue;
|
||||
}
|
||||
|
||||
public void setBooleanValue(final boolean booleanValue) {
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.jackson.ignore.dtos;
|
||||
|
||||
public class MyDtoWithSpecialField {
|
||||
|
||||
private String[] stringValue;
|
||||
private int intValue;
|
||||
private boolean booleanValue;
|
||||
|
||||
public MyDtoWithSpecialField() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MyDtoWithSpecialField(final String[] stringValue, final int intValue, final boolean booleanValue) {
|
||||
super();
|
||||
|
||||
this.stringValue = stringValue;
|
||||
this.intValue = intValue;
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public String[] getStringValue() {
|
||||
return stringValue;
|
||||
}
|
||||
|
||||
public void setStringValue(final String[] stringValue) {
|
||||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
public int getIntValue() {
|
||||
return intValue;
|
||||
}
|
||||
|
||||
public void setIntValue(final int intValue) {
|
||||
this.intValue = intValue;
|
||||
}
|
||||
|
||||
public boolean isBooleanValue() {
|
||||
return booleanValue;
|
||||
}
|
||||
|
||||
public void setBooleanValue(final boolean booleanValue) {
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.jackson.ignore.dtos;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreType;
|
||||
|
||||
@JsonIgnoreType
|
||||
public class MyMixInForIgnoreType {
|
||||
//
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue