PDA

View Full Version : Problem with subclass using @RequestMapping


almeitus
May 6th, 2008, 05:10 PM
Maybe I'm going at this the wrong way, but I have something that works in the 2.5 release, but it no longer works once I upgraded to 2.5.4. I also noticed a lot has changed in AnnotationMethodHandlerAdapter between these two releases.

I'm creating RESTful webservices via SpringMVC. Here is a test case that works in 2.5, but does NOT work with 2.5.4


public class ParentController {

RequestMapping(method = RequestMethod.GET)
public void doGet(
HttpServletRequest req,
HttpServletResponse resp
) throws IOException, ServletException {

resp.sendError(HttpServletResponse.SC_METHOD_NOT_A LLOWED,
"GET is NOT allowed");
}
}



Point here is that parent would have a default impl for all HTTP methods, like not allowed, and children can implement supported methods as appropriate. Here is an example child:


@Controller
@RequestMapping("/child/test")
public class ChildController extends ParentController {

@RequestMapping(method = RequestMethod.GET)
public void doGet(
HttpServletRequest req,
HttpServletResponse resp,
@RequestParam("childId") String childId
) throws IOException, ServletException {

resp.getWriter().write(
"<response>I am GET for childId: " + childId + "</response>");
}
}


You'll notice that "child" also uses @RequestParam which is really slick for grabbing req params, which is one of the reasons I went this route as opposed to using SimpleServletHandlerAdapter and manually pulling the param(s) out of the req. Also, I have full binding support via this route too.

So, can anyone tell me, is this a bug? One other thing, if I take out the @RequestParam in child, it works under 2.5.4.

-Chris

joshk
May 11th, 2008, 04:03 AM
Hi Chris,

Your general problem is that Spring MVC is using the appropriate method with the least method arguments, this is why when you overload the method with the same method signature everything works fine.

One way around this issue is to put this sort of error throwing into an extended annotation method handler adapter?

Hope this helps

Josh

almeitus
May 11th, 2008, 12:58 PM
Thanks for the input joshk. I realize why the overridden method works fine, but was confused as to why when I run this under 2.5 final, the overloaded get* method is called, but under 2.5.4 a 404 is returned.

The problem is that *no* method is being invoked and a 404 error occurs. If you look at this JIRA: http://jira.springframework.org/browse/SPR-4786

the problem seems to stem from the ambiguity of the overloaded get* and the fact that @RequestParam is NOT req'd.

-Chris

almeitus
May 20th, 2008, 10:07 PM
http://jira.springframework.org/browse/SPR-4786?page=com.atlassian.jira.plugin.system.issueta bpanels:all-tabpanel