Need help converting from web.xml spring boot

I am trying to convert a web.xml based web application to boot spring, but I am having trouble setting up the HttpRequestHandlerServlet. In my web.xml, I have the following:

<servlet> <servlet-name>webServices</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>updateServlet</servlet-name> <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>updateServlet</servlet-name> <url-pattern>/update</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>webServices</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> 

DispatcherServlet was not a problem:

 @SpringBootApplication public class WebApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(PoolWebApplication.class); } @Bean public DispatcherServlet dispatcherServlet() { return new DispatcherServlet(); } @Bean public ServletRegistrationBean servletRegistrationBean() { return new ServletRegistrationBean(dispatcherServlet(), "/ws/*"); } } 

But I can not figure out how to configure updateServlet.

How to configure servlet in HttpRequestHandlerServlet in spring boot application?

Additional Information:

I tried the suggested answer, but it does not work for me.

One thing that I haven't mentioned is that the UpdateServlet is called "updateServlet": Componenet ("updateServlet") The public UpdateServlet class implements the HttpRequestHandler

This name conflicts with the name bean in the response. After changing it (for updating) I get:

 No bean named 'httpRequestHandlerServlet' is defined 

After changing the name of updateServlet to httpRequestHandlerServlet I get

 org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'httpRequestHandlerServlet' must be of type [org.springframework.web.HttpRequestHandler], but was actually of type [org.springframework.web.context.support.HttpRequestHandlerServlet] 
+7
java spring spring-boot spring-mvc
source share
3 answers

For DispatcherServlet much easier to just add a line to application.properties and remove the bean servlet from your application class.

 server.servlet-path=/ws/* 

Then just add your HttpRequestHandlerServlet definition to your configuration instead of DispatcherServlet .

 @SpringBootApplication public class WebApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(PoolWebApplication.class); } @Bean public HttpRequestHandlerServlet updateServlet() { return new HttpRequestHandlerServlet(); } @Bean public ServletRegistrationBean updateServletRegistrationBean() { return new ServletRegistrationBean(updateServlet(), "/update"); } } 

UPD:

Please note that using the updateServlet() method is absolutely legal here and can be used to get an instance of SpringBean (see comments below).

+3
source share

You probably want to have a servlet like a Spring bean (or at least this is the scope of the HttpRequestHandlerServlet). The problem is the init method of this class

 @Override public void init() throws ServletException { WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); this.target = wac.getBean(getServletName(), HttpRequestHandler.class); } 

I did not find a way to set the servlet name using java config, as you could do in web.xml, so in this case the servlet name will default to "httpRequestHandlerServlet".

You can only have one servlet as a Spring bean, and you must give it that name. If you want to use multiple servlets, then you need to find another way. Suppose you have this servlet:

 public class MySpringBeanServlet implements org.springframework.web.HttpRequestHandler{ @Autowired private SomeBean someBean; @Override public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //do your magic here } } @SpringBootApplication public class WebApplication extends SpringBootServletInitializer{ public static void main(String[] args){ SpringApplication.run(PoolWebApplication.class); } /** * This is the key, setting the bean name **/ @Bean(name="httpRequestHandlerServlet") public HttpRequestHandler mySpringBeanServlet(){ return new MySpringBeanServlet(); } @Bean public ServletRegistrationBean updateServletRegistrationBean(){ return new ServletRegistrationBean(new HttpRequestHandlerServlet();, "/update"); } } 
+1
source share

You can easily set the bean name using the following code (note the setName () method)

 @Bean public HttpRequestHandler image() { return new ImageServlet(); } @Bean public ServletRegistrationBean imageServletRegistrationBean() { ServletRegistrationBean imageServlet = new ServletRegistrationBean( new HttpRequestHandlerServlet(), "/image/*"); imageServlet.setName("image"); return imageServlet; } 

You may have several custom servlet paths specified.

+1
source share

All Articles