Search in sources :

Example 6 with PathPatternParser

use of org.springframework.web.util.pattern.PathPatternParser in project spring-framework by spring-projects.

the class RequestMappingHandlerMappingTests method pathPrefix.

@Test
public void pathPrefix() throws Exception {
    this.handlerMapping.setEmbeddedValueResolver(value -> "/${prefix}".equals(value) ? "/api" : value);
    this.handlerMapping.setPathPrefixes(Collections.singletonMap("/${prefix}", HandlerTypePredicate.forAnnotation(RestController.class)));
    Method method = UserController.class.getMethod("getUser");
    RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, UserController.class);
    assertThat(info).isNotNull();
    assertThat(info.getPatternsCondition().getPatterns()).isEqualTo(Collections.singleton(new PathPatternParser().parse("/api/user/{id}")));
}
Also used : RequestMappingInfo(org.springframework.web.reactive.result.method.RequestMappingInfo) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) Method(java.lang.reflect.Method) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Test(org.junit.jupiter.api.Test)

Example 7 with PathPatternParser

use of org.springframework.web.util.pattern.PathPatternParser in project spring-framework by spring-projects.

the class WebMvcConfigurationSupport method routerFunctionMapping.

/**
 * Return a {@link RouterFunctionMapping} ordered at 3 to map
 * {@linkplain org.springframework.web.servlet.function.RouterFunction router functions}.
 * Consider overriding one of these other more fine-grained methods:
 * <ul>
 * <li>{@link #addInterceptors} for adding handler interceptors.
 * <li>{@link #addCorsMappings} to configure cross origin requests processing.
 * <li>{@link #configureMessageConverters} for adding custom message converters.
 * <li>{@link #configurePathMatch(PathMatchConfigurer)} for customizing the {@link PathPatternParser}.
 * </ul>
 * @since 5.2
 */
@Bean
public RouterFunctionMapping routerFunctionMapping(@Qualifier("mvcConversionService") FormattingConversionService conversionService, @Qualifier("mvcResourceUrlProvider") ResourceUrlProvider resourceUrlProvider) {
    RouterFunctionMapping mapping = new RouterFunctionMapping();
    mapping.setOrder(3);
    mapping.setInterceptors(getInterceptors(conversionService, resourceUrlProvider));
    mapping.setCorsConfigurations(getCorsConfigurations());
    mapping.setMessageConverters(getMessageConverters());
    PathPatternParser patternParser = getPathMatchConfigurer().getPatternParser();
    if (patternParser != null) {
        mapping.setPatternParser(patternParser);
    }
    return mapping;
}
Also used : RouterFunctionMapping(org.springframework.web.servlet.function.support.RouterFunctionMapping) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) Bean(org.springframework.context.annotation.Bean)

Example 8 with PathPatternParser

use of org.springframework.web.util.pattern.PathPatternParser in project spring-framework by spring-projects.

the class PathPatternsRequestConditionTests method matchTrailingSlash.

@Test
void matchTrailingSlash() {
    MockHttpServletRequest request = createRequest("/foo/");
    PathPatternsRequestCondition condition = createCondition("/foo");
    PathPatternsRequestCondition match = condition.getMatchingCondition(request);
    assertThat(match).isNotNull();
    assertThat(match.getPatternValues().iterator().next()).as("Should match by default").isEqualTo("/foo");
    PathPatternParser strictParser = new PathPatternParser();
    strictParser.setMatchOptionalTrailingSeparator(false);
    condition = new PathPatternsRequestCondition(strictParser, "/foo");
    match = condition.getMatchingCondition(request);
    assertThat(match).isNull();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) Test(org.junit.jupiter.api.Test)

Example 9 with PathPatternParser

use of org.springframework.web.util.pattern.PathPatternParser in project spring-framework by spring-projects.

the class WebFluxConfigurationSupportTests method requestMappingHandlerMapping.

@Test
public void requestMappingHandlerMapping() {
    ApplicationContext context = loadConfig(WebFluxConfig.class);
    final Field field = ReflectionUtils.findField(PathPatternParser.class, "matchOptionalTrailingSeparator");
    ReflectionUtils.makeAccessible(field);
    String name = "requestMappingHandlerMapping";
    RequestMappingHandlerMapping mapping = context.getBean(name, RequestMappingHandlerMapping.class);
    assertThat(mapping).isNotNull();
    assertThat(mapping.getOrder()).isEqualTo(0);
    PathPatternParser patternParser = mapping.getPathPatternParser();
    assertThat(patternParser).isNotNull();
    boolean matchOptionalTrailingSlash = (boolean) ReflectionUtils.getField(field, patternParser);
    assertThat(matchOptionalTrailingSlash).isTrue();
    name = "webFluxContentTypeResolver";
    RequestedContentTypeResolver resolver = context.getBean(name, RequestedContentTypeResolver.class);
    assertThat(mapping.getContentTypeResolver()).isSameAs(resolver);
    ServerWebExchange exchange = MockServerWebExchange.from(get("/path").accept(MediaType.APPLICATION_JSON));
    assertThat(resolver.resolveMediaTypes(exchange)).isEqualTo(Collections.singletonList(MediaType.APPLICATION_JSON));
}
Also used : Field(java.lang.reflect.Field) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) RequestMappingHandlerMapping(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) Test(org.junit.jupiter.api.Test)

Example 10 with PathPatternParser

use of org.springframework.web.util.pattern.PathPatternParser in project spring-framework by spring-projects.

the class RequestMappingHandlerMappingTests method pathPatternsArguments.

@SuppressWarnings("unused")
static Stream<Arguments> pathPatternsArguments() {
    RequestMappingHandlerMapping mapping1 = new RequestMappingHandlerMapping();
    StaticWebApplicationContext wac1 = new StaticWebApplicationContext();
    mapping1.setPatternParser(new PathPatternParser());
    mapping1.setApplicationContext(wac1);
    RequestMappingHandlerMapping mapping2 = new RequestMappingHandlerMapping();
    StaticWebApplicationContext wac2 = new StaticWebApplicationContext();
    mapping2.setApplicationContext(wac2);
    return Stream.of(Arguments.of(mapping1, wac1), Arguments.of(mapping2, wac2));
}
Also used : PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext)

Aggregations

PathPatternParser (org.springframework.web.util.pattern.PathPatternParser)22 Test (org.junit.jupiter.api.Test)13 RequestMappingInfo (org.springframework.web.reactive.result.method.RequestMappingInfo)4 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)3 Field (java.lang.reflect.Field)2 Collections (java.util.Collections)2 List (java.util.List)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 ApplicationContext (org.springframework.context.ApplicationContext)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)2 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)2 PathPattern (org.springframework.web.util.pattern.PathPattern)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Tag (io.micrometer.core.instrument.Tag)1 Method (java.lang.reflect.Method)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1 BiConsumer (java.util.function.BiConsumer)1