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"));
}
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);
}
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);
}
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());
}
};
}
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);
}
};
}
Aggregations