Dec 12, 2013

Spring @RequestMethod Double Slashes

Ran into a painful issue with Spring-3.0.5's @RequestMethod not handling double slashes. It absorbs single slashes in urls as well!

package com.jostens.spring3template.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Test of spring's handling of double slashes
 * 
 * @see http://webmasters.stackexchange.com/questions/8354/what-does-the-double-slash-mean-in-urls
 */
@Controller
public class SlashTestController {
 @RequestMapping(value = "/productDetail/{param1}/{param2}/{param3}", method = RequestMethod.GET)
 @ResponseBody
 public String slashTest1(HttpServletRequest request,
   @PathVariable String param1, @PathVariable String param2,
   @PathVariable String param3) throws Exception {
  return "slashTest1\n"
   + request.getServletPath() + "\n"
   + "param1[" + param1 + "]\n" + "param2[" + param2 + "]\n" + "param3[" + param3 + "]\n";
 }

 @RequestMapping(value = "/productDetail/{param1}//{param3}", method = RequestMethod.GET)
 @ResponseBody
 public String slashTest2(HttpServletRequest request,
   @PathVariable String param1, @PathVariable String param3)
   throws Exception {
  String param2 = "-";
  return "slashTest2\n"
   + request.getServletPath() + "\n"
   + "param1[" + param1 + "]\n" + "param2[" + param2 + "]\n" + "param3[" + param3 + "]\n";
 }

 @RequestMapping(value = "/productDetail/{param1}/{param2}", method = RequestMethod.GET)
 @ResponseBody
 public String slashTest3(HttpServletRequest request,
   @PathVariable String param1, @PathVariable String param2)
   throws Exception {
  return "slashTest3\n"
   + request.getServletPath() + "\n"
   + "param1[" + param1 + "]\n" + "param2[" + param2 + "]\n";
 }
}

Logged as SPR-11220 RequestMapping maps double-slashes to single slashes.

No comments:

Post a Comment