/** * Uses method and type-level @{@link RequestMapping} annotations to create * the RequestMappingInfo. * @return the created RequestMappingInfo, or {@code null} if the method * does not have a {@code @RequestMapping} annotation. * @see #getCustomMethodCondition(Method) * @see #getCustomTypeCondition(Class) */ @Override @Nullable protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { RequestMappingInfo info = createRequestMappingInfo(method); if (info != null) { RequestMappingInfo typeInfo = createRequestMappingInfo(handlerType); if (typeInfo != null) { info = typeInfo.combine(info); } String prefix = getPathPrefix(handlerType); if (prefix != null) { info = RequestMappingInfo.paths(prefix).build().combine(info); } } return info; }
/** * Create a {@link RequestMappingInfo} from the supplied * {@link RequestMapping @RequestMapping} annotation, which is either * a directly declared annotation, a meta-annotation, or the synthesized * result of merging annotation attributes within an annotation hierarchy. */ protected RequestMappingInfo createRequestMappingInfo( RequestMapping requestMapping, @Nullable RequestCondition<?> customCondition) { RequestMappingInfo.Builder builder = RequestMappingInfo .paths(resolveEmbeddedValuesInPatterns(requestMapping.path())) .methods(requestMapping.method()) .params(requestMapping.params()) .headers(requestMapping.headers()) .consumes(requestMapping.consumes()) .produces(requestMapping.produces()) .mappingName(requestMapping.name()); if (customCondition != null) { builder.customCondition(customCondition); } return builder.options(this.config).build(); }
.paths(resolveEmbeddedValuesInPatterns(requestMapping.path())) .methods(requestMapping.method()) .params(requestMapping.params()) .headers(requestMapping.headers()) .consumes(requestMapping.consumes()) .produces(requestMapping.produces()) .mappingName(requestMapping.name()) .customCondition(customCondition) .options(this.config) .build();
.methods(RequestMethod.POST).build(); RequestMappingInfo getInfo = RequestMappingInfo.paths(bathPath + "/verificationCode/{phone}") .methods(RequestMethod.GET).produces("application/json").build(); RequestMappingInfo verifyInfo = RequestMappingInfo.paths(bathPath + "/verificationCode") .methods(RequestMethod.POST).build(); RequestMappingInfo noticeInfo = RequestMappingInfo.paths(bathPath + "/notice").methods(RequestMethod.PUT) .build();
private RequestMappingInfo createRequestMappingInfo(String codelessName, String action) { RequestMappingInfo.Builder builder = RequestMappingInfo .paths(resolveEmbeddedValuesInPatterns(new String[] {"/" + codelessName + "/" + action})); if ("add".equals(action)) { builder.methods(RequestMethod.POST); } if ("modify".equals(action)) { builder.methods(RequestMethod.PUT); } if ("delete".equals(action)) { builder.methods(RequestMethod.DELETE); } if ("get".equals(action) || Pattern.matches("list.*", action)) { builder.methods(RequestMethod.GET); } builder.mappingName("[" + codelessEntityMetadataMap.get(codelessName).getEntityClass().getSimpleName() + "] " + action + " mapping"); return builder.options(this.config).build(); }
@PostConstruct public void setup() { requestMappingHandlerMapping .registerMapping(RequestMappingInfo.paths(getContext().getUrlPath() + "/login").build(), loginController(), new Mirror().on(LoginController.class) .reflect().method("getLoginView").withAnyArgs()); }
@PostConstruct public void config() throws Exception, SecurityException { RequestMappingInfo info = RequestMappingInfo.paths("/test/hh/name").mappingName("mine").build(); mapping.registerMapping(info, new TestMvc(), TestMvc.class.getDeclaredMethod("testA", String.class)); System.out.println("===========xxxxxxxxxxxxxxxxxxxxxxxxxxx=============="); } //
@Override public RequestMatchResult match(HttpServletRequest request, String pattern) { RequestMappingInfo info = RequestMappingInfo.paths(pattern).options(builderConfig) .build(); RequestMappingInfo matchingInfo = info.getMatchingCondition(request); if (matchingInfo == null) { return null; } Set<String> patterns = matchingInfo.getPatternsCondition().getPatterns(); String lookupPath = getUrlPathHelper().getLookupPathForRequest(request); return new RequestMatchResult(patterns.iterator().next(), lookupPath, getPathMatcher()); }
/** * Create a {@link RequestMappingInfo} from the supplied * {@link RequestMapping @RequestMapping} annotation, which is either * a directly declared annotation, a meta-annotation, or the synthesized * result of merging annotation attributes within an annotation hierarchy. */ protected RequestMappingInfo createRequestMappingInfo( RequestMapping requestMapping, @Nullable RequestCondition<?> customCondition) { RequestMappingInfo.Builder builder = RequestMappingInfo .paths(resolveEmbeddedValuesInPatterns(requestMapping.path())) .methods(requestMapping.method()) .params(requestMapping.params()) .headers(requestMapping.headers()) .consumes(requestMapping.consumes()) .produces(requestMapping.produces()) .mappingName(requestMapping.name()); if (customCondition != null) { builder.customCondition(customCondition); } return builder.options(this.config).build(); }
@Test public void equals() { RequestMappingInfo info1 = paths("/foo").methods(GET) .params("foo=bar", "customFoo=customBar").headers("foo=bar") .consumes("text/plain").produces("text/plain") .build(); RequestMappingInfo info2 = paths("/foo").methods(GET) .params("foo=bar", "customFoo=customBar").headers("foo=bar") .consumes("text/plain").produces("text/plain") .build(); info2 = paths("/foo", "/NOOOOOO").methods(GET) .params("foo=bar", "customFoo=customBar").headers("foo=bar") .consumes("text/plain").produces("text/plain") .build(); info2 = paths("/foo").methods(GET, RequestMethod.POST) .params("foo=bar", "customFoo=customBar").headers("foo=bar") .consumes("text/plain").produces("text/plain") .build(); info2 = paths("/foo").methods(GET) .params("/NOOOOOO", "customFoo=customBar").headers("foo=bar") .consumes("text/plain").produces("text/plain") .build(); info2 = paths("/foo").methods(GET) .params("foo=bar", "customFoo=customBar").headers("/NOOOOOO") .consumes("text/plain").produces("text/plain")
@Override public RequestMatchResult match(HttpServletRequest request, String pattern) { RequestMappingInfo info = RequestMappingInfo.paths(pattern).options(this.config).build(); RequestMappingInfo matchingInfo = info.getMatchingCondition(request); if (matchingInfo == null) { return null; } Set<String> patterns = matchingInfo.getPatternsCondition().getPatterns(); String lookupPath = getUrlPathHelper().getLookupPathForRequest(request); return new RequestMatchResult(patterns.iterator().next(), lookupPath, getPathMatcher()); }
/** * Uses method and type-level @{@link RequestMapping} annotations to create * the RequestMappingInfo. * @return the created RequestMappingInfo, or {@code null} if the method * does not have a {@code @RequestMapping} annotation. * @see #getCustomMethodCondition(Method) * @see #getCustomTypeCondition(Class) */ @Override @Nullable protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { RequestMappingInfo info = createRequestMappingInfo(method); if (info != null) { RequestMappingInfo typeInfo = createRequestMappingInfo(handlerType); if (typeInfo != null) { info = typeInfo.combine(info); } String prefix = getPathPrefix(handlerType); if (prefix != null) { info = RequestMappingInfo.paths(prefix).build().combine(info); } } return info; }
@Test // SPR-14383 public void compareToWithHttpHeadMapping() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod("HEAD"); request.addHeader("Accept", "application/json"); RequestMappingInfo noMethods = paths().build(); RequestMappingInfo getMethod = paths().methods(GET).produces("application/json").build(); RequestMappingInfo headMethod = paths().methods(HEAD).build(); Comparator<RequestMappingInfo> comparator = (info, otherInfo) -> info.compareTo(otherInfo, request); List<RequestMappingInfo> list = asList(noMethods, getMethod, headMethod); Collections.shuffle(list); Collections.sort(list, comparator); assertEquals(headMethod, list.get(0)); assertEquals(getMethod, list.get(1)); assertEquals(noMethods, list.get(2)); }
@SuppressWarnings("unchecked") @Test // SPR-9098 public void handleMatchUriTemplateVariablesDecode() { RequestMappingInfo key = RequestMappingInfo.paths("/{group}/{identifier}").build(); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/group/a%2Fb"); UrlPathHelper pathHelper = new UrlPathHelper(); pathHelper.setUrlDecode(false); String lookupPath = pathHelper.getLookupPathForRequest(request); this.handlerMapping.setUrlPathHelper(pathHelper); this.handlerMapping.handleMatch(key, lookupPath, request); String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE; Map<String, String> uriVariables = (Map<String, String>) request.getAttribute(name); assertNotNull(uriVariables); assertEquals("group", uriVariables.get("group")); assertEquals("a/b", uriVariables.get("identifier")); }
private void handleMatch(MockHttpServletRequest request, String pattern, String lookupPath) { RequestMappingInfo info = RequestMappingInfo.paths(pattern).build(); this.handlerMapping.handleMatch(info, lookupPath, request); }
@SuppressWarnings("unchecked") @Test public void handleMatchUriTemplateVariables() { RequestMappingInfo key = RequestMappingInfo.paths("/{path1}/{path2}").build(); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2"); String lookupPath = new UrlPathHelper().getLookupPathForRequest(request); this.handlerMapping.handleMatch(key, lookupPath, request); String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE; Map<String, String> uriVariables = (Map<String, String>) request.getAttribute(name); assertNotNull(uriVariables); assertEquals("1", uriVariables.get("path1")); assertEquals("2", uriVariables.get("path2")); }
@Test public void getMappingPathPatterns() throws Exception { String[] patterns = {"/foo/*", "/foo", "/bar/*", "/bar"}; RequestMappingInfo info = RequestMappingInfo.paths(patterns).build(); Set<String> actual = this.handlerMapping.getMappingPathPatterns(info); assertEquals(new HashSet<>(Arrays.asList(patterns)), actual); }
@Test public void compareToWithImpicitVsExplicitHttpMethodDeclaration() { RequestMappingInfo noMethods = paths().build(); RequestMappingInfo oneMethod = paths().methods(GET).build(); RequestMappingInfo oneMethodOneParam = paths().methods(GET).params("foo").build(); Comparator<RequestMappingInfo> comparator = (info, otherInfo) -> info.compareTo(otherInfo, new MockHttpServletRequest()); List<RequestMappingInfo> list = asList(noMethods, oneMethod, oneMethodOneParam); Collections.shuffle(list); Collections.sort(list, comparator); assertEquals(oneMethodOneParam, list.get(0)); assertEquals(oneMethod, list.get(1)); assertEquals(noMethods, list.get(2)); }