-->

Spring 3 RequestMapping: Get path value

2019-01-01 10:12发布

问题:

Is there a way to get the complete path value after the requestMapping @PathVariable values have been parsed?

That is: /{id}/{restOfTheUrl} should be able to parse /1/dir1/dir2/file.html into id=1 and restOfTheUrl=/dir1/dir2/file.html

Any ideas would be appreciated.

回答1:

Non-matched part of the URL is exposed as a request attribute named HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE:

@RequestMapping(\"/{id}/**\")
public void foo(@PathVariable(\"id\") int id, HttpServletRequest request) {
    String restOfTheUrl = (String) request.getAttribute(
        HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    ...
}


回答2:

Just found that issue corresponding to my problem. Using HandlerMapping constants I was able to wrote a small utility for that purpose:

/**
 * Extract path from a controller mapping. /controllerUrl/** => return matched **
 * @param request incoming request.
 * @return extracted path
 */
public static String extractPathFromPattern(final HttpServletRequest request){


    String path = (String) request.getAttribute(
            HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    String bestMatchPattern = (String ) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);

    AntPathMatcher apm = new AntPathMatcher();
    String finalPath = apm.extractPathWithinPattern(bestMatchPattern, path);

    return finalPath;

}


回答3:

This has been here quite a while but posting this. Might be useful for someone.

@RequestMapping( \"/{id}/**\" )
public void foo( @PathVariable String id, HttpServletRequest request ) {
    String urlTail = new AntPathMatcher()
            .extractPathWithinPattern( \"/{id}/**\", request.getRequestURI() );
}


回答4:

I have used the Tuckey URLRewriteFilter to handle path elements that contain \'/\' characters, as I don\'t think Spring 3 MVC supports them yet.

http://www.tuckey.org/

You put this filter in to your app, and provide an XML config file. In that file you provide rewrite rules, which you can use to translate path elements containing \'/\' characters into request parameters that Spring MVC can deal with properly using @RequestParam.

WEB-INF/web.xml:

<filter>
  <filter-name>UrlRewriteFilter</filter-name>
  <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<!-- map to /* -->

WEB-INF/urlrewrite.xml:

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<!DOCTYPE urlrewrite
    PUBLIC \"-//tuckey.org//DTD UrlRewrite 3.0//EN\"
    \"http://tuckey.org/res/dtds/urlrewrite3.0.dtd\">
<urlrewrite>
  <rule>
    <from>^/(.*)/(.*)$</from>
    <to last=\"true\">/$1?restOfTheUrl=$2</to>
</urlrewrite>

Controller method:

@RequestMapping(\"/{id}\")
public void handler(@PathVariable(\"id\") int id, @RequestParam(\"restOfTheUrl\") String pathToFile) {
  ...
}


回答5:

Yes the restOfTheUrl is not returning only required value but we can get the value by using UriTemplate matching.

I have solved the problem, so here the working solution for the problem:

@RequestMapping(\"/{id}/**\")
public void foo(@PathVariable(\"id\") int id, HttpServletRequest request) {
String restOfTheUrl = (String) request.getAttribute(
    HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    /*We can use UriTemplate to map the restOfTheUrl*/
    UriTemplate template = new UriTemplate(\"/{id}/{value}\");        
    boolean isTemplateMatched = template.matches(restOfTheUrl);
    if(isTemplateMatched) {
        Map<String, String> matchTemplate = new HashMap<String, String>();
        matchTemplate = template.match(restOfTheUrl);
        String value = matchTemplate.get(\"value\");
       /*variable `value` will contain the required detail.*/
    }
}


回答6:

Here is how I did it. You can see how I convert the requestedURI to a filesystem path (what this SO question is about). Bonus: and also how to respond with the file.

@RequestMapping(value = \"/file/{userId}/**\", method = RequestMethod.GET)
public void serveFile(@PathVariable(\"userId\") long userId, HttpServletRequest request, HttpServletResponse response) {
    assert request != null;
    assert response != null;

    // requestURL:  http://192.168.1.3:8080/file/54/documents/tutorial.pdf
    // requestURI:  /file/54/documents/tutorial.pdf
    // servletPath: /file/54/documents/tutorial.pdf
    // logger.debug(\"requestURL: \" + request.getRequestURL());
    // logger.debug(\"requestURI: \" + request.getRequestURI());
    // logger.debug(\"servletPath: \" + request.getServletPath());

    String requestURI = request.getRequestURI();
    String relativePath = requestURI.replaceFirst(\"^/file/\", \"\");

    Path path = Paths.get(\"/user_files\").resolve(relativePath);
    try {
        InputStream is = new FileInputStream(path.toFile());  
        org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());
        response.flushBuffer();
    } catch (IOException ex) {
        logger.error(\"Error writing file to output stream. Path: \'\" + path + \"\', requestURI: \'\" + requestURI + \"\'\");
        throw new RuntimeException(\"IOError writing file to output stream\");
    }
}


回答7:

You need to use built-in pathMatcher:

@RequestMapping(\"/{id}/**\")
public void test(HttpServletRequest request, @PathVariable long id) throws Exception {
    ResourceUrlProvider urlProvider = (ResourceUrlProvider) request
            .getAttribute(ResourceUrlProvider.class.getCanonicalName());
    String restOfUrl = urlProvider.getPathMatcher().extractPathWithinPattern(
            String.valueOf(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)),
            String.valueOf(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)));


回答8:

private final static String MAPPING = \"/foo/*\";

@RequestMapping(value = MAPPING, method = RequestMethod.GET)
public @ResponseBody void foo(HttpServletRequest request, HttpServletResponse response) {
    final String mapping = getMapping(\"foo\").replace(\"*\", \"\"); 
    final String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    final String restOfPath = url.replace(mapping, \"\");
    System.out.println(restOfPath);
}

private String getMapping(String methodName) {
    Method methods[] = this.getClass().getMethods();
    for (int i = 0; i < methods.length; i++) {
        if (methods[i].getName() == methodName) {
            String mapping[] = methods[i].getAnnotation(RequestMapping.class).value();
            if (mapping.length > 0) {
                return mapping[mapping.length - 1];
            }
        }
    }
    return null;
}


回答9:

I have a similar problem and I resolved in this way:

@RequestMapping(value = \"{siteCode}/**/{fileName}.{fileExtension}\")
public HttpEntity<byte[]> getResource(@PathVariable String siteCode,
        @PathVariable String fileName, @PathVariable String fileExtension,
        HttpServletRequest req, HttpServletResponse response ) throws IOException {
    String fullPath = req.getPathInfo();
    // Calling http://localhost:8080/SiteXX/images/argentine/flag.jpg
    // fullPath conentent: /SiteXX/images/argentine/flag.jpg
}

Note that req.getPathInfo() will return the complete path (with {siteCode} and {fileName}.{fileExtension}) so you will have to process conveniently.