Add EndpointObjectMapper supported types method
Issue: 45876
This commit is contained in:
@@ -16,13 +16,15 @@
|
||||
|
||||
package org.springframework.boot.actuate.endpoint.jackson;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.OperationResponseBody;
|
||||
|
||||
/**
|
||||
* Interface used to supply the {@link ObjectMapper} that should be used when serializing
|
||||
* {@link OperationResponseBody} endpoint results.
|
||||
* endpoint results.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 3.0.0
|
||||
@@ -30,6 +32,11 @@ import org.springframework.boot.actuate.endpoint.OperationResponseBody;
|
||||
*/
|
||||
public interface EndpointObjectMapper {
|
||||
|
||||
/**
|
||||
* The default supported types.
|
||||
*/
|
||||
Set<Class<?>> DEFAULT_SUPPORTED_TYPES = Set.of(OperationResponseBody.class);
|
||||
|
||||
/**
|
||||
* Return the {@link ObjectMapper} that should be used to serialize
|
||||
* {@link OperationResponseBody} endpoint results.
|
||||
@@ -37,4 +44,13 @@ public interface EndpointObjectMapper {
|
||||
*/
|
||||
ObjectMapper get();
|
||||
|
||||
/**
|
||||
* Return the types that this endpoint mapper supports.
|
||||
* @return the supported types
|
||||
* @since 4.0.0
|
||||
*/
|
||||
default Set<Class<?>> getSupportedTypes() {
|
||||
return DEFAULT_SUPPORTED_TYPES;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -229,7 +229,12 @@ class JerseyWebEndpointManagementContextConfiguration {
|
||||
|
||||
@Override
|
||||
public ObjectMapper getContext(Class<?> type) {
|
||||
return OperationResponseBody.class.isAssignableFrom(type) ? this.endpointObjectMapper.get() : null;
|
||||
for (Class<?> supportedType : this.endpointObjectMapper.getSupportedTypes()) {
|
||||
if (supportedType.isAssignableFrom(type)) {
|
||||
return this.endpointObjectMapper.get();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -66,6 +67,7 @@ import org.springframework.http.codec.EncoderHttpMessageWriter;
|
||||
import org.springframework.http.codec.HttpMessageWriter;
|
||||
import org.springframework.http.codec.ServerCodecConfigurer;
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.function.SingletonSupplier;
|
||||
import org.springframework.web.reactive.DispatcherHandler;
|
||||
@@ -183,13 +185,18 @@ public class WebFluxEndpointManagementContextConfiguration {
|
||||
@SuppressWarnings({ "removal", "deprecation" })
|
||||
private void process(Encoder<?> encoder) {
|
||||
if (encoder instanceof org.springframework.http.codec.json.Jackson2JsonEncoder jackson2JsonEncoder) {
|
||||
jackson2JsonEncoder.registerObjectMappersForType(OperationResponseBody.class, (associations) -> {
|
||||
ObjectMapper objectMapper = this.endpointObjectMapper.get().get();
|
||||
MEDIA_TYPES.forEach((mimeType) -> associations.put(mimeType, objectMapper));
|
||||
});
|
||||
this.endpointObjectMapper.get()
|
||||
.getSupportedTypes()
|
||||
.forEach((type) -> jackson2JsonEncoder.registerObjectMappersForType(type,
|
||||
this::registerForAllMimeTypes));
|
||||
}
|
||||
}
|
||||
|
||||
private void registerForAllMimeTypes(Map<MimeType, ObjectMapper> registrar) {
|
||||
ObjectMapper objectMapper = this.endpointObjectMapper.get().get();
|
||||
MEDIA_TYPES.forEach((mimeType) -> registrar.put(mimeType, objectMapper));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@@ -185,10 +186,13 @@ public class WebMvcEndpointManagementContextConfiguration {
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
private void configure(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter converter) {
|
||||
converter.registerObjectMappersForType(OperationResponseBody.class, (associations) -> {
|
||||
ObjectMapper objectMapper = this.endpointObjectMapper.get();
|
||||
MEDIA_TYPES.forEach((mimeType) -> associations.put(mimeType, objectMapper));
|
||||
});
|
||||
this.endpointObjectMapper.getSupportedTypes()
|
||||
.forEach((type) -> converter.registerObjectMappersForType(type, this::registerForAllMimeTypes));
|
||||
}
|
||||
|
||||
private void registerForAllMimeTypes(Map<MediaType, ObjectMapper> registrar) {
|
||||
ObjectMapper objectMapper = this.endpointObjectMapper.get();
|
||||
MEDIA_TYPES.forEach((mimeType) -> registrar.put(mimeType, objectMapper));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user