Spring boot MessageDispatcherServlet overriding DispatcherServlet. how to skip overriding and register both DispatcherServlet? - spring

in a single application, I m trying to use soap as well as rest web service. and each servlet is given different URI, below is the code of config class.
The question is: only soap service URL is working fine but for the rest on getting 405 error.
#EnableWs
#Configuration
public class WebServiceConfig {
#Bean
public ServletRegistrationBean RsRegistrationBean(ApplicationContext applicationContext) {
DispatcherServlet servlet = new DispatcherServlet();
servlet.setApplicationContext(applicationContext);
return new ServletRegistrationBean(servlet,"/rest/*");
}
#Bean
public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(
ApplicationContext context) {
MessageDispatcherServlet messageDispatcherServlet = new MessageDispatcherServlet();
messageDispatcherServlet.setApplicationContext(context);
messageDispatcherServlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean<>(messageDispatcherServlet,"/");
}
}

Remove your WebServiceConfig class, as both are auto-configured by Spring Boot already (as of Spring Boot 1.4). Add the following to your application.properties
spring.mvc.servlet.path=/rest
spring.webservices.path=/
Now you leverage the Spring Boot proivded infrastructure instead of fighting with it.

Related

ServletRegistrationBean not working with Spring

I'm trying to register servlets using ServletRegistrationBean in Spring (not Spring Boot) using following code:
#configuration
public class MyConfiguration {
#Bean
public ServletRegistrationBean<HttpServlet> customServlet() {
ServletRegistrationBean<HttpServlet> bean = new ServletRegistrationBean<>(new CustomServlet(), "/custom");
bean.setLoadOnStartUp(1);
return bean;
}
}
I have confirmed the bean is being created, and the servlet CustomServlet is not registered. BTW, the web.xml registers some other servlets and they are fully functional.
What could be the cause of this?
ServletRegistrationBean can only work with Spring Boot?
ServletRegistrationBean cannot mix with web.xml?
I missed something?
I googled this but couldn't find any related question.

Avoid automatic servlet mapping for a Servlet created as a #Bean in Spring Boot

