Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help converting from web.xml spring boot

I am trying to convert a web.xml based web app to spring boot but am having trouble configuring an HttpRequestHandlerServlet. I have the following in my web.xml:

<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>

The 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't figure out how to configure updateServlet.

How does one configure an HttpRequestHandlerServlet based servlet in a Spring Boot application?

Additional Info:

I tried the suggested answer but it doesn't work for me.

One thing that I didn't mention is that UpdateServlet is named "updateServlet": Componenet("updateServlet") public class UpdateServlet implements HttpRequestHandler

That name conflicts with the bean name in the answer. After changing it (to update), 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]
like image 551
Dan Ciarniello Avatar asked Jun 19 '15 05:06

Dan Ciarniello


People also ask

Does Spring boot application need web xml?

Spring MVC web applications use the web. xml file as a deployment descriptor file. Also, it defines mappings between URL paths and the servlets in the web.

Does Spring boot have DispatcherServlet?

In Spring MVC all incoming requests go through a single servlet is called Dispatcher Servlet (front controller). The front controller is a design pattern in web application development. A single servlet receives all the request and transfers them to all other components of the application.

What is DispatcherServlet and ContextLoaderListener?

Basic. The task of the DispatcherServlet is to send request to the specific Spring MVC controller. ContextLoaderListener reads the Spring configuration file (with value given against contextConfigLocation in web.xml ), parses it and loads the singleton bean defined in that config file.

What is the difference between POM xml and web xml?

The pom. xml is for configure your project with Maven. The web. xml is use in all Java EE project under Tomcat for example.


2 Answers

For the DispatcherServlet there is a much easier way simply add a line to the application.properties and remove the servlet bean from your application class.

server.servlet-path=/ws/*

Next just add your definition of the HttpRequestHandlerServlet to your configuration instead of the 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:

Note, that using updateServlet() method is absolutely legal here, and can be used to get SpringBean instance (see comments below).

like image 96
M. Deinum Avatar answered Sep 29 '22 19:09

M. Deinum


You probably want to have a servlet as a Spring bean (or at least this is the scope of the HttpRequestHandlerServlet). The problem is in 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 haven't found a way of setting the servlet name with java config as you could do in web.xml, so in this case the servlet name will be the default "httpRequestHandlerServlet".

You can have only one servlet as a Spring bean and you have to give it this name. If you want multiple servlets like this, then you need to find another way. Assume 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");
    }
}
like image 34
Cosmin Vasii Avatar answered Sep 29 '22 20:09

Cosmin Vasii