Search in sources :

Example 66 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method resolveDefaultValue.

@Test
public void resolveDefaultValue() throws Exception {
    MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof String);
    assertEquals("Invalid result", "bar", result);
}
Also used : MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 67 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class WebArgumentResolverAdapterTests method setUp.

@Before
public void setUp() throws Exception {
    adaptee = mock(WebArgumentResolver.class);
    adapter = new TestWebArgumentResolverAdapter(adaptee);
    parameter = new MethodParameter(getClass().getMethod("handle", Integer.TYPE), 0);
    webRequest = new ServletWebRequest(new MockHttpServletRequest());
    // Expose request to the current thread (for SpEL expressions)
    RequestContextHolder.setRequestAttributes(webRequest);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MethodParameter(org.springframework.core.MethodParameter) WebArgumentResolver(org.springframework.web.bind.support.WebArgumentResolver) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Before(org.junit.Before)

Example 68 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class ErrorsMethodArgumentResolver method getModelAttributeName.

private String getModelAttributeName(MethodParameter parameter) {
    Assert.isTrue(parameter.getParameterIndex() > 0, "Errors argument must be immediately after a model attribute argument.");
    int index = parameter.getParameterIndex() - 1;
    MethodParameter attributeParam = new MethodParameter(parameter.getMethod(), index);
    Class<?> attributeType = attributeParam.getParameterType();
    ResolvableType type = ResolvableType.forMethodParameter(attributeParam);
    ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type.resolve());
    Assert.isNull(adapter, "Errors/BindingResult cannot be used with an async model attribute. " + "Either declare the model attribute without the async wrapper type " + "or handle WebExchangeBindException through the async type.");
    ModelAttribute annot = parameter.getParameterAnnotation(ModelAttribute.class);
    if (annot != null && StringUtils.hasText(annot.value())) {
        return annot.value();
    }
    // TODO: Conventions does not deal with async wrappers
    return ClassUtils.getShortNameAsProperty(attributeType);
}
Also used : ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) MethodParameter(org.springframework.core.MethodParameter) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 69 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class ModelAttributeMethodArgumentResolver method resolveArgument.

@Override
public Mono<Object> resolveArgument(MethodParameter parameter, BindingContext context, ServerWebExchange exchange) {
    ResolvableType type = ResolvableType.forMethodParameter(parameter);
    ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type.resolve());
    ResolvableType valueType = (adapter != null ? type.getGeneric(0) : type);
    Assert.state(adapter == null || !adapter.isMultiValue(), getClass().getSimpleName() + " doesn't support multi-value reactive type wrapper: " + parameter.getGenericParameterType());
    String name = getAttributeName(valueType, parameter);
    Mono<?> valueMono = getAttributeMono(name, valueType, context.getModel());
    Map<String, Object> model = context.getModel().asMap();
    MonoProcessor<BindingResult> bindingResultMono = MonoProcessor.create();
    model.put(BindingResult.MODEL_KEY_PREFIX + name, bindingResultMono);
    return valueMono.then(value -> {
        WebExchangeDataBinder binder = context.createDataBinder(exchange, value, name);
        return binder.bind(exchange).doOnError(bindingResultMono::onError).doOnSuccess(aVoid -> {
            validateIfApplicable(binder, parameter);
            BindingResult errors = binder.getBindingResult();
            model.put(BindingResult.MODEL_KEY_PREFIX + name, errors);
            model.put(name, value);
            bindingResultMono.onNext(errors);
        }).then(Mono.fromCallable(() -> {
            BindingResult errors = binder.getBindingResult();
            if (adapter != null) {
                return adapter.fromPublisher(errors.hasErrors() ? Mono.error(new WebExchangeBindException(parameter, errors)) : valueMono);
            } else {
                if (errors.hasErrors() && !hasErrorsArgument(parameter)) {
                    throw new WebExchangeBindException(parameter, errors);
                }
                return value;
            }
        }));
    });
}
Also used : ReactiveAdapter(org.springframework.core.ReactiveAdapter) Errors(org.springframework.validation.Errors) Validated(org.springframework.validation.annotation.Validated) ClassUtils(org.springframework.util.ClassUtils) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) MonoProcessor(reactor.core.publisher.MonoProcessor) Mono(reactor.core.publisher.Mono) BindingResult(org.springframework.validation.BindingResult) BindingContext(org.springframework.web.reactive.BindingContext) HandlerMethodArgumentResolver(org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HandlerMethodArgumentResolverSupport(org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport) Model(org.springframework.ui.Model) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) Map(java.util.Map) MethodParameter(org.springframework.core.MethodParameter) Annotation(java.lang.annotation.Annotation) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) BeanUtils(org.springframework.beans.BeanUtils) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) BindingResult(org.springframework.validation.BindingResult) ResolvableType(org.springframework.core.ResolvableType) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 70 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class ResponseBodyResultHandler method supports.

@Override
public boolean supports(HandlerResult result) {
    MethodParameter parameter = result.getReturnTypeSource();
    Class<?> containingClass = parameter.getContainingClass();
    return (AnnotationUtils.findAnnotation(containingClass, ResponseBody.class) != null || parameter.getMethodAnnotation(ResponseBody.class) != null);
}
Also used : MethodParameter(org.springframework.core.MethodParameter) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

MethodParameter (org.springframework.core.MethodParameter)322 Test (org.junit.Test)251 Method (java.lang.reflect.Method)65 ArrayList (java.util.ArrayList)35 RequestParam (org.springframework.web.bind.annotation.RequestParam)30 HandlerMethod (org.springframework.web.method.HandlerMethod)28 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)27 Before (org.junit.Before)25 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)25 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)23 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)23 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)23 ResolvableType (org.springframework.core.ResolvableType)21 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)21 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)21 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)20 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)20 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)20 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)20 Mono (reactor.core.publisher.Mono)19