vendredi 20 février 2015

Organizing spring-mvc URL endpoints so they remain refactorable

I'm often working on very large web applications that use spring-mvc and I find that it becomes increasingly difficult to organize my URL endpoints in a way that keeps them refactorable. By convention, my controllers might look like this:



@Controller
@RequestMapping(value = ProtocolController.ROOT_MAPPING)
public class ProtocolController extends BaseController {

public static final String ROOT_MAPPING = "/protocol";

@RequestMapping(value="/management", method = RequestMethod.GET)
public ModelAndView management(){
...
}
}


I might have a view somewhere else in the application that has a link back to the "management" page so that view has to build the URL. In my view I can access the context path via the model (context path was injected into model by BaseController), I can access the next segment of the URL via ProtocolController.ROOT_MAPPING but then the last part of the URL is just a regular ol' String. My choice of view technology is a custom in-house solution that involves writing raw Java to generate HTML:



String url = model.get("contextPath") + ProtocolController.ROOT_MAPPING + "/management"


The problem is that if I ever change the "/management" endpoint, I can't find all the places in my code that reference it. I suppose I could replace the string literal with a static final String just like I do with ROOT_MAPPING but is there a cleaner way? Are there any known design patterns for organizing/referencing endpoints?


Aucun commentaire:

Enregistrer un commentaire