I'm investigating a Spring Boot project generated by JHipster and found out that its request mappings aren't done via web.xml
nor via Spring's @RequestMapping
but like so:
ServletRegistration.Dynamic someServlet =
servletContext.addServlet("someServlet", new SomeServlet());
someServlet.addMapping("/someUrl");
someServlet.setAsyncSupported(true);
My questions are:
someServlet.setAsyncSupported(true)
just another way of making response.setHeader("Access-Control-Allow-Origin", "*")
?ServletRegistration.DynamicInterface through which a Servlet registered via one of the addServlet methods on ServletContext may be further configured.
setLoadOnStartup(int loadOnStartup) Sets the loadOnStartup priority on the Servlet represented by this dynamic ServletRegistration. void. setMultipartConfig(MultipartConfigElement multipartConfig) Sets the MultipartConfigElement to be applied to the mappings defined for this ServletRegistration .
- Is there any reasonable advantages of dynamic registration instead of classic mapping?
Dynamic servlet registration Servlet 3+ way of registering servlets. In Servlets 3 you can avoid creating web.xml
and configure application in pure Java. It gives you some advantages like compile time check if everything is fine there and what's more important since you do it in Java code, you can do some additional checks or conditions - for example register particular servlet only if environment property is set or class is available on the classpath.
It's not a replacement for @RequestMapping
. In case of Spring Boot you will use it most probably when you want to register some 3rd party servlet - like Dropwizard Metrics servlet in case of JHipster.
- Is it spring-boot's standard of registering mappings or it's just a will of jhipster's owner?
There are at least 2 ways of registering additional servlets in Spring Boot. See answers here: How can I register a secondary servlet with Spring Boot?.
Your own controllers you map as usual with @RequestMapping
.
- Is someServlet.setAsyncSupported(true) just another way of making response.setHeader("Access-Control-Allow-Origin", "*")?
Nope. For setting this header you use usually CORSFilter (read more: Enabling Cross Origin Requests for a RESTful Web Service). asyncSupported
flag is used to make servlet able to process request asynchronously.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With