Add a ResolvableType field to HandlerResult

This change allows to be able to check generic type on the return value
at HandlerAdapter and ResultHandler level. For example, it allows to do
a Publisher<Void> check in SimpleHandlerResultHandler.
This commit is contained in:
Sebastien Deleuze
2015-11-02 12:16:39 +01:00
committed by Rossen Stoyanchev
parent 5d4201d500
commit c6713c23e3
7 changed files with 96 additions and 10 deletions

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.reactive.web.dispatch;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.reactivestreams.Publisher;
import org.springframework.core.ResolvableType;
import org.springframework.web.method.HandlerMethod;
/**
* @author Sebastien Deleuze
*/
public class SimpleHandlerResultHandlerTests {
@Test
public void supports() throws NoSuchMethodException {
SimpleHandlerResultHandler resultHandler = new SimpleHandlerResultHandler();
TestController controller = new TestController();
HandlerMethod hm = new HandlerMethod(controller, TestController.class.getMethod("voidReturnValue"));
ResolvableType type = ResolvableType.forMethodParameter(hm.getReturnType());
assertFalse(resultHandler.supports(new HandlerResult(hm, null, type)));
hm = new HandlerMethod(controller, TestController.class.getMethod("publisherString"));
type = ResolvableType.forMethodParameter(hm.getReturnType());
assertFalse(resultHandler.supports(new HandlerResult(hm, null, type)));
hm = new HandlerMethod(controller, TestController.class.getMethod("publisherVoid"));
type = ResolvableType.forMethodParameter(hm.getReturnType());
assertTrue(resultHandler.supports(new HandlerResult(hm, null, type)));
}
@SuppressWarnings("unused")
private static class TestController {
public Publisher<String> voidReturnValue() {
return null;
}
public Publisher<String> publisherString() {
return null;
}
public Publisher<Void> publisherVoid() {
return null;
}
}
}

View File

@@ -21,6 +21,7 @@ import java.util.Collections;
import org.junit.Test;
import org.reactivestreams.Publisher;
import org.springframework.core.ResolvableType;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.reactive.codec.encoder.StringEncoder;
import org.springframework.reactive.web.dispatch.HandlerResult;
@@ -43,13 +44,16 @@ public class ResponseBodyResultHandlerTests {
TestController controller = new TestController();
HandlerMethod hm = new HandlerMethod(controller,TestController.class.getMethod("notAnnotated"));
assertFalse(handler.supports(new HandlerResult(hm, null)));
ResolvableType type = ResolvableType.forMethodParameter(hm.getReturnType());
assertFalse(handler.supports(new HandlerResult(hm, null, type)));
hm = new HandlerMethod(controller, TestController.class.getMethod("publisherString"));
assertTrue(handler.supports(new HandlerResult(hm, null)));
type = ResolvableType.forMethodParameter(hm.getReturnType());
assertTrue(handler.supports(new HandlerResult(hm, null, type)));
hm = new HandlerMethod(controller, TestController.class.getMethod("publisherVoid"));
assertTrue(handler.supports(new HandlerResult(hm, null)));
type = ResolvableType.forMethodParameter(hm.getReturnType());
assertTrue(handler.supports(new HandlerResult(hm, null, type)));
}