【问题标题】:Can I represent a runnable Spring boot restlet application as a servlet?我可以将可运行的 Spring boot restlet 应用程序表示为 servlet 吗?
【发布时间】:2016-01-19 06:16:51
【问题描述】:

我有一个运行良好的 Spring Boot restlet 应用程序,它目前是一个独立的可执行文件,启动代码不多:

@SpringBootApplication
@EnableJpaRepositories(basePackageClasses = { MelonRepository.class })
@EnableTransactionManagement
public class Runner {

  public static void main(String[] args) {
    SpringApplication.run(Runner.class, args);
  }
}

我也有一些看起来像的restlets

@RestController
@RequestMapping(value = "/here", produces = "application/json")
public class TheController {

  @Autowired
  protected Business processor;

  @RequestMapping(value = "/{accoId}")
  public Something findSomethingByAccoId(@PathVariable String id) {

  (...)

目前我没有自己的 XML,一切都是使用注解配置的。

然而,虽然该应用程序可以很好地独立运行,但我需要使其成为旧版 .war 应用程序的一部分,以便可以使用共享 web.xml 中的其他(旧版)servlet 进行配置(已经存在)。

如何将 Spring Boot restlet 包装成一个“普通”的 servlet,以便与其他“普通”的 servlet 一起部署?

【问题讨论】:

  • 使用独立的 Spring MVC 和一个简单的 WebApplicationInitializer 来配置你的应用程序。

标签: java spring servlets web.xml legacy


【解决方案1】:

这样做的方法是通过org.springframework.web.servlet.DispatcherServlet,这是一个正是为此目的的servlet。如果你想通过注解来配置它,对应的 web.xml 片段看起来像

<servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
           com.mine.config.WebConfiguration
    </param-value>
    </init-param>
</servlet>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-core-config.xml</param-value>
</context-param>

其中 spring-core-config.xml 可能包含额外的 context:component 标签(用于扫描所需注释组件的包)或消息转换器或其他任何东西。

com.mine.config.WebConfiguration 被假定为您的基于注释的 Web 配置类。

【讨论】:

    猜你喜欢
    • 2016-01-08
    • 2016-05-03
    • 2017-12-13
    • 2017-02-10
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 2018-05-02
    • 2014-11-17
    相关资源
    最近更新 更多