Friday, August 17, 2012

Spring MVC - Convention over Configuration w/o Confusion?

Convention over configuration is an essential feature of Spring MVC and it does save some unnecessary coding effort, but I have more than one case where it can get really messy debugging issues.
Here is a simple example: URL app/contact - worked before invoking list method as the only GET method w/o any method level mapping - catch all after the controller mapping.

Beginning 3.1 it will not & throws a 404. But app/contact/list works & will invoke this GET method (IRRESPECTIVE OF THE METHOD NAME), as long it is the only catch-all GET method.

So you could even name it as fooBar and the same URL will invoke it. If however you get confident that the catch-all GET method will always be called for the GET and the URL won't matter, you are right but then the RequestToViewNameMapping goes out the window for the desired view; instead it will look for foorBar.jsp
Something to watch out for!
@Controller
@RequestMapping("contact")
public class ContactController{

@RequestMapping(method = RequestMethod.GET)
 public List<contact> list() {
  logger.info("default method list");
  return contactService.list();
 }
}
Note the mapping generated for this method: INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/contact/*],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.List com.aj.recipes.web.ContactController.list()

No comments: