Add Encoder constructor to HttpMessageConverterView

This commit is contained in:
Rossen Stoyanchev 2016-06-06 09:35:10 -04:00
parent b45a48d0fc
commit 505569c992
3 changed files with 24 additions and 12 deletions

View File

@ -25,7 +25,9 @@ import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
import org.springframework.core.codec.Encoder;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.converter.reactive.CodecHttpMessageConverter;
import org.springframework.http.converter.reactive.HttpMessageConverter; import org.springframework.http.converter.reactive.HttpMessageConverter;
import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
@ -48,6 +50,19 @@ public class HttpMessageConverterView implements View {
private final List<MediaType> mediaTypes; private final List<MediaType> mediaTypes;
/**
* Create a {@code View} with the given {@code Encoder}.
* Internally this creates
* {@link CodecHttpMessageConverter#CodecHttpMessageConverter(Encoder)
* CodecHttpMessageConverter(Encoder)}.
*/
public HttpMessageConverterView(Encoder<?> encoder) {
this(new CodecHttpMessageConverter<>(encoder));
}
/**
* Create a View that delegates to the given message converter.
*/
public HttpMessageConverterView(HttpMessageConverter<?> converter) { public HttpMessageConverterView(HttpMessageConverter<?> converter) {
Assert.notNull(converter, "'converter' is required."); Assert.notNull(converter, "'converter' is required.");
this.converter = converter; this.converter = converter;
@ -55,6 +70,9 @@ public class HttpMessageConverterView implements View {
} }
/**
* Return the configured message converter.
*/
public HttpMessageConverter<?> getConverter() { public HttpMessageConverter<?> getConverter() {
return this.converter; return this.converter;
} }

View File

@ -18,7 +18,6 @@ package org.springframework.web.reactive;
import java.net.URI; import java.net.URI;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -30,7 +29,6 @@ import reactor.core.util.SignalKind;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.codec.support.StringDecoder;
import org.springframework.core.codec.support.StringEncoder; import org.springframework.core.codec.support.StringEncoder;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.DefaultConversionService;
@ -63,7 +61,9 @@ import org.springframework.web.server.handler.FilteringWebHandler;
import org.springframework.web.server.session.WebSessionManager; import org.springframework.web.server.session.WebSessionManager;
import static org.hamcrest.CoreMatchers.startsWith; import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
/** /**

View File

@ -35,12 +35,9 @@ import org.springframework.core.codec.support.JacksonJsonEncoder;
import org.springframework.core.codec.support.Jaxb2Encoder; import org.springframework.core.codec.support.Jaxb2Encoder;
import org.springframework.core.codec.support.Pojo; import org.springframework.core.codec.support.Pojo;
import org.springframework.core.codec.support.StringEncoder; import org.springframework.core.codec.support.StringEncoder;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.support.DataBufferTestUtils; import org.springframework.core.io.buffer.support.DataBufferTestUtils;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.converter.reactive.CodecHttpMessageConverter;
import org.springframework.http.converter.reactive.HttpMessageConverter;
import org.springframework.http.server.reactive.MockServerHttpRequest; import org.springframework.http.server.reactive.MockServerHttpRequest;
import org.springframework.http.server.reactive.MockServerHttpResponse; import org.springframework.http.server.reactive.MockServerHttpResponse;
import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.ExtendedModelMap;
@ -74,8 +71,7 @@ public class HttpMessageConverterViewTests {
@Before @Before
public void setup() throws Exception { public void setup() throws Exception {
HttpMessageConverter<?> converter = new CodecHttpMessageConverter<>(new JacksonJsonEncoder()); this.view = new HttpMessageConverterView(new JacksonJsonEncoder());
this.view = new HttpMessageConverterView(converter);
this.model = new ExtendedModelMap(); this.model = new ExtendedModelMap();
this.result = new HandlerResult(new Object(), null, ResolvableType.NONE, model); this.result = new HandlerResult(new Object(), null, ResolvableType.NONE, model);
} }
@ -127,8 +123,7 @@ public class HttpMessageConverterViewTests {
@Test @Test
public void extractObjectMultipleMatchesNotSupported() throws Exception { public void extractObjectMultipleMatchesNotSupported() throws Exception {
HttpMessageConverter<?> converter = new CodecHttpMessageConverter<>(new StringEncoder()); HttpMessageConverterView view = new HttpMessageConverterView(new StringEncoder());
HttpMessageConverterView view = new HttpMessageConverterView(converter);
view.setModelKeys(new HashSet<>(Arrays.asList("foo1", "foo2"))); view.setModelKeys(new HashSet<>(Arrays.asList("foo1", "foo2")));
this.model.addAttribute("foo1", "bar1"); this.model.addAttribute("foo1", "bar1");
this.model.addAttribute("foo2", "bar2"); this.model.addAttribute("foo2", "bar2");
@ -145,8 +140,7 @@ public class HttpMessageConverterViewTests {
@Test @Test
public void extractObjectNotSupported() throws Exception { public void extractObjectNotSupported() throws Exception {
HttpMessageConverter<?> converter = new CodecHttpMessageConverter<>(new Jaxb2Encoder()); HttpMessageConverterView view = new HttpMessageConverterView(new Jaxb2Encoder());
HttpMessageConverterView view = new HttpMessageConverterView(converter);
view.setModelKeys(new HashSet<>(Collections.singletonList("foo1"))); view.setModelKeys(new HashSet<>(Collections.singletonList("foo1")));
this.model.addAttribute("foo1", "bar1"); this.model.addAttribute("foo1", "bar1");