需要帮助从web.xml spring boot转换

时间:2015-06-19 05:57:44

标签: java spring spring-mvc spring-boot

我正在尝试将基于web.xml的Web应用程序转换为spring boot但是在配置HttpRequestHandlerServlet时遇到问题。我在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>

DispatcherServlet不是问题:

@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/*");
  }
}

但我无法弄清楚如何配置updateServlet。

如何在Spring Boot应用程序中配置基于HttpRequestHandlerServlet的servlet?

其他信息:

我尝试了建议的答案,但它对我没用。

我没有提到的一件事是UpdateServlet被命名为&#34; updateServlet&#34;:     组元(&#34; updateServlet&#34)     公共类UpdateServlet实现HttpRequestHandler

该名称与答案中的bean名称冲突。更改后(更新),我得到:

No bean named 'httpRequestHandlerServlet' is defined

将updateServlet的名称更改为httpRequestHandlerServlet后,我得到了

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]

3 个答案:

答案 0 :(得分:3)

对于DispatcherServlet,只需向application.properties添加一行并从应用程序类中删除servlet bean就可以轻松实现。

server.servlet-path=/ws/*

接下来只需将HttpRequestHandlerServlet的定义添加到您的配置中,而不是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:

注意,使用updateServlet()方法在这里绝对合法,可用于获取SpringBean实例(请参阅下面的注释)。

答案 1 :(得分:1)

您可能希望将servlet作为Spring bean(或者至少这是HttpRequestHandlerServlet的范围)。问题出在这个类的init方法

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

我没有像在web.xml中那样找到用java配置设置servlet名称的方法,所以在这种情况下,servlet名称将是默认的“httpRequestHandlerServlet”。

你只能有一个servlet作为Spring bean,你必须给它这个名字。如果你想要这样的多个servlet,那么你需要找到另一种方法。假设你有这个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");
    }
}

答案 2 :(得分:1)

您可以使用下面的代码轻松设置bean名称(请注意setName()方法)

@Bean
  public HttpRequestHandler image() {
    return new ImageServlet();
  }

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

您可以通过这种方式声明多个自定义servlet路径。