I have a servlet to which I need to supply dependencies via autowiring (it's a class from an external library, I cannot change its code). I try to register it as a bean and later register it using programmatic registration (ServletContextInitializer). Here is what I have:
#Configuration
public class MyConfiguration {
#Bean
public MyServlet myServlet() {
return new MyServlet();
}
}
Also, SpringMVC-related autoconfiguration creates a usual DispatcherServlet and maps it at /.
When I try to start the application, I get the following:
Caused by: java.lang.IllegalStateException: Multiple servlets map to path /: dispatcherServlet[mapped:JAVAX_API:null],myServlet[mapped:JAVAX_API:null]
So it looks like Spring Boot (or Spring itself?) automatically maps the servlet at the default /. I would like to avoid the mapping at all as I just need to create the servlet instance; I will register it myself later.
Can this be done?
you should use a ServletRegistrationBean then you can provide an extra mapping
#Bean
public MyServlet myServlet() {
return new MyServlet();
}
#Bean
public ServletRegistrationBean myServletRegistration(MyServlet myServlet) {
    ServletRegistrationBean registration = new ServletRegistrationBean(myServlet,
"/myservlet/*");
registration.setLoadOnStartup(1);
    return registration;
}

not able to initialize the init methof of servlet from spring boot

i have a custom servlet initialized in my spring boot app.
#Bean
public ServletRegistrationBean<CustomServlet> servletRegistrationBean()
{
log.info("going to initialise the servlet");
return new ServletRegistrationBean<>(new CustomServlet(), “/path1/*");
}
This is inside a configuration classes annotated with #Configuration, whatever i do spring does not call the init method inside the CustomServlet, although the above log is printed
This code was copied from another sample app, where exact same thing is done, but there, the servlet methods init is initialised, Any thoughts ?
spring boot starter parent version:
2.0.4.RELEASE
Try this;
#Bean
public ServletRegistrationBean exampleServletBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(new CustomServlet(), "/exampleServlet/*");
bean.setLoadOnStartup(1);
return bean;
}
and check this out, it may help : https://www.baeldung.com/register-servlet

How to find the togglz url when integrating with spring-boot and jersey

My application based on spring-boot and jersey. I have configured togglz in my application. I can successfully launch my web application but running gradle bootRun. From the output during the startup, I am able to see below log message. But I am not able to access http://localhost:8080/togglz. My application root path is "/" which is managed by jersey. It seems that spring-mvc works well with togglz but failed to access it when integrating with jersey. What should I do in order to let jersey to accept the url?
2016-03-30 18:40:35.191 INFO 81748 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/togglz || /togglz.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
For Jersey 2 to work along with Spring MVC endpoints in a Spring Boot application, I would suggest to make sure your application.yml (or .properties makes such distinction, something like:
...
# Spring MVC dispatcher servlet path. Needs to be different than Jersey's to enable/disable Actuator endpoints access (/info, /health, ...)
server.servlet-path: /
# Jersey dispatcher servlet
spring.jersey.application-path: /api
...
You could read more about this at my blog: http://tech.asimio.net/2016/04/05/Microservices-using-Spring-Boot-Jersey-Swagger-and-Docker.html#implement-api-endpoints-using-jersey
If you are integrating Jersey 1 with Spring MVC endpoints in a Spring Boot app, Spring Boot doesn't provide a jersey 1 starter so everything needs to be "manually" configured, but basically if your Jersey servlet is mapped to "/", you would need to configure it to let pass 404 to the servlet container for further handling (maybe a Spring MVC or plain servlet endpoint). Something like:
Jersey 1 resources configuration:
#ApplicationPath("/")
public class DemoResourcesConfig extends PackagesResourceConfig {
private static final Map<String, Object> properties() {
Map<String, Object> result = new HashMap<>();
result.put(PackagesResourceConfig.PROPERTY_PACKAGES, "com.sun.jersey;com.asimio.api.demo1.rest");
// To forward non-Jersey paths to servlet container for Spring Boot actuator endpoints to work.
result.put("com.sun.jersey.config.feature.FilterForwardOn404", "true");
result.put(JSONConfiguration.FEATURE_POJO_MAPPING, "true");
return result;
}
public DemoResourcesConfig() {
super(properties());
}
...
}
Jersey 1 resource implementation:
package com.asimio.api.demo1.rest;
...
#Component
#Path("/actors")
#Produces(MediaType.APPLICATION_JSON)
public class ActorResource {
#GET
public List<Actor> findActors() {
...
}
#GET
#Path("{id}")
public Actor getActor(#PathParam("id") String id) {
...
}
...
}
ServletContextInitializer bean
#Bean
public ServletContextInitializer servletInitializer() {
return new ServletContextInitializer() {
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
final ServletRegistration.Dynamic appServlet = servletContext.addServlet("jersey-servlet", new SpringServlet());
Map<String, String> filterParameters = new HashMap<>();
// Set filter parameters
filterParameters.put("javax.ws.rs.Application", "com.asimio.api.demo1.config.DemoResourcesConfig");
appServlet.setInitParameters(filterParameters);
appServlet.setLoadOnStartup(2);
appServlet.addMapping("/*");
}
};
}
application.yml
...
# For Spring MVC to enable Endpoints access (/admin/info, /admin/health, ...) along with Jersey
server.servlet-path: /admin
...
More about Jersey 1 and Spring Boot / Cloud could be found at my blog: http://tech.asimio.net/2016/11/14/Microservices-Registration-and-Discovery-using-Spring-Cloud-Eureka-Ribbon-and-Feign.html#create-the-demo-service-1

Spring Boot with multiple DispatcherServlet, each having their own #Controllers

Basically I want to split my application into 2 parts. Each part has it's own security stuff and own #Controllers. The #Services should be accessible from both parts.
So I thought, I should get 2 DispatcherServlet. One listening to /admin/* and the second listening to everything else ( / ). Each of those will have its own AnnotationConfigWebApplicationContext so I can have separate component scan for the #Controllers.
And because Spring Boot provides one DispatcherServlet listening on / out of the box, I thought, I can just add a second one:
#Configuration
public class MyConfig {
#Bean(name="myDS")
public DispatcherServlet myDS(ApplicationContext applicationContext) {
AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
webContext.setParent(applicationContext);
webContext.register(MyConfig2.class);
// webContext.refresh();
return new DispatcherServlet(webContext);
}
#Bean
public ServletRegistrationBean mySRB(#Qualifier("myDS") DispatcherServlet dispatcherServlet) {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet);
servletRegistrationBean.addUrlMappings("/admin/*");
servletRegistrationBean.setName("adminServlet");
return servletRegistrationBean;
}
}
The MyConfig2 class, only has #Configuration and #ComponentScan. Within the same package is a #Controller.
When starting the application, I can see, that the second servlet mapping is getting registered, but the #Controller is not. Additionally I can now access all #Controllers from / and /admin.
Any idea how I can get this working?
I got it working somehow!
Here's my Package Layout:
test.foo.
FooConfig.java
FooController.java
test.bar.
BarConfig.java
BarController.java
test.app.
Application.java
MyService.java
src/main/resources/application.properties
Application.java:
#SpringBootApplication(exclude=DispatcherServletAutoConfiguration.class)
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
#Bean
public ServletRegistrationBean foo() {
DispatcherServlet dispatcherServlet = new DispatcherServlet();
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(FooConfig.class);
dispatcherServlet.setApplicationContext(applicationContext);
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet, "/foo/*");
servletRegistrationBean.setName("foo");
return servletRegistrationBean;
}
#Bean
public ServletRegistrationBean bar() {
DispatcherServlet dispatcherServlet = new DispatcherServlet();
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(BarConfig.class);
dispatcherServlet.setApplicationContext(applicationContext);
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet, "/bar/*");
servletRegistrationBean.setName("bar");
return servletRegistrationBean;
}
}
The exclude does prevent Spring Boot from creating its own DispatcherServlet with / mapping. You can remove that line, if you want that mapping or define your own.
You can add servletRegistrationBean.setLoadOnStartup(1) if you want to have your Servlets initialized on application start. Else it will wait for the first request for that servlet.
It's important to set servletRegistrationBean.setName(...), else the servlets will override each other.
FooConfig.java & BarConfig.java:
#Configuration #ComponentScan #EnableWebMvc
public class FooConfig { }
#EnableWebMvc will enable the component scan. Without it, it won't find the #Controller class.
The Controller and Service code is not important. You just have to know, that if you have #RequestMapping("/foo") inside FooController, the request must be GET /foo/foo because the Servlet's URL mapping is /foo/*. It's not possible to call the URL GET /foo because the Servlet URL mapping needs a / at the end of its path (in other words: GET /foo will look for a Servlet with / mapping!), though #RequestMapping("") must be called via GET /foo/. And of course it was not possible to use /foo or /foo* as Servlet mapping (or I just did not find the correct settings for that)
Scope: The Controllers can't see each other, though it's not possible to #Autowired them in each other. Also the Service can't #Autowired any of the Controllers. But the Controllers can #Autowired the Service.
Though it's a classical parent child context hierarchy.
The only "bad" thing is, that we need #EnableMvcConfig and don't get the auto configured sugar from Spring boot within the context. The parent context is getting auto configured. I put some database stuff within the application.properties and did a query inside MyService which got called by FooController and it worked flawlessly! :)
I hope this may help some people!

Resources