Search in sources :

Example 51 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.

the class RequestMappingInfoHandlerMappingTests method testHttpOptions.

private void testHttpOptions(String requestURI, String allowHeader) throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.options(requestURI).toExchange();
    HandlerMethod handlerMethod = (HandlerMethod) this.handlerMapping.getHandler(exchange).block();
    BindingContext bindingContext = new BindingContext();
    InvocableHandlerMethod invocable = new InvocableHandlerMethod(handlerMethod);
    Mono<HandlerResult> mono = invocable.invoke(exchange, bindingContext);
    HandlerResult result = mono.block();
    assertNotNull(result);
    Optional<Object> value = result.getReturnValue();
    assertTrue(value.isPresent());
    assertEquals(HttpHeaders.class, value.get().getClass());
    assertEquals(allowHeader, ((HttpHeaders) value.get()).getFirst("Allow"));
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) HandlerResult(org.springframework.web.reactive.HandlerResult) BindingContext(org.springframework.web.reactive.BindingContext) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 52 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.

the class MvcUriComponentsBuilder method fromMappingName.

/**
	 * An alternative to {@link #fromMappingName(String)} that accepts a
	 * {@code UriComponentsBuilder} representing the base URL. This is useful
	 * when using MvcUriComponentsBuilder outside the context of processing a
	 * request or to apply a custom baseUrl not matching the current request.
	 * @param builder the builder for the base URL; the builder will be cloned
	 * and therefore not modified and may be re-used for further calls.
	 * @param name the mapping name
	 * @return a builder to prepare the URI String
	 * @throws IllegalArgumentException if the mapping name is not found or
	 * if there is no unique match
	 * @since 4.2
	 */
public static MethodArgumentBuilder fromMappingName(UriComponentsBuilder builder, String name) {
    RequestMappingInfoHandlerMapping handlerMapping = getRequestMappingInfoHandlerMapping();
    List<HandlerMethod> handlerMethods = handlerMapping.getHandlerMethodsForMappingName(name);
    if (handlerMethods == null) {
        throw new IllegalArgumentException("Mapping mappingName not found: " + name);
    }
    if (handlerMethods.size() != 1) {
        throw new IllegalArgumentException("No unique match for mapping mappingName " + name + ": " + handlerMethods);
    }
    HandlerMethod handlerMethod = handlerMethods.get(0);
    Class<?> controllerType = handlerMethod.getBeanType();
    Method method = handlerMethod.getMethod();
    return new MethodArgumentBuilder(builder, controllerType, method);
}
Also used : RequestMappingInfoHandlerMapping(org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 53 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapter method getModelFactory.

private ModelFactory getModelFactory(HandlerMethod handlerMethod, WebDataBinderFactory binderFactory) {
    SessionAttributesHandler sessionAttrHandler = getSessionAttributesHandler(handlerMethod);
    Class<?> handlerType = handlerMethod.getBeanType();
    Set<Method> methods = this.modelAttributeCache.get(handlerType);
    if (methods == null) {
        methods = MethodIntrospector.selectMethods(handlerType, MODEL_ATTRIBUTE_METHODS);
        this.modelAttributeCache.put(handlerType, methods);
    }
    List<InvocableHandlerMethod> attrMethods = new ArrayList<>();
    // Global methods first
    for (Entry<ControllerAdviceBean, Set<Method>> entry : this.modelAttributeAdviceCache.entrySet()) {
        if (entry.getKey().isApplicableToBeanType(handlerType)) {
            Object bean = entry.getKey().resolveBean();
            for (Method method : entry.getValue()) {
                attrMethods.add(createModelAttributeMethod(binderFactory, bean, method));
            }
        }
    }
    for (Method method : methods) {
        Object bean = handlerMethod.getBean();
        attrMethods.add(createModelAttributeMethod(binderFactory, bean, method));
    }
    return new ModelFactory(attrMethods, binderFactory, sessionAttrHandler);
}
Also used : SessionAttributesHandler(org.springframework.web.method.annotation.SessionAttributesHandler) Set(java.util.Set) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) ArrayList(java.util.ArrayList) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) ModelFactory(org.springframework.web.method.annotation.ModelFactory) ControllerAdviceBean(org.springframework.web.method.ControllerAdviceBean)

Example 54 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.

the class HandlerResultMatchers method method.

/**
	 * Assert the controller method used to process the request.
	 */
public ResultMatcher method(final Method method) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) throws Exception {
            HandlerMethod handlerMethod = getHandlerMethod(result);
            assertEquals("Handler method", method, handlerMethod.getMethod());
        }
    };
}
Also used : ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 55 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.

the class HandlerResultMatchers method methodName.

/**
	 * Assert the name of the controller method used to process the request
	 * using the given Hamcrest {@link Matcher}.
	 */
public ResultMatcher methodName(final Matcher<? super String> matcher) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) throws Exception {
            HandlerMethod handlerMethod = getHandlerMethod(result);
            assertThat("Handler method", handlerMethod.getMethod().getName(), matcher);
        }
    };
}
Also used : ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult) HandlerMethod(org.springframework.web.method.HandlerMethod)

Aggregations

HandlerMethod (org.springframework.web.method.HandlerMethod)131 Test (org.junit.Test)81 Method (java.lang.reflect.Method)42 InvocableHandlerMethod (org.springframework.web.method.support.InvocableHandlerMethod)34 ArrayList (java.util.ArrayList)26 ModelAndView (org.springframework.web.servlet.ModelAndView)24 MethodParameter (org.springframework.core.MethodParameter)20 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)19 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)19 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)18 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)18 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)16 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)16 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)16 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)13 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)8 HttpMethod (org.springframework.http.HttpMethod)7 ServerWebExchange (org.springframework.web.server.ServerWebExchange)7 Map (java.util.Map)6 List (java.util.List)5