Use server customizer for Eureka Jackson mappings in mock server

Possible after Boot addressed https://github.com/spring-projects/spring-boot/issues/47798
This commit is contained in:
Ryan Baxter
2025-11-07 15:54:41 -05:00
parent 2122fa420c
commit e570ec275b
2 changed files with 13 additions and 18 deletions
@@ -27,6 +27,7 @@ import com.netflix.discovery.shared.Application;
import com.netflix.discovery.shared.Applications;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.http.converter.autoconfigure.ServerHttpMessageConvertersCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
@@ -34,7 +35,6 @@ import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.User;
@@ -111,15 +111,6 @@ public class EurekaServerMockApplication {
.setNamespace("namespace1")
.build();
/**
* Simulates Eureka Server own's serialization.
* @return converter
*/
@Bean
public MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter() {
return EurekaHttpClientUtils.mappingJacksonHttpMessageConverter();
}
@ResponseStatus(HttpStatus.OK)
@PostMapping("/apps/{appName}")
public void register(@PathVariable String appName, @RequestBody InstanceInfo instanceInfo) {
@@ -189,6 +180,17 @@ public class EurekaServerMockApplication {
return INFO;
}
/*
* Use this customizer to make sure we use the Jackson mappings for the Eureka server
*/
@Bean
@Order
ServerHttpMessageConvertersCustomizer customServerConvertersCustomizer() {
return builder -> {
builder.jsonMessageConverter(EurekaHttpClientUtils.mappingJacksonHttpMessageConverter());
};
}
@Configuration(proxyBeanMethods = false)
@Order(Ordered.HIGHEST_PRECEDENCE)
protected static class TestSecurityConfiguration {
@@ -34,14 +34,7 @@ import org.springframework.web.reactive.function.client.WebClient;
*/
@SpringBootTest(classes = EurekaServerMockApplication.class,
properties = { "debug=true", "security.basic.enabled=true", "eureka.client.webclient.enabled=true",
"eureka.client.fetch-registry=false", "eureka.client.register-with-eureka=false",
// spring.http.converters.preferred-json-mapper=jackson2 is only necessary
// to make the
// mock Eureka Server used in this test to work
// TODO we can likely remove this once this Boot issue is fixed post
// 2025.1.0-RC1:
// https://github.com/spring-projects/spring-boot/issues/47798
"spring.http.converters.preferred-json-mapper=jackson2" },
"eureka.client.fetch-registry=false", "eureka.client.register-with-eureka=false" },
webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
class WebClientEurekaHttpClientTests extends AbstractEurekaHttpClientTests {