|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2021 the original author or authors. |
| 2 | + * Copyright 2012-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.boot.actuate.autoconfigure.endpoint.web.reactive;
|
18 | 18 |
|
19 | 19 | import java.util.ArrayList;
|
| 20 | +import java.util.Arrays; |
20 | 21 | import java.util.Collection;
|
| 22 | +import java.util.Collections; |
21 | 23 | import java.util.List;
|
22 | 24 |
|
| 25 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | + |
| 27 | +import org.springframework.beans.BeansException; |
| 28 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 29 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
23 | 30 | import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
|
24 | 31 | import org.springframework.boot.actuate.autoconfigure.endpoint.expose.EndpointExposure;
|
25 | 32 | import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
|
|
28 | 35 | import org.springframework.boot.actuate.autoconfigure.web.server.ConditionalOnManagementPort;
|
29 | 36 | import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
|
30 | 37 | import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
|
| 38 | +import org.springframework.boot.actuate.endpoint.OperationResponseBody; |
31 | 39 | import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
| 40 | +import org.springframework.boot.actuate.endpoint.jackson.EndpointObjectMapper; |
32 | 41 | import org.springframework.boot.actuate.endpoint.web.EndpointLinksResolver;
|
33 | 42 | import org.springframework.boot.actuate.endpoint.web.EndpointMapping;
|
34 | 43 | import org.springframework.boot.actuate.endpoint.web.EndpointMediaTypes;
|
|
48 | 57 | import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
|
49 | 58 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
50 | 59 | import org.springframework.context.annotation.Bean;
|
| 60 | +import org.springframework.context.annotation.Role; |
| 61 | +import org.springframework.core.codec.Encoder; |
51 | 62 | import org.springframework.core.env.Environment;
|
| 63 | +import org.springframework.http.MediaType; |
| 64 | +import org.springframework.http.codec.EncoderHttpMessageWriter; |
| 65 | +import org.springframework.http.codec.HttpMessageWriter; |
| 66 | +import org.springframework.http.codec.ServerCodecConfigurer; |
| 67 | +import org.springframework.http.codec.json.Jackson2JsonEncoder; |
52 | 68 | import org.springframework.http.server.reactive.HttpHandler;
|
53 | 69 | import org.springframework.util.StringUtils;
|
54 | 70 | import org.springframework.web.reactive.DispatcherHandler;
|
@@ -114,4 +130,55 @@ public ControllerEndpointHandlerMapping controllerEndpointHandlerMapping(
|
114 | 130 | corsProperties.toCorsConfiguration());
|
115 | 131 | }
|
116 | 132 |
|
| 133 | + @Bean |
| 134 | + @ConditionalOnBean(EndpointObjectMapper.class) |
| 135 | + @Role(BeanDefinition.ROLE_INFRASTRUCTURE) |
| 136 | + static ServerCodecConfigurerEndpointObjectMapperBeanPostProcessor serverCodecConfigurerEndpointObjectMapperBeanPostProcessor( |
| 137 | + EndpointObjectMapper endpointObjectMapper) { |
| 138 | + return new ServerCodecConfigurerEndpointObjectMapperBeanPostProcessor(endpointObjectMapper); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * {@link BeanPostProcessor} to apply {@link EndpointObjectMapper} for |
| 143 | + * {@link OperationResponseBody} to {@link Jackson2JsonEncoder} instances. |
| 144 | + */ |
| 145 | + private static class ServerCodecConfigurerEndpointObjectMapperBeanPostProcessor implements BeanPostProcessor { |
| 146 | + |
| 147 | + private static final List<MediaType> MEDIA_TYPES = Collections |
| 148 | + .unmodifiableList(Arrays.asList(MediaType.APPLICATION_JSON, new MediaType("application", "*+json"))); |
| 149 | + |
| 150 | + private final EndpointObjectMapper endpointObjectMapper; |
| 151 | + |
| 152 | + ServerCodecConfigurerEndpointObjectMapperBeanPostProcessor(EndpointObjectMapper endpointObjectMapper) { |
| 153 | + this.endpointObjectMapper = endpointObjectMapper; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| 158 | + if (bean instanceof ServerCodecConfigurer) { |
| 159 | + process((ServerCodecConfigurer) bean); |
| 160 | + } |
| 161 | + return bean; |
| 162 | + } |
| 163 | + |
| 164 | + private void process(ServerCodecConfigurer configurer) { |
| 165 | + for (HttpMessageWriter<?> writer : configurer.getWriters()) { |
| 166 | + if (writer instanceof EncoderHttpMessageWriter) { |
| 167 | + process(((EncoderHttpMessageWriter<?>) writer).getEncoder()); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + private void process(Encoder<?> encoder) { |
| 173 | + if (encoder instanceof Jackson2JsonEncoder) { |
| 174 | + Jackson2JsonEncoder jackson2JsonEncoder = (Jackson2JsonEncoder) encoder; |
| 175 | + jackson2JsonEncoder.registerObjectMappersForType(OperationResponseBody.class, (associations) -> { |
| 176 | + ObjectMapper objectMapper = this.endpointObjectMapper.get(); |
| 177 | + MEDIA_TYPES.forEach((mimeType) -> associations.put(mimeType, objectMapper)); |
| 178 | + }); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + } |
| 183 | + |
117 | 184 | }
|
0 commit comments