这是indexloc提供的服务,不要输入任何密码
Skip to content

[BAEL-7021] Add Sample Code #18699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 24, 2025
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
@@ -0,0 +1,7 @@
package com.baeldung.jackson.staticobjectmapper;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtils {
public static final ObjectMapper MAPPER = new ObjectMapper();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.baeldung.jackson.staticobjectmapper;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import org.junit.jupiter.api.Test;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Collections;
import java.util.Date;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

class ConflictingRequirementsUnitTest {

private static final ObjectMapper GLOBAL_MAPPER = new ObjectMapper();

@Test
void whenSwitchingDateFormatGlobally_thenEndpointsCollide() throws Exception {
SimpleDateFormat iso = new SimpleDateFormat("yyyy-MM-dd");
GLOBAL_MAPPER.setDateFormat(iso);

Map<String, Date> payload = Collections.singletonMap(
"dob",
Date.from(LocalDate.of(1990, 10, 5)
.atTime(12, 0)
.toInstant(ZoneOffset.UTC)));

String forA = GLOBAL_MAPPER.writeValueAsString(payload);
assertEquals("{\"dob\":\"1990-10-05\"}", forA);

SimpleDateFormat european = new SimpleDateFormat("dd/MM/yyyy");
GLOBAL_MAPPER.setDateFormat(european);

String forB = GLOBAL_MAPPER.writeValueAsString(payload);
assertEquals("{\"dob\":\"05/10/1990\"}", forB);

String nowBrokenForA = GLOBAL_MAPPER.writeValueAsString(payload);
assertNotEquals(forA, nowBrokenForA);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.baeldung.jackson.staticobjectmapper;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.Collections;
import java.util.Date;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

class HiddenCouplingUnitTest {

private static final ObjectMapper GLOBAL_MAPPER = new ObjectMapper();

@Test
@Order(1)
void givenCustomDateFormat_whenConfiguredFirst_thenPasses() throws Exception {
GLOBAL_MAPPER.setDateFormat(new SimpleDateFormat("dd-MM-yyyy"));
Map<String, Date> payload = Collections.singletonMap("date", Date.from(LocalDate.of(1998, 2, 9).atTime(12, 0).toInstant(ZoneOffset.UTC)));
String json = GLOBAL_MAPPER.writeValueAsString(payload);
assertEquals("{\"date\":\"09-02-1998\"}", json);
}

@Test
@Order(2)
void givenDefaultDateFormat_whenRunAfterMutation_thenFails() throws Exception {
Map<String, Date> payload = Collections.singletonMap("date", Date.from(LocalDate.of(1998, 2, 9).atTime(12, 0).toInstant(ZoneOffset.UTC)));
String json = GLOBAL_MAPPER.writeValueAsString(payload);
assertNotEquals("{\"date\":887025600000}", json);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.baeldung.jackson.staticobjectmapper;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import static java.util.Collections.singletonMap;
import static org.junit.jupiter.api.Assertions.assertEquals;


class ObjectMapperThreadSafetyUnitTest {

private ObjectMapper GLOBAL_MAPPER = new ObjectMapper();

/**
* two real threads, created once and reused for every repetition
*/
private static final ExecutorService POOL = Executors.newFixedThreadPool(2);

@Test
void whenRegisteringDateFormatGlobally_thenAffectsAllConsumers() throws Exception {
Map<String, Date> payload = singletonMap("today", Date.from(LocalDate.of(1998, 2, 9).atTime(12, 0).toInstant(ZoneOffset.UTC)));

String before = GLOBAL_MAPPER.writeValueAsString(payload);
assertEquals("{\"today\":887025600000}", before);

GLOBAL_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));

String after = GLOBAL_MAPPER.writeValueAsString(payload);
assertEquals("{\"today\":\"1998-02-09\"}", after);
}

@Test
void whenSimpleDateFormatChanges_thenConflictHappens() throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
GLOBAL_MAPPER.setDateFormat(format);

Callable<String> task = () -> GLOBAL_MAPPER.writeValueAsString(singletonMap("key", Date.from(LocalDate.of(1998, 2, 9).atTime(12, 0).toInstant(ZoneOffset.UTC))));
Callable<Void> mutator = () -> {
format.applyPattern("dd-MM-yyyy");
return null;
};

Future<String> taskResult1 = POOL.submit(task);
assertEquals("{\"key\":\"1998-02-09\"}", taskResult1.get());
POOL.submit(mutator).get();
Future<String> taskResult2 = POOL.submit(task);
assertEquals("{\"key\":\"09-02-1998\"}", taskResult2.get());
}

@Test
void whenUsingCopyScopedMapper_thenNoInterference() throws Exception {
ObjectMapper localCopy = GLOBAL_MAPPER.copy().enable(SerializationFeature.INDENT_OUTPUT);
assertEquals("{\n \"key\" : \"value\"\n}", localCopy.writeValueAsString(singletonMap("key", "value")));
assertEquals("{\"key\":\"value\"}", GLOBAL_MAPPER.writeValueAsString(singletonMap("key", "value")));
}

@BeforeEach
void setup() {
GLOBAL_MAPPER = new ObjectMapper();
}

@AfterAll
static void shutdownPool() {
POOL.shutdownNow();
}
}