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.
Related
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.
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;
}
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
I'm integrating Spring Boot with Apache CXF. Everyhthing is OK, but I cannot get the SearchContext to work inside the controllers:
#Context
private SearchContext context;
public #ResponseBody List<Users> getAll(#Context SearchContext context, #RequestParam String search){
....
}
The SearchContext is not correctly injected, as is not created by the SearchContextProvider in the package org.apache.cxf.jaxrs.ext.search.
I'm launching the CXF Servlet with this Bean:
#Bean
public ServletRegistrationBean cxfServlet() {
CXFServlet cxf = new CXFServlet();
ServletRegistrationBean registration = new ServletRegistrationBean(cxf, "/api/*");
return registration;
}
I'm using a Java-based configuration, no XML.
In an XML based configuration, it seems that this tag is required:
<jaxrs:providers>
<bean class="org.apache.cxf.jaxrs.ext.search.SearchContextProvider"/>
</jaxrs:providers>
How can I declare the provider in a Java based configuration of the servlet?
In your main config class, add:
#ComponentScan("org.apache.cxf.jaxrs")
Since the class (SearchContextProvider) is already annotated with #Provider annotation, Spring will be able to pick it up if it is being scanned. Assuming that you are already doing component-scan on your own package, you can just add the jaxrs package like:
#ComponentScan({ "com.something", "org.apache.cxf.jaxrs" })
Although there are a lot of code example on the internet of integrating Spring Boot/Spring and Resteasy, but most of them are out of data or even don't work.
I am look at the latest Resteasy document, try to create a instance of SpringBeanProcessorServletAware in my config bean.
#Bean
public ServletListenerRegistrationBean<ResteasyBootstrap> resteasyBootstrapRegistratio() {
ServletListenerRegistrationBean<ResteasyBootstrap> registration = new ServletListenerRegistrationBean<>();
registration.setListener(new ResteasyBootstrap());
return registration;
}
#Bean
public ServletRegistrationBean resteasyServletRegistratio() {
ServletRegistrationBean registration = new ServletRegistrationBean();
registration.setServlet(new HttpServletDispatcher());
registration.addUrlMappings("/*");
return registration;
}
#Bean
public SpringBeanProcessorServletAware springBeanProcessorServletAware() {
SpringBeanProcessorServletAware springBeanProcessor = new SpringBeanProcessorServletAware();
return springBeanProcessor;
}
But it will throw Nullpoint exception. It seems like the servletContext is required to make SpringBeanProcessorServletAware work.
Then I try to inject servletContext. But the bean SpringBeanProcessorServletAware is being created before ServletContextInitializer finished.
How to make some bean created after ServletContextInitializer is finished?
Am I wrong in do the integration between Spring Boot and Resteasy.