Commit a53ba390 by Johannes Stelzer

Add MappingJackson2HttpMessageConverter only if not present

fixes #61
parent 967525aa
......@@ -14,6 +14,9 @@ spring:
cloud:
config:
enabled: false
jackson:
serialization:
indent_output: true
endpoints:
health:
......
......@@ -43,6 +43,11 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</exclusion>
<exclusion>
<!-- ships old hamcres matchers -->
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
......@@ -80,13 +85,8 @@
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
......
......@@ -47,14 +47,17 @@ import org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFil
import org.springframework.cloud.netflix.zuul.web.ZuulController;
import org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hazelcast.config.Config;
import com.hazelcast.core.EntryEvent;
import com.hazelcast.core.EntryListener;
......@@ -79,14 +82,32 @@ import de.codecentric.boot.admin.zuul.ApplicationRouteLocator;
import de.codecentric.boot.admin.zuul.ApplicationRouteRefreshListener;
@Configuration
public class AdminServerWebConfiguration extends WebMvcConfigurerAdapter {
public class AdminServerWebConfiguration extends WebMvcConfigurerAdapter implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
/**
* Add JSON MessageConverter to send JSON objects to web clients.
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MappingJackson2HttpMessageConverter());
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
if (!hasConverter(converters, MappingJackson2HttpMessageConverter.class)) {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().applicationContext(this.applicationContext)
.build();
converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
}
}
private boolean hasConverter(List<HttpMessageConverter<?>> converters,
Class<? extends HttpMessageConverter<?>> clazz) {
for (HttpMessageConverter<?> converter : converters) {
if (clazz.isInstance(converter)) {
return true;
}
}
return false;
}
/**
......
package de.codecentric.boot.admin.config;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.isA;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import de.codecentric.boot.admin.discovery.ApplicationDiscoveryListener;
......@@ -26,6 +35,31 @@ public class AdminServerWebConfigurationTest {
}
@Test
public void jacksonMapperPresentFromDefault() {
AdminServerWebConfiguration config = new AdminServerWebConfiguration();
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
converters.add(new MappingJackson2HttpMessageConverter());
config.extendMessageConverters(converters);
assertThat(converters,
hasItem(isA(MappingJackson2HttpMessageConverter.class)));
assertThat(converters.size(), is(1));
}
@Test
public void jacksonMapperPresentNeedExtend() {
AdminServerWebConfiguration config = new AdminServerWebConfiguration();
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
config.extendMessageConverters(converters);
assertThat(converters, hasItem(isA(MappingJackson2HttpMessageConverter.class)));
assertThat(converters.size(), is(1));
}
@Test
public void simpleConfig() {
load("spring.boot.admin.hazelcast.enabled:false", "spring.boot.admin.discovery.enabled:false");
assertTrue(context.getBean(ApplicationStore.class) instanceof SimpleApplicationStore);
......@@ -46,15 +80,14 @@ public class AdminServerWebConfigurationTest {
context.getBean(ApplicationDiscoveryListener.class);
}
private void load(String... environment) {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(ServerPropertiesAutoConfiguration.class);
applicationContext.register(NoopDiscoveryClientAutoConfiguration.class);
applicationContext.register(AdminServerWebConfiguration.class);
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
applicationContext.refresh();
this.context = applicationContext;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment