Polish SimpleResultHandler
This commit is contained in:
parent
1b308cffbf
commit
f8a7024b73
|
@ -47,9 +47,6 @@ public class SimpleResultHandler implements Ordered, HandlerResultHandler {
|
|||
private int order = Ordered.LOWEST_PRECEDENCE;
|
||||
|
||||
|
||||
public SimpleResultHandler() {
|
||||
}
|
||||
|
||||
public SimpleResultHandler(ConversionService conversionService) {
|
||||
Assert.notNull(conversionService, "'conversionService' is required.");
|
||||
this.conversionService = conversionService;
|
||||
|
@ -76,18 +73,14 @@ public class SimpleResultHandler implements Ordered, HandlerResultHandler {
|
|||
@Override
|
||||
public boolean supports(HandlerResult result) {
|
||||
ResolvableType type = result.getReturnValueType();
|
||||
return (type != null && (Void.TYPE.equals(type.getRawClass()) || isConvertibleToVoidPublisher(type)));
|
||||
if (Void.TYPE.equals(type.getRawClass())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isConvertibleToVoidPublisher(ResolvableType type) {
|
||||
return (isConvertibleToPublisher(type) &&
|
||||
Void.class.isAssignableFrom(type.getGeneric(0).getRawClass()));
|
||||
if (this.conversionService.canConvert(type.getRawClass(), Publisher.class)) {
|
||||
Class<?> clazz = result.getReturnValueType().getGeneric(0).getRawClass();
|
||||
return Void.class.equals(clazz);
|
||||
}
|
||||
|
||||
private boolean isConvertibleToPublisher(ResolvableType type) {
|
||||
Class<?> clazz = type.getRawClass();
|
||||
return (Publisher.class.isAssignableFrom(clazz) ||
|
||||
((this.conversionService != null) && this.conversionService.canConvert(clazz, Publisher.class)));
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
|
|
@ -24,6 +24,7 @@ import reactor.core.publisher.Flux;
|
|||
import rx.Observable;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.core.convert.support.GenericConversionService;
|
||||
import org.springframework.core.convert.support.ReactiveStreamsToCompletableFutureConverter;
|
||||
import org.springframework.core.convert.support.ReactiveStreamsToRxJava1Converter;
|
||||
|
@ -35,48 +36,19 @@ import static org.junit.Assert.assertFalse;
|
|||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link SimpleResultHandler}.
|
||||
* @author Sebastien Deleuze
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class SimpleResultHandlerTests {
|
||||
|
||||
@Test
|
||||
public void supports() throws NoSuchMethodException {
|
||||
|
||||
SimpleResultHandler resultHandler = new SimpleResultHandler();
|
||||
TestController controller = new TestController();
|
||||
|
||||
HandlerMethod hm = new HandlerMethod(controller, TestController.class.getMethod("voidReturnValue"));
|
||||
ResolvableType type = ResolvableType.forMethodParameter(hm.getReturnType());
|
||||
assertTrue(resultHandler.supports(createHandlerResult(hm, type)));
|
||||
|
||||
hm = new HandlerMethod(controller, TestController.class.getMethod("publisherString"));
|
||||
type = ResolvableType.forMethodParameter(hm.getReturnType());
|
||||
assertFalse(resultHandler.supports(createHandlerResult(hm, type)));
|
||||
|
||||
hm = new HandlerMethod(controller, TestController.class.getMethod("publisherVoid"));
|
||||
type = ResolvableType.forMethodParameter(hm.getReturnType());
|
||||
assertTrue(resultHandler.supports(createHandlerResult(hm, type)));
|
||||
|
||||
hm = new HandlerMethod(controller, TestController.class.getMethod("streamVoid"));
|
||||
type = ResolvableType.forMethodParameter(hm.getReturnType());
|
||||
// Reactor Flux is a Publisher
|
||||
assertTrue(resultHandler.supports(createHandlerResult(hm, type)));
|
||||
|
||||
hm = new HandlerMethod(controller, TestController.class.getMethod("observableVoid"));
|
||||
type = ResolvableType.forMethodParameter(hm.getReturnType());
|
||||
assertFalse(resultHandler.supports(createHandlerResult(hm, type)));
|
||||
|
||||
hm = new HandlerMethod(controller, TestController.class.getMethod("completableFutureVoid"));
|
||||
type = ResolvableType.forMethodParameter(hm.getReturnType());
|
||||
assertFalse(resultHandler.supports(createHandlerResult(hm, type)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsWithConversionService() throws NoSuchMethodException {
|
||||
|
||||
GenericConversionService conversionService = new GenericConversionService();
|
||||
conversionService.addConverter(new ReactiveStreamsToCompletableFutureConverter());
|
||||
conversionService.addConverter(new ReactiveStreamsToRxJava1Converter());
|
||||
|
||||
SimpleResultHandler resultHandler = new SimpleResultHandler(conversionService);
|
||||
TestController controller = new TestController();
|
||||
|
||||
|
|
|
@ -26,7 +26,12 @@ import org.junit.Test;
|
|||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
@ -60,10 +65,8 @@ public class WebHandlerIntegrationTests extends AbstractHttpHandlerIntegrationTe
|
|||
@Override
|
||||
protected HttpHandler createHttpHandler() {
|
||||
|
||||
StaticApplicationContext wac = new StaticApplicationContext();
|
||||
wac.registerSingleton("handlerMapping", TestSimpleUrlHandlerMapping.class);
|
||||
wac.registerSingleton("handlerAdapter", SimpleHandlerAdapter.class);
|
||||
wac.registerSingleton("resultHandler", SimpleResultHandler.class);
|
||||
AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
|
||||
wac.register(WebConfig.class);
|
||||
wac.refresh();
|
||||
|
||||
DispatcherHandler dispatcherHandler = new DispatcherHandler();
|
||||
|
@ -172,4 +175,24 @@ public class WebHandlerIntegrationTests extends AbstractHttpHandlerIntegrationTe
|
|||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class WebConfig {
|
||||
|
||||
@Bean
|
||||
public TestSimpleUrlHandlerMapping handlerMapping() {
|
||||
return new TestSimpleUrlHandlerMapping();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleHandlerAdapter handlerAdapter() {
|
||||
return new SimpleHandlerAdapter();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleResultHandler resultHandler() {
|
||||
return new SimpleResultHandler(new DefaultConversionService());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue