Skip to content

Commit 4d9e2d1

Browse files
committed
Add EndpointObjectMapper supported types method
Issue: 45876
1 parent 55af0bd commit 4d9e2d1

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jackson/EndpointObjectMapper.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,41 @@
1616

1717
package org.springframework.boot.actuate.endpoint.jackson;
1818

19+
import java.util.Set;
20+
1921
import com.fasterxml.jackson.databind.ObjectMapper;
2022

2123
import org.springframework.boot.actuate.endpoint.OperationResponseBody;
2224

2325
/**
2426
* Interface used to supply the {@link ObjectMapper} that should be used when serializing
25-
* {@link OperationResponseBody} endpoint results.
27+
* endpoint results.
2628
*
2729
* @author Phillip Webb
2830
* @since 3.0.0
2931
* @see OperationResponseBody
3032
*/
3133
public interface EndpointObjectMapper {
3234

35+
/**
36+
* The default supported types.
37+
*/
38+
Set<Class<?>> DEFAULT_SUPPORTED_TYPES = Set.of(OperationResponseBody.class);
39+
3340
/**
3441
* Return the {@link ObjectMapper} that should be used to serialize
3542
* {@link OperationResponseBody} endpoint results.
3643
* @return the object mapper
3744
*/
3845
ObjectMapper get();
3946

47+
/**
48+
* Return the types that this endpoint mapper supports.
49+
* @return the supported types
50+
* @since 4.0.0
51+
*/
52+
default Set<Class<?>> getSupportedTypes() {
53+
return DEFAULT_SUPPORTED_TYPES;
54+
}
55+
4056
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,12 @@ private EndpointObjectMapperContextResolver(EndpointObjectMapper endpointObjectM
229229

230230
@Override
231231
public ObjectMapper getContext(Class<?> type) {
232-
return OperationResponseBody.class.isAssignableFrom(type) ? this.endpointObjectMapper.get() : null;
232+
for (Class<?> supportedType : this.endpointObjectMapper.getSupportedTypes()) {
233+
if (supportedType.isAssignableFrom(type)) {
234+
return this.endpointObjectMapper.get();
235+
}
236+
}
237+
return null;
233238
}
234239

235240
}

spring-boot-project/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/actuate/web/WebFluxEndpointManagementContextConfiguration.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collection;
2222
import java.util.Collections;
2323
import java.util.List;
24+
import java.util.Map;
2425
import java.util.function.Supplier;
2526

2627
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -66,6 +67,7 @@
6667
import org.springframework.http.codec.HttpMessageWriter;
6768
import org.springframework.http.codec.ServerCodecConfigurer;
6869
import org.springframework.http.server.reactive.HttpHandler;
70+
import org.springframework.util.MimeType;
6971
import org.springframework.util.StringUtils;
7072
import org.springframework.util.function.SingletonSupplier;
7173
import org.springframework.web.reactive.DispatcherHandler;
@@ -183,13 +185,18 @@ private void process(ServerCodecConfigurer configurer) {
183185
@SuppressWarnings({ "removal", "deprecation" })
184186
private void process(Encoder<?> encoder) {
185187
if (encoder instanceof org.springframework.http.codec.json.Jackson2JsonEncoder jackson2JsonEncoder) {
186-
jackson2JsonEncoder.registerObjectMappersForType(OperationResponseBody.class, (associations) -> {
187-
ObjectMapper objectMapper = this.endpointObjectMapper.get().get();
188-
MEDIA_TYPES.forEach((mimeType) -> associations.put(mimeType, objectMapper));
189-
});
188+
this.endpointObjectMapper.get()
189+
.getSupportedTypes()
190+
.forEach((type) -> jackson2JsonEncoder.registerObjectMappersForType(type,
191+
this::registerForAllMimeTypes));
190192
}
191193
}
192194

195+
private void registerForAllMimeTypes(Map<MimeType, ObjectMapper> registrar) {
196+
ObjectMapper objectMapper = this.endpointObjectMapper.get().get();
197+
MEDIA_TYPES.forEach((mimeType) -> registrar.put(mimeType, objectMapper));
198+
}
199+
193200
}
194201

195202
}

spring-boot-project/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/actuate/web/WebMvcEndpointManagementContextConfiguration.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collection;
2222
import java.util.Collections;
2323
import java.util.List;
24+
import java.util.Map;
2425

2526
import com.fasterxml.jackson.databind.ObjectMapper;
2627

@@ -185,10 +186,13 @@ public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
185186

186187
@SuppressWarnings("removal")
187188
private void configure(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter converter) {
188-
converter.registerObjectMappersForType(OperationResponseBody.class, (associations) -> {
189-
ObjectMapper objectMapper = this.endpointObjectMapper.get();
190-
MEDIA_TYPES.forEach((mimeType) -> associations.put(mimeType, objectMapper));
191-
});
189+
this.endpointObjectMapper.getSupportedTypes()
190+
.forEach((type) -> converter.registerObjectMappersForType(type, this::registerForAllMimeTypes));
191+
}
192+
193+
private void registerForAllMimeTypes(Map<MediaType, ObjectMapper> registrar) {
194+
ObjectMapper objectMapper = this.endpointObjectMapper.get();
195+
MEDIA_TYPES.forEach((mimeType) -> registrar.put(mimeType, objectMapper));
192196
}
193197

194198
}

0 commit comments

Comments
 (0)