Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC controller - getPathInfo() is null

I have worked servlet that need to convert to Spring MVC controller to have access spring beans etc. Why in normal servlet request.getPathInfo() return not null, but in Spring Controller i get null value ? I know i can use @PathVariable, but wonder why the results of this method is the difference?

@RequestMapping(value = {"/test", "/test/*"})
public void test(HttpServletRequest req, HttpServletResponse res) {

    log.info(req.getPathInfo() == null); // true!

    if (req.getMethod().equalsIgnoreCase("get")) {
        // analogue to doGet...
    } else {
        // analogue to doPost...
    }

}
like image 648
marioosh Avatar asked Nov 10 '11 10:11

marioosh


1 Answers

I think the solution is in the javadoc of getPathInfo()

The extra path information follows the servlet path but precedes the query string and will start with a "/" character.

In case of Spring the servlet path is the full path hence if you call getServletPath() it will always return the full URI and getPathInfo() will return nothing.

like image 164
Peter Szanto Avatar answered Oct 03 '22 08:10

Peter Szanto