spring 4.1.6 中通过继承RequestMappingHandlerMapping 实现自定义url访问

新建web项目sp,使用的是spring 4.1.6,实现通过controller类名中除controller之外的字符串和方法名作为访问路径的方式:

例如:TestController 中有方法 public String uuid(){}

方法路径为:http://localhost/sp/service/test@uuid

=======================================================================================

web.xml文件内容如下:

    
    
        controller
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            /WEB-INF/config/spring-servlet.xml
        

        1
    

    
        controller
        /service/*
    



spring-servlet.xml中相关代码如下:

             class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"
        p:ignoreDefaultModelOnRedirect="true">
        
            
                                     class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
            

        

    
    
    




cn.ddx.util.MyRequestMappingHandlerMapping中代码如下:
package cn.ddx.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.condition.ConsumesRequestCondition;
import org.springframework.web.servlet.mvc.condition.HeadersRequestCondition;
import org.springframework.web.servlet.mvc.condition.ParamsRequestCondition;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@RequestMapping
public class MyRequestMappingHandlerMapping extends RequestMappingHandlerMapping {

    private boolean useSuffixPatternMatch = true;

    private boolean useTrailingSlashMatch = true;

    private ContentNegotiationManager contentNegotiationManager = new ContentNegotiationManager();

    private final List fileExtensions = new ArrayList();
    
    private static RequestMapping requestMapping = (RequestMapping) MyRequestMappingHandlerMapping.class
            .getAnnotation(RequestMapping.class);


    @Override
    protected RequestMappingInfo getMappingForMethod(Method method, Class handlerType) {
        RequestMappingInfo info = null;
        if (requestMapping != null) {
            RequestCondition methodCondition = getCustomMethodCondition(method);
            info = createRequestMappingInfo(requestMapping, methodCondition, method);
            RequestMapping typeAnnotation = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class);
            if (typeAnnotation != null) {
                RequestCondition typeCondition = getCustomTypeCondition(handlerType);
                info = createRequestMappingInfo(typeAnnotation, typeCondition, method).combine(info);
            }
        }
        return info;
    }

    protected RequestMappingInfo createRequestMappingInfo(RequestMapping annotation,
            RequestCondition customCondition, Method method) {
        String className = method.getDeclaringClass().getSimpleName();
        String classNameExceptAction = StringUtils.uncapitalize(className.substring(0, className.length() - 10));
        String[] patterns = resolveEmbeddedValuesInPatterns(annotation.value());
        if ((patterns != null)&& (patterns.length == 0)) {//
            patterns = new String[] { classNameExceptAction + "!" + method.getName() };
        }
        System.out.println(patterns[0]);

    
        return new RequestMappingInfo(
                new PatternsRequestCondition(patterns, getUrlPathHelper(), getPathMatcher(), this.useSuffixPatternMatch,
                        this.useTrailingSlashMatch, this.fileExtensions),
                new RequestMethodsRequestCondition(annotation.method()),
                new ParamsRequestCondition(annotation.params()), new HeadersRequestCondition(annotation.headers()),
                new ConsumesRequestCondition(annotation.consumes(), annotation.headers()), new ProducesRequestCondition(
                        annotation.produces(), annotation.headers(), this.contentNegotiationManager),
                customCondition);
    }

}



测试类TestController.java内容如下:

package cn.ddx.controller;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class TestController {

    public ModelAndView uuid(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg", "UUID:" + UUID.randomUUID());
        mv.setViewName("/test");
        return mv;
    }

    public ModelAndView random(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg", "RandomUtils:" + RandomUtils.nextInt(1000, 9999));
        mv.setViewName("/test");
        return mv;
    }
}

页面test内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


${msg}





启动tomcat之后,控制台显示信息如下:

test@random
test@uuid


访问路径如下:

http://localhost/sp/service/test@uuid

spring 4.1.6 中通过继承RequestMappingHandlerMapping 实现自定义url访问_第1张图片

你可能感兴趣的:(Spring)