Skip to content

Commit 555321c

Browse files
mp911deodrotbohm
authored andcommitted
GH-2483 - Switch to Jackson 3.
1 parent 523a231 commit 555321c

File tree

38 files changed

+299
-349
lines changed

38 files changed

+299
-349
lines changed

pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@
113113

114114
<dependencies>
115115

116+
<dependency>
117+
<groupId>org.springframework.data</groupId>
118+
<artifactId>spring-data-commons</artifactId>
119+
<version>${springdata.commons}</version>
120+
</dependency>
121+
116122
<dependency>
117123
<groupId>jakarta.validation</groupId>
118124
<artifactId>jakarta.validation-api</artifactId>
@@ -146,6 +152,16 @@
146152
<enabled>false</enabled>
147153
</releases>
148154
</repository>
155+
<repository>
156+
<id>central-snapshot</id>
157+
<url>https://central.sonatype.com/repository/maven-snapshots/</url>
158+
<snapshots>
159+
<enabled>true</enabled>
160+
</snapshots>
161+
<releases>
162+
<enabled>false</enabled>
163+
</releases>
164+
</repository>
149165
<repository>
150166
<id>spring-milestone</id>
151167
<url>https://repo.spring.io/milestone</url>

spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/AbstractWebIntegrationTests.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static org.assertj.core.api.Assertions.*;
1919
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
2020

21-
import io.micrometer.observation.ObservationRegistry;
2221
import jakarta.servlet.Filter;
2322
import jakarta.servlet.FilterChain;
2423
import jakarta.servlet.ServletException;
@@ -35,7 +34,6 @@
3534

3635
import org.junit.jupiter.api.BeforeEach;
3736
import org.junit.jupiter.api.extension.ExtendWith;
38-
3937
import org.springframework.beans.factory.annotation.Autowired;
4038
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
4139
import org.springframework.hateoas.Link;
@@ -132,17 +130,15 @@ protected MockHttpServletResponse putAndGet(Link link, Object payload, MediaType
132130
return StringUtils.hasText(response.getContentAsString()) ? response : client.request(link);
133131
}
134132

135-
protected MockHttpServletResponse putOnlyExpect5XXStatus(Link link, Object payload, MediaType mediaType)
133+
protected MockHttpServletResponse putOnlyExpect4XXStatus(Link link, Object payload, MediaType mediaType)
136134
throws Exception {
137135

138136
String href = link.isTemplated() ? link.expand().getHref() : link.getHref();
139137

140138
MvcTestResult result = mvc.perform(put(href).content(payload.toString()).contentType(mediaType));
141-
assertThat(result).hasStatus5xxServerError();
142-
143-
MockHttpServletResponse response = result.getResponse();
139+
assertThat(result).hasStatus4xxClientError();
144140

145-
return StringUtils.hasText(response.getContentAsString()) ? response : client.request(link);
141+
return result.getResponse();
146142
}
147143

148144
protected MockHttpServletResponse patchAndGet(Link link, Object payload, MediaType mediaType) throws Exception {
@@ -153,9 +149,7 @@ protected MockHttpServletResponse patchAndGet(Link link, Object payload, MediaTy
153149
content(payload.toString()).contentType(mediaType));
154150
assertThat(result).hasStatus2xxSuccessful();
155151

156-
MockHttpServletResponse response = result.getResponse();
157-
158-
return StringUtils.hasText(response.getContentAsString()) ? response : client.request(href);
152+
return result.getResponse();
159153
}
160154

