Skip to content

Commit 6d93283

Browse files
committed
Hacking: use Jackson 2 with Jersey
1 parent bb8d716 commit 6d93283

File tree

23 files changed

+121
-181
lines changed

23 files changed

+121
-181
lines changed

module/spring-boot-jersey/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ dependencies {
4040
optional(project(":core:spring-boot-autoconfigure"))
4141
optional(project(":module:spring-boot-actuator-autoconfigure"))
4242
optional(project(":module:spring-boot-health"))
43-
optional(project(":module:spring-boot-jackson"))
4443
optional(project(":module:spring-boot-jackson2"))
4544
optional(project(":module:spring-boot-micrometer-metrics"))
4645
optional(project(":module:spring-boot-micrometer-observation"))
@@ -49,6 +48,9 @@ dependencies {
4948

5049
testFixturesApi(testFixtures(project(":module:spring-boot-actuator")))
5150
testFixturesImplementation(project(":module:spring-boot-tomcat"))
51+
testFixturesRuntimeOnly(project(":module:spring-boot-jackson2"))
52+
testFixturesRuntimeOnly("com.fasterxml.jackson.datatype:jackson-datatype-jdk8")
53+
testFixturesRuntimeOnly("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
5254

5355
testImplementation(project(":core:spring-boot-test"))
5456
testImplementation(project(":module:spring-boot-restclient"))

module/spring-boot-jersey/src/main/java/org/springframework/boot/jersey/autoconfigure/JerseyAutoConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@
6868
* @author Stephane Nicoll
6969
* @since 4.0.0
7070
*/
71-
@AutoConfiguration(afterName = { "org.springframework.boot.jackson.autoconfigure.JacksonAutoConfiguration",
72-
"org.springframework.boot.jackson2.autoconfigure.Jackson2AutoConfiguration" })
71+
@AutoConfiguration(afterName = { "org.springframework.boot.jackson2.autoconfigure.Jackson2AutoConfiguration" })
7372
@ConditionalOnClass({ SpringComponentProvider.class, ServletRegistration.class })
7473
@ConditionalOnBean(type = "org.glassfish.jersey.server.ResourceConfig")
7574
@ConditionalOnWebApplication(type = Type.SERVLET)

module/spring-boot-jersey/src/main/java/org/springframework/boot/jersey/autoconfigure/actuate/web/EndpointJacksonFeature.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

module/spring-boot-jersey/src/main/java/org/springframework/boot/jersey/autoconfigure/actuate/web/EndpointJacksonJsonProvider.java

Lines changed: 0 additions & 76 deletions
This file was deleted.

module/spring-boot-jersey/src/main/java/org/springframework/boot/jersey/autoconfigure/actuate/web/JerseyWebEndpointManagementContextConfiguration.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
import java.util.List;
2424
import java.util.Objects;
2525

26+
import com.fasterxml.jackson.databind.ObjectMapper;
2627
import jakarta.annotation.Priority;
2728
import jakarta.ws.rs.Priorities;
2829
import jakarta.ws.rs.ext.ContextResolver;
2930
import org.glassfish.jersey.server.ResourceConfig;
3031
import org.glassfish.jersey.server.model.Resource;
3132
import org.jspecify.annotations.Nullable;
32-
import tools.jackson.databind.json.JsonMapper;
3333

3434
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
3535
import org.springframework.boot.actuate.autoconfigure.endpoint.expose.EndpointExposure;
@@ -41,7 +41,7 @@
4141
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
4242
import org.springframework.boot.actuate.endpoint.OperationResponseBody;
4343
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
44-
import org.springframework.boot.actuate.endpoint.jackson.EndpointJsonMapper;
44+
import org.springframework.boot.actuate.endpoint.jackson.EndpointJackson2ObjectMapper;
4545
import org.springframework.boot.actuate.endpoint.web.EndpointLinksResolver;
4646
import org.springframework.boot.actuate.endpoint.web.EndpointMapping;
4747
import org.springframework.boot.actuate.endpoint.web.EndpointMediaTypes;
@@ -109,12 +109,12 @@ JerseyAdditionalHealthEndpointPathsManagementResourcesRegistrar jerseyDifferentP
109109
}
110110

111111
@Bean
112-
@ConditionalOnBean(EndpointJsonMapper.class)
113-
ResourceConfigCustomizer endpointObjectMapperResourceConfigCustomizer(EndpointJsonMapper endpointJsonMapper) {
114-
return (config) -> {
115-
config.register(new EndpointJsonMapperContextResolver(endpointJsonMapper), ContextResolver.class);
116-
config.register(EndpointJacksonFeature.class);
117-
};
112+
@ConditionalOnBean(EndpointJackson2ObjectMapper.class)
113+
@SuppressWarnings("removal")
114+
ResourceConfigCustomizer endpointJackson2ObjectMapperResourceConfigCustomizer(
115+
org.springframework.boot.actuate.endpoint.jackson.EndpointJackson2ObjectMapper endpointJackson2ObjectMapper) {
116+
return (config) -> config.register(
117+
new EndpointJackson2ObjectMapperContextResolver(endpointJackson2ObjectMapper), ContextResolver.class);
118118
}
119119

120120
private boolean shouldRegisterLinksMapping(WebEndpointProperties properties, Environment environment,
@@ -218,20 +218,22 @@ private void register(Collection<Resource> resources, ResourceConfig config) {
218218
}
219219

220220
/**
221-
* {@link ContextResolver} used to obtain the {@link JsonMapper} that should be used
221+
* {@link ContextResolver} used to obtain the {@link ObjectMapper} that should be used
222222
* for {@link OperationResponseBody} instances.
223223
*/
224+
@SuppressWarnings("removal")
224225
@Priority(Priorities.USER - 100)
225-
private static final class EndpointJsonMapperContextResolver implements ContextResolver<JsonMapper> {
226+
private static final class EndpointJackson2ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
226227

227-
private final EndpointJsonMapper mapper;
228+
private final org.springframework.boot.actuate.endpoint.jackson.EndpointJackson2ObjectMapper mapper;
228229

229-
private EndpointJsonMapperContextResolver(EndpointJsonMapper mapper) {
230+
private EndpointJackson2ObjectMapperContextResolver(
231+
org.springframework.boot.actuate.endpoint.jackson.EndpointJackson2ObjectMapper mapper) {
230232
this.mapper = mapper;
231233
}
232234

233235
@Override
234-
public @Nullable JsonMapper getContext(Class<?> type) {
236+
public @Nullable ObjectMapper getContext(Class<?> type) {
235237
return OperationResponseBody.class.isAssignableFrom(type) ? this.mapper.get() : null;
236238
}
237239

module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/actuate/endpoint/web/JerseyEndpointIntegrationTests.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,27 @@
1616

1717
package org.springframework.boot.jersey.autoconfigure.actuate.endpoint.web;
1818

19+
import java.io.IOException;
1920
import java.nio.charset.StandardCharsets;
2021
import java.time.Duration;
2122
import java.util.ArrayList;
2223
import java.util.Arrays;
2324
import java.util.List;
2425

26+
import com.fasterxml.jackson.core.JsonGenerator;
27+
import com.fasterxml.jackson.databind.ObjectMapper;
28+
import com.fasterxml.jackson.databind.SerializerProvider;
29+
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
30+
import com.fasterxml.jackson.databind.module.SimpleModule;
31+
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
2532
import org.glassfish.jersey.server.ResourceConfig;
2633
import org.junit.jupiter.api.Test;
27-
import tools.jackson.core.JsonGenerator;
28-
import tools.jackson.databind.SerializationContext;
29-
import tools.jackson.databind.json.JsonMapper;
30-
import tools.jackson.databind.jsontype.TypeSerializer;
31-
import tools.jackson.databind.module.SimpleModule;
32-
import tools.jackson.databind.ser.std.StdScalarSerializer;
3334

3435
import org.springframework.boot.actuate.autoconfigure.beans.BeansEndpointAutoConfiguration;
3536
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
3637
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
3738
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration;
38-
import org.springframework.boot.actuate.endpoint.jackson.EndpointJsonMapper;
3939
import org.springframework.boot.autoconfigure.AutoConfigurations;
40-
import org.springframework.boot.jackson.autoconfigure.JacksonAutoConfiguration;
4140
import org.springframework.boot.jersey.autoconfigure.JerseyAutoConfiguration;
4241
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
4342
import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration;
@@ -133,11 +132,13 @@ WebApplicationContextRunner getContextRunner(Class<?>[] userConfigurations,
133132
.withPropertyValues("management.endpoints.web.exposure.include:*", "server.port:0");
134133
}
135134

135+
@SuppressWarnings("removal")
136136
private Class<?>[] getAutoconfigurations(Class<?>... additional) {
137-
List<Class<?>> autoconfigurations = new ArrayList<>(Arrays.asList(JacksonAutoConfiguration.class,
138-
JerseyAutoConfiguration.class, EndpointAutoConfiguration.class,
139-
TomcatServletWebServerAutoConfiguration.class, WebEndpointAutoConfiguration.class,
140-
ManagementContextAutoConfiguration.class, BeansEndpointAutoConfiguration.class));
137+
List<Class<?>> autoconfigurations = new ArrayList<>(
138+
Arrays.asList(org.springframework.boot.jackson2.autoconfigure.Jackson2AutoConfiguration.class,
139+
JerseyAutoConfiguration.class, EndpointAutoConfiguration.class,
140+
TomcatServletWebServerAutoConfiguration.class, WebEndpointAutoConfiguration.class,
141+
ManagementContextAutoConfiguration.class, BeansEndpointAutoConfiguration.class));
141142
autoconfigurations.addAll(Arrays.asList(additional));
142143
return autoconfigurations.toArray(new Class<?>[0]);
143144
}
@@ -184,11 +185,12 @@ ResourceConfig testResourceConfig() {
184185
static class EndpointObjectMapperConfiguration {
185186

186187
@Bean
187-
EndpointJsonMapper endpointJsonMapper() {
188+
org.springframework.boot.actuate.endpoint.jackson.EndpointJackson2ObjectMapper endpointJackson2ObjectMapper() {
188189
SimpleModule module = new SimpleModule();
189190
module.addSerializer(String.class, new ReverseStringSerializer());
190-
JsonMapper jsonMapper = JsonMapper.builder().addModule(module).build();
191-
return () -> jsonMapper;
191+
ObjectMapper objectMapper = new ObjectMapper();
192+
objectMapper.registerModule(module);
193+
return () -> objectMapper;
192194
}
193195

194196
static class ReverseStringSerializer extends StdScalarSerializer<Object> {
@@ -198,22 +200,22 @@ static class ReverseStringSerializer extends StdScalarSerializer<Object> {
198200
}
199201

200202
@Override
201-
public boolean isEmpty(SerializationContext context, Object value) {
203+
public boolean isEmpty(SerializerProvider provider, Object value) {
202204
return ((String) value).isEmpty();
203205
}
204206

205207
@Override
206-
public void serialize(Object value, JsonGenerator gen, SerializationContext context) {
208+
public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
207209
serialize(value, gen);
208210
}
209211

210212
@Override
211-
public final void serializeWithType(Object value, JsonGenerator gen, SerializationContext context,
212-
TypeSerializer typeSer) {
213+
public final void serializeWithType(Object value, JsonGenerator gen, SerializerProvider provider,
214+
TypeSerializer typeSer) throws IOException {
213215
serialize(value, gen);
214216
}
215217

216-
private void serialize(Object value, JsonGenerator gen) {
218+
private void serialize(Object value, JsonGenerator gen) throws IOException {
217219
StringBuilder builder = new StringBuilder((String) value);
218220
gen.writeString(builder.reverse().toString());
219221
}

module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/actuate/web/JerseyHealthEndpointAdditionalPathIntegrationTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.springframework.boot.health.autoconfigure.actuate.endpoint.HealthEndpointAutoConfiguration;
2525
import org.springframework.boot.health.autoconfigure.application.DiskSpaceHealthContributorAutoConfiguration;
2626
import org.springframework.boot.health.autoconfigure.registry.HealthContributorRegistryAutoConfiguration;
27-
import org.springframework.boot.jackson.autoconfigure.JacksonAutoConfiguration;
2827
import org.springframework.boot.jersey.autoconfigure.JerseyAutoConfiguration;
2928
import org.springframework.boot.jersey.autoconfigure.actuate.endpoint.web.HealthEndpointJerseyExtensionAutoConfiguration;
3029
import org.springframework.boot.servlet.autoconfigure.actuate.web.ServletManagementContextAutoConfiguration;
@@ -44,10 +43,13 @@
4443
class JerseyHealthEndpointAdditionalPathIntegrationTests extends
4544
AbstractHealthEndpointAdditionalPathIntegrationTests<WebApplicationContextRunner, ConfigurableWebApplicationContext, AssertableWebApplicationContext> {
4645

46+
@SuppressWarnings("removal")
4747
JerseyHealthEndpointAdditionalPathIntegrationTests() {
4848
super(new WebApplicationContextRunner(AnnotationConfigServletWebServerApplicationContext::new)
49-
.withConfiguration(AutoConfigurations.of(JacksonAutoConfiguration.class, JerseyAutoConfiguration.class,
50-
EndpointAutoConfiguration.class, TomcatServletWebServerAutoConfiguration.class,
49+
.withConfiguration(AutoConfigurations.of(
50+
org.springframework.boot.jackson2.autoconfigure.Jackson2AutoConfiguration.class,
51+
JerseyAutoConfiguration.class, EndpointAutoConfiguration.class,
52+
TomcatServletWebServerAutoConfiguration.class,
5153
TomcatServletManagementContextAutoConfiguration.class, WebEndpointAutoConfiguration.class,
5254
JerseyAutoConfiguration.class, ManagementContextAutoConfiguration.class,
5355
ServletManagementContextAutoConfiguration.class, HealthEndpointAutoConfiguration.class,

module/spring-boot-jersey/src/testFixtures/java/org/springframework/boot/jersey/actuate/endpoint/web/test/JerseyEndpointConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.springframework.boot.actuate.endpoint.web.EndpointMediaTypes;
3030
import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpointDiscoverer;
3131
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
32-
import org.springframework.boot.jackson.autoconfigure.JacksonAutoConfiguration;
3332
import org.springframework.boot.jersey.actuate.endpoint.web.JerseyEndpointResourceFactory;
3433
import org.springframework.boot.jersey.autoconfigure.JerseyAutoConfiguration;
3534
import org.springframework.boot.jersey.autoconfigure.ResourceConfigCustomizer;
@@ -44,8 +43,10 @@
4443
* @author Andy Wilkinson
4544
* @author Stephane Nicoll
4645
*/
46+
@SuppressWarnings("removal")
4747
@Configuration(proxyBeanMethods = false)
48-
@ImportAutoConfiguration({ JacksonAutoConfiguration.class, JerseyAutoConfiguration.class })
48+
@ImportAutoConfiguration({ org.springframework.boot.jackson2.autoconfigure.Jackson2AutoConfiguration.class,
49+
JerseyAutoConfiguration.class })
4950
class JerseyEndpointConfiguration {
5051

5152
private final ApplicationContext applicationContext;

platform/spring-boot-dependencies/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,6 +2181,7 @@ bom {
21812181
"spring-boot-starter-jdbc",
21822182
"spring-boot-starter-jdbc-test",
21832183
"spring-boot-starter-jersey",
2184+
"spring-boot-starter-jersey-test",
21842185
"spring-boot-starter-jetty",
21852186
"spring-boot-starter-jms",
21862187
"spring-boot-starter-jms-test",

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ include "starter:spring-boot-starter-jackson-test"
279279
include "starter:spring-boot-starter-jdbc"
280280
include "starter:spring-boot-starter-jdbc-test"
281281
include "starter:spring-boot-starter-jersey"
282+
include "starter:spring-boot-starter-jersey-test"
282283
include "starter:spring-boot-starter-jetty"
283284
include "starter:spring-boot-starter-jms"
284285
include "starter:spring-boot-starter-jms-test"

0 commit comments

Comments
 (0)