Search in sources :

Example 6 with HandlerMethod

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

the class AbstractHandlerMethodMapping method createHandlerMethod.

/**
	 * Create the HandlerMethod instance.
	 * @param handler either a bean name or an actual handler instance
	 * @param method the target method
	 * @return the created HandlerMethod
	 */
protected HandlerMethod createHandlerMethod(Object handler, Method method) {
    HandlerMethod handlerMethod;
    if (handler instanceof String) {
        String beanName = (String) handler;
        handlerMethod = new HandlerMethod(beanName, getApplicationContext().getAutowireCapableBeanFactory(), method);
    } else {
        handlerMethod = new HandlerMethod(handler, method);
    }
    return handlerMethod;
}
Also used : HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 7 with HandlerMethod

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

the class MessageReaderArgumentResolverTests method parameterizedMethodArgument.

// SPR-9964
@Test
public void parameterizedMethodArgument() throws Exception {
    Method method = AbstractParameterizedController.class.getMethod("handleDto", Identifiable.class);
    HandlerMethod handlerMethod = new HandlerMethod(new ConcreteParameterizedController(), method);
    MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
    SimpleBean simpleBean = resolveValue(methodParam, "{\"name\" : \"Jad\"}");
    assertEquals("Jad", simpleBean.getName());
}
Also used : HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) ResolvableMethod(org.springframework.web.method.ResolvableMethod) MethodParameter(org.springframework.core.MethodParameter) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 8 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 9 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 10 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)235 Test (org.junit.jupiter.api.Test)87 Method (java.lang.reflect.Method)68 ModelAndView (org.springframework.web.servlet.ModelAndView)44 InvocableHandlerMethod (org.springframework.web.method.support.InvocableHandlerMethod)42 ArrayList (java.util.ArrayList)28 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)26 MethodParameter (org.springframework.core.MethodParameter)25 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)25 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)24 Test (org.junit.Test)19 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)19 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)17 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)17 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)17 IOException (java.io.IOException)14 RequestMethod (org.springframework.web.bind.annotation.RequestMethod)14 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)14 Map (java.util.Map)13 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)12