161155
protected void deleteAndVerify(Link link) throws Exception {
@@ -165,8 +159,6 @@ protected void deleteAndVerify(Link link) throws Exception {
165159
MvcTestResult result = mvc.perform(delete(href));
166160
assertThat(result).hasStatus(HttpStatus.NO_CONTENT);
167161

168-
MockHttpServletResponse response = result.getResponse();
169-
170162
// Check that the resource is unavailable after a DELETE
171163
assertThat(mvc.perform(get(href))).hasStatus(HttpStatus.NOT_FOUND);//
172164
}

spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/RepositoryTestsConfig.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
import static org.mockito.Mockito.*;
1919

20+
import tools.jackson.databind.DeserializationFeature;
21+
import tools.jackson.databind.ObjectMapper;
22+
import tools.jackson.databind.json.JsonMapper;
23+
import tools.jackson.databind.module.SimpleModule;
24+
2025
import java.util.Collections;
2126
import java.util.List;
2227

@@ -30,6 +35,7 @@
3035
import org.springframework.data.repository.support.DefaultRepositoryInvokerFactory;
3136
import org.springframework.data.repository.support.DomainClassConverter;
3237
import org.springframework.data.repository.support.Repositories;
38+
import org.springframework.data.repository.support.RepositoryInvokerFactory;
3339
import org.springframework.data.rest.core.UriToEntityConverter;
3440
import org.springframework.data.rest.core.config.EnumTranslationConfiguration;
3541
import org.springframework.data.rest.core.config.MetadataConfiguration;
@@ -40,8 +46,8 @@
4046
import org.springframework.data.rest.core.support.EntityLookup;
4147
import org.springframework.data.rest.core.support.SelfLinkProvider;
4248
import org.springframework.data.rest.webmvc.EmbeddedResourcesAssembler;
43-
import org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module;
44-
import org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module.LookupObjectSerializer;
49+
import org.springframework.data.rest.webmvc.json.PersistentEntityJackson3Module;
50+
import org.springframework.data.rest.webmvc.json.PersistentEntityJackson3Module.LookupObjectSerializer;
4551
import org.springframework.data.rest.webmvc.mapping.Associations;
4652
import org.springframework.data.rest.webmvc.mapping.DefaultLinkCollector;
4753
import org.springframework.data.rest.webmvc.mapping.LinkCollector;
@@ -53,19 +59,15 @@
5359
import org.springframework.format.support.FormattingConversionService;
5460
import org.springframework.hateoas.mediatype.MessageResolver;
5561
import org.springframework.hateoas.mediatype.hal.DefaultCurieProvider;
56-
import org.springframework.hateoas.mediatype.hal.Jackson2HalModule;
62+
import org.springframework.hateoas.mediatype.hal.HalJacksonModule;
63+
import org.springframework.hateoas.mediatype.hal.HalJacksonModule.HalHandlerInstantiator;
5764
import org.springframework.hateoas.server.EntityLinks;
5865
import org.springframework.hateoas.server.LinkRelationProvider;
5966
import org.springframework.hateoas.server.RepresentationModelProcessor;
6067
import org.springframework.hateoas.server.core.EvoInflectorLinkRelationProvider;
6168
import org.springframework.hateoas.server.mvc.RepresentationModelProcessorInvoker;
6269
import org.springframework.plugin.core.PluginRegistry;
6370

64-
import com.fasterxml.jackson.annotation.JsonInclude.Include;
65-
import com.fasterxml.jackson.databind.DeserializationFeature;
66-
import com.fasterxml.jackson.databind.Module;
67-
import com.fasterxml.jackson.databind.ObjectMapper;
68-
6971
/**
7072
* @author Jon Brisbin
7173
* @author Greg Turnquist
@@ -107,7 +109,7 @@ public PersistentEntities persistentEntities() {
107109
}
108110

109111
@Bean
110-
public Module persistentEntityModule() {
112+
public SimpleModule persistentEntityModule() {
111113

112114
var conversionService = new DefaultConversionService();
113115

@@ -118,14 +120,14 @@ public Module persistentEntityModule() {
118120
SelfLinkProvider selfLinkProvider = new DefaultSelfLinkProvider(persistentEntities(), entityLinks,
119121
Collections.<EntityLookup<?>> emptyList(), conversionService);
120122

121-
DefaultRepositoryInvokerFactory invokerFactory = new DefaultRepositoryInvokerFactory(repositories());
123+
RepositoryInvokerFactory invokerFactory = new DefaultRepositoryInvokerFactory(repositories());
122124
UriToEntityConverter uriToEntityConverter = new UriToEntityConverter(persistentEntities(), invokerFactory,
123125
() -> conversionService);
124126

125127
Associations associations = new Associations(mappings, config());
126128
LinkCollector collector = new DefaultLinkCollector(persistentEntities(), selfLinkProvider, associations);
127129

128-
return new PersistentEntityJackson2Module(associations, persistentEntities(), uriToEntityConverter, collector,
130+
return new PersistentEntityJackson3Module(associations, persistentEntities(), uriToEntityConverter, collector,
129131
invokerFactory, mock(LookupObjectSerializer.class),
130132
new RepresentationModelProcessorInvoker(Collections.<RepresentationModelProcessor<?>> emptyList()),
131133
new EmbeddedResourcesAssembler(persistentEntities(), associations, mock(ExcerptProjector.class)));
@@ -135,15 +137,13 @@ invokerFactory, mock(LookupObjectSerializer.class),
135137
public ObjectMapper objectMapper() {
136138

137139
LinkRelationProvider relProvider = new EvoInflectorLinkRelationProvider();
138-
ObjectMapper mapper = new ObjectMapper();
139-
140-
mapper.registerModule(new Jackson2HalModule());
141-
mapper.registerModule(persistentEntityModule());
142-
mapper.setHandlerInstantiator(new Jackson2HalModule.HalHandlerInstantiator(relProvider,
143-
new DefaultCurieProvider(Collections.emptyMap()), MessageResolver.DEFAULTS_ONLY));
144-
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
145-
mapper.setSerializationInclusion(Include.NON_EMPTY);
146140

147-
return mapper;
141+
return JsonMapper.builder()
142+
.addModule(new HalJacksonModule())
143+
.addModule(persistentEntityModule())
144+
.handlerInstantiator(new HalHandlerInstantiator(relProvider,
145+
new DefaultCurieProvider(Collections.emptyMap()), MessageResolver.DEFAULTS_ONLY))
146+
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
147+
.build();
148148
}
149149
}

spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/DataRest262Tests.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import jakarta.persistence.Id;
2525
import jakarta.persistence.ManyToOne;
2626
import jakarta.validation.constraints.NotNull;
27+
import tools.jackson.databind.json.JsonMapper;
2728

2829
import org.junit.jupiter.api.BeforeEach;
2930
import org.junit.jupiter.api.Test;
@@ -42,7 +43,7 @@
4243
import org.springframework.hateoas.Link;
4344
import org.springframework.hateoas.MediaTypes;
4445
import org.springframework.http.HttpHeaders;
45-
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
46+
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
4647
import org.springframework.mock.web.MockHttpServletRequest;
4748
import org.springframework.test.context.ContextConfiguration;
4849
import org.springframework.test.context.junit.jupiter.SpringExtension;
@@ -51,8 +52,6 @@
5152
import org.springframework.web.context.request.ServletRequestAttributes;
5253

5354
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
54-
import com.fasterxml.jackson.annotation.PropertyAccessor;
55-
import com.fasterxml.jackson.databind.ObjectMapper;
5655
import com.jayway.jsonpath.JsonPath;
5756

5857
/**
@@ -67,25 +66,23 @@ class DataRest262Tests {
6766
@Configuration
6867
@Import({ RepositoryRestMvcConfiguration.class, JpaInfrastructureConfig.class })
6968
@EnableJpaRepositories(considerNestedRepositories = true)
70-
static class Config {
71-
72-
}
69+
static class Config {}
7370

7471
@Autowired ApplicationContext beanFactory;
7572
@Autowired JpaMetamodelMappingContext mappingContext;
7673
@Autowired AirportRepository repository;
7774

78-
ObjectMapper mapper;
75+
JsonMapper mapper;
7976

8077
@BeforeEach
8178
void setUp() {
8279

8380
this.mapper = beanFactory //
84-
.getBean("halJacksonHttpMessageConverter", AbstractJackson2HttpMessageConverter.class) //
85-
.getObjectMapper() //
86-
.copy();
87-
88-
this.mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
81+
.getBean("halJacksonHttpMessageConverter", JacksonJsonHttpMessageConverter.class) //
82+
.getMapper() //
83+
.rebuild()
84+
.changeDefaultVisibility(it -> it.withFieldVisibility(Visibility.ANY))
85+
.build();
8986
}
9087

9188
@Test // DATAREST-262

spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,9 @@ void associateTwoCreatorsToOrderWithSinglePut() throws Exception {
374374
Link secondCreatorLink = preparePersonResource(new Person("Pippin", "Baggins"));
375375
Link orderLinkToItsCreator = prepareOrderResource(new Order());
376376

377-
MockHttpServletResponse response = putOnlyExpect5XXStatus(orderLinkToItsCreator,
377+
MockHttpServletResponse response = putOnlyExpect4XXStatus(orderLinkToItsCreator,
378378
toUriList(firstCreatorLink, secondCreatorLink), TEXT_URI_LIST);
379-
assertThat(response.getContentAsString()).contains("send only 1 link");
379+
assertThat(response.getErrorMessage()).contains("send only 1 link");
380380
}
381381

382382
@Test // DATAREST-219
@@ -587,7 +587,7 @@ void returnConflictWhenConcurrentlyEditingVersionedEntity() throws Exception {
587587

588588
assertThat(mvc.perform(patch(builder.build().toUriString()).content("{ \"saleItem\" : \"SpringyTequila\" }")
589589
.contentType(MediaType.APPLICATION_JSON).header(IF_MATCH, "\"falseETag\"")))
590-
.hasStatus(HttpStatus.PRECONDITION_FAILED);
590+
.hasStatus(HttpStatus.PRECONDITION_FAILED);
591591
}
592592

593593
@Test // DATAREST-423

spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/json/Jackson2DatatypeHelperIntegrationTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.assertj.core.api.Assertions.*;
1919

2020
import jakarta.persistence.EntityManager;
21+
import tools.jackson.databind.json.JsonMapper;
2122

2223
import org.junit.jupiter.api.BeforeEach;
2324
import org.junit.jupiter.api.Test;
@@ -34,13 +35,11 @@
3435
import org.springframework.data.rest.webmvc.jpa.OrderRepository;
3536
import org.springframework.data.rest.webmvc.jpa.Person;
3637
import org.springframework.data.rest.webmvc.jpa.PersonRepository;
37-
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
38+
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
3839
import org.springframework.test.context.ContextConfiguration;
3940
import org.springframework.test.context.junit.jupiter.SpringExtension;
4041
import org.springframework.transaction.annotation.Transactional;
4142

42-
import com.fasterxml.jackson.databind.ObjectMapper;
43-
4443
/**
4544
* Integration tests for {@link Jackson2DatatypeHelper}.
4645
*
@@ -58,16 +57,16 @@ class Jackson2DatatypeHelperIntegrationTests {
5857
@Autowired OrderRepository orders;
5958
@Autowired EntityManager em;
6059

61-
ObjectMapper objectMapper;
60+
JsonMapper objectMapper;
6261

6362
Order order;
6463

6564
@BeforeEach
6665
void setUp() {
6766

6867
this.order = orders.save(new Order(people.save(new Person("Dave", "Matthews"))));
69-
this.objectMapper = context.getBean("halJacksonHttpMessageConverter", AbstractJackson2HttpMessageConverter.class)
70-
.getObjectMapper();
68+
this.objectMapper = context.getBean("halJacksonHttpMessageConverter", JacksonJsonHttpMessageConverter.class)
69+
.getMapper();
7170

7271
// Reset JPA to make sure the query returns a result with proxy references
7372
em.flush();

spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20+
import tools.jackson.databind.ObjectMapper;
21+
import tools.jackson.databind.json.JsonMapper;
22+
2023
import java.io.IOException;
2124
import java.io.StringWriter;
2225
import java.util.Arrays;
@@ -54,7 +57,6 @@
5457
import org.springframework.web.context.request.ServletWebRequest;
5558
import org.springframework.web.util.UriTemplate;
5659

57-
import com.fasterxml.jackson.databind.ObjectMapper;
5860
import com.jayway.jsonpath.JsonPath;
5961

6062
/**
@@ -83,11 +85,11 @@ static class TestConfig extends RepositoryTestsConfig {
8385

8486
@Bean
8587
@Override
86-
public ObjectMapper objectMapper() {
88+
public JsonMapper objectMapper() {
8789

88-
ObjectMapper objectMapper = super.objectMapper();
89-
objectMapper.registerModule(new JacksonSerializers(new EnumTranslator(MessageResolver.DEFAULTS_ONLY)));
90-
return objectMapper;
90+
return super.objectMapper().rebuild()
91+
.addModule(new Jackson3Serializers(new EnumTranslator(MessageResolver.DEFAULTS_ONLY)))
92+
.build();
9193
}
9294
}
9395

0 commit comments

Comments
 (0)