1
votes

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 implements ViewResolver for main site views. Next I've created An AppInitializer class which extends AbstractAnnotationConfigDispatcherServletInitializer 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 interface WebApplicationInitializer. 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...).

    1. I could access my main site via DispatcherServlet #1
      (http://example.com)

    2. I could access my webstore main site via DispatcherServlet #2 (http://example.com/webstore)

    3. 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.
1

1 Answers

1
votes

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.

For multiple servlets you need to have a single class which implements WebApplicationInitializer and there provide contextLoader class with all beans for the root context and define a separate class with all the beans for each servlet (usualliy the class which extends WebAppConfigurer). You also need to provide a mapping for each individual servlet. Have a look here how it's done as an example (the last 2 responses)

As regards to

 Is my concept even good at all?

Why do you want to have three separate servlets? Are these three different apps bundled together? If that's the case I wouldn't put them in the same package but make three 3 different apps. You can also use a single Dispatchetservlet which maps to every url and have separate controllers for each component. That will reduce the complexity of the whole project. So it could be either way. Without knowing more about what you are trying to do no one can say it's right or wrong.

How to prevent from loading default view, in this case scenario, when I'm pointing to non-existing url and load error code sites?

For error handling page have a look here. You would normally use @ResponseStatus for a specific HTTP error code or @ExceptionHandler for a general purpose error. In both cases you would need to redirect to a default error page.