如何配置Spring-Boot应用程序继续使用RestEasy?

我有一个旧的Web应用程序(纯servlet,没有Spring),我想以fat-jar的forms运行。 这个应用程序提供了很多REST服务。 我不想修改旧代码。
如何配置Spring-Boot应用程序继续使用RestEasy?

您可以使用RESTEasy Spring Boot启动程序。 这是你如何做到的:

添加POM依赖项

将Maven依赖项添加到Spring Boot应用程序pom文件中。

 com.paypal.springboot resteasy-spring-boot-starter 2.1.1-RELEASE runtime  

注册JAX-RS应用程序类

只需将您的JAX-RS应用程序类(Application的子类)定义为Spring bean,它就会自动注册。 请参阅下面的示例。 有关详细信息,请参阅如何使用RESTEasy Spring Boot Starter中的 JAX-RS应用程序注册方法部分。

 package com.test; import org.springframework.stereotype.Component; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @Component @ApplicationPath("/sample-app/") public class JaxrsApplication extends Application { } 

注册JAX-RS资源和提供程序

只需将它们定义为Spring bean,它们就会自动注册。 请注意,JAX-RS资源可以是单例或请求作用域,而JAX-RS提供者必须是单例。

有关项目GitHub页面的更多信息 。

这并不困难。 我只是使用Spring注释从旧的web.xml重写了配置。

 package kjkrol; import org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher; import org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.embedded.ServletContextInitializer; import org.springframework.boot.context.embedded.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import javax.servlet.ServletContextListener; @ComponentScan @EnableAutoConfiguration @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public ServletContextInitializer initializer() { return servletContext -> { // RestEasy configuration servletContext.setInitParameter("resteasy.scan", "true"); servletContext.setInitParameter("resteasy.servlet.mapping.prefix", "/services"); }; } @Bean public ServletContextListener restEasyBootstrap() { return new ResteasyBootstrap(); } @Bean public ServletRegistrationBean restEasyServlet() { final ServletRegistrationBean registrationBean = new ServletRegistrationBean(); registrationBean.setServlet(new HttpServletDispatcher()); registrationBean.setName("restEasy-servlet"); registrationBean.addUrlMappings("/services/*"); registrationBean.addInitParameter("javax.ws.rs.Application", "kjkrol.MyRESTApplication"); return registrationBean; } } package kjkrol; import javax.ws.rs.core.Application; import java.util.HashSet; import java.util.Set; public class MyRESTApplication extends Application { private final Set singletons = new HashSet(); public MyRESTApplication() { this.init(); } protected void init() { //TODO: Register your Rest services here: // this.singletons.add(YourService.class); } @Override public Set getSingletons() { return singletons; } }