I'm just new into Spring Framework tech and I'm making a website with few WebServices(Webstore, social media site etc.) included. What I want for it, is that every webservice would have it owns DispatcherServlet with own ViewResolver. So the structure of my projects looks like:
- The Main site - DispatcherServlet #1 mapping only to http://example.com (yeah, i know i could just use static html site but it's for learning purposes.
- Webservice no.1 e.g Webstore - DispatcherServlet #2 mapping only to /webstore/*
- Webservice no.2 e.g Social media site - DispatcherServlet #3 mapping only to /social/*
In configuration I'm using Java Classes, so what I did was:
- Firstly I've Created AppConfig class that extends
WebMvcConfigureAdapter
and implementsViewResolver
for main site views. Next I've created AnAppInitializer
class which extendsAbstractAnnotationConfigDispatcherServletInitializer
class and inside of it I've configured RootConfigClass and servlet mapping - Secondly I've had problem - namely I couldn't create another class for Dispatcher Servlet #2 (couldn't have two classes which extended
AbstractAnnotationConfigDispatcherServletInitializer
), what I've found was creating class which would implement an interfaceWebApplicationInitializer
. Which I did and finally had working Second DispatcherServlet Thirdly I've reapeted steps from second point and had Three working Dispatcher Servlets(in theory...).
I could access my main site via DispatcherServlet #1
(http://example.com)I could access my webstore main site via DispatcherServlet #2 (http://example.com/webstore)
and I could access my webservice via DispatcherServlet #3(http://example.com/social)
but after all, when I've created another view pointing e.g. to login section (http://example.com/webstore/login) It didn't work at all. The errors said that Dispatcher Servlet #1 couldn't handle mapping request, where the address should be tied with Dispatcher Servlet #2. What I did to resolve this problem was changing The Dispatcher Servlet #2 configuration class part of servlet mapping from servlet.addMapping("/webstore/");
to servlet.addMapping("/webstore/*");
which I've read is not the best idea but It worked.
The problem appears when I would type address that Controller doesn't handle(doesn't exists) like (http://example.com/webstore/thisaddressdoesntexists), the view controller gives me back main default view from http://example.com/webstore, instead of 404 not found page. This is not the behavior I've intented to implement. Besides that little 'feature', everything works fine. All specyfic request are served with their specyfic DispatcherServlet. Finnal questions are:
- How to prevent from loading default view, in this case scenario, when I'm pointing to non-existing url and load error code sites?
- Is my concept even good at all? I mean using classes which implements WebApplicationInitializer for using more DispatcherServlets or there is another way? I'm only talking about Java-Based Config Classes, no xmls.