-
Notifications
You must be signed in to change notification settings - Fork 212
Configuring Servlets
Microserver services can all run in true Microservice stand-alone style, or Microserver allows multiple otherwise independent services to be bundled together at runtime to act as a 'micro'-monolith.
Autodiscovered Servlets, are discovered within the shared Spring context, and thus (currently) become globally available to all bundled microservices. If you are not bundling services - this is not significant, but if you are, or are planning to - it is something to be mindful of.
Servlets can be configured locally via the Module interface (when you start your Microservice),or by the Plugin interface.
These Servlets will be available on all Microservices sharing a core Spring context.
In order to automagically add a Servlet to your Microservice, implement the interface ServletConfiguration on a Spring bean. If the bean implementing ServletConfiguration is also a Servle only the method getMapping() need be implemented. This will be the context URL of your Servlet.
public Class<? extends Servlet> getServlet()
Override the above method to configure a specified Servlet class. Typically used for Servlets in external jar files.
public String getName()
To change the Servlet name to something other than the Class name.
public Map<String,String> getInitParameters()
To configure the init parameters for the Servlet
public Map<String,String> getInitParameters(){
return ImmutableMap.of();
}
@Component
public class AutodiscoveredServlet extends HttpServlet
implements ServletConfiguration {
@Override
public String[] getMapping() {
return new String[] { "/servlet" };
}
@Override
protected void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
resp.getWriter().write("hello world");
}
}
#Configuring Local Servlets
These Servlets will only be available on the Microservice modules they are configured against.
In your Module object override
public Map<String,Servlet> getServlets()
To configure module local servlets and their contexts Urls .
public Class<? extends ServletConfiguration>[] getServletConfigurationClass()
Servlets can be configured inside a plugin both as explicitly defined Servlets, or as auto discovered Spring Beans that implement ServletConfiguration.
Override the filters method to provide a lookup function for the Servlets
public Function<ServerData,Map<String,Servlet>> servlets(){
return serverData -> myMapOfFilters;
}
or override springClasses and add your ServletConfiguration Spring beans there
public Set<Class> springClasses(){
return new HashSet<>();
}