如何配置Spring-Boot app 继续使用RestEasy?
How configure Spring-Boot app to continue to use RestEasy?
我有一个旧的 Web 应用程序(纯 servlet,没有 Spring),我想 运行 作为 fat-jar。
这个应用程序提供了很多 REST 服务。我不想修改旧代码。
如何配置Spring-Boot app继续使用RestEasy?
没那么难。我只是使用 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<Object> singletons = new HashSet<Object>();
public MyRESTApplication() {
this.init();
}
protected void init() {
//TODO: Register your Rest services here:
// this.singletons.add(YourService.class);
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
您可以使用 RESTEasy Spring 启动器。以下是您的操作方法:
添加 POM 依赖项
将下面的 Maven 依赖项添加到您的 Spring 启动应用程序 pom 文件。
<dependency>
<groupId>com.paypal.springboot</groupId>
<artifactId>resteasy-spring-boot-starter</artifactId>
<version>2.1.1-RELEASE</version>
<scope>runtime</scope>
</dependency>
正在注册 JAX-RS 应用程序classes
只需将您的 JAX-RS 应用程序 class(应用程序的子class)定义为 Spring bean,它就会自动注册。请参见下面的示例。有关详细信息,请参阅 How to use 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 beans,它们就会自动注册。请注意,JAX-RS 资源可以是单例或请求范围的,而 JAX-RS 提供者必须是单例。
我有一个旧的 Web 应用程序(纯 servlet,没有 Spring),我想 运行 作为 fat-jar。
这个应用程序提供了很多 REST 服务。我不想修改旧代码。
如何配置Spring-Boot app继续使用RestEasy?
没那么难。我只是使用 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<Object> singletons = new HashSet<Object>();
public MyRESTApplication() {
this.init();
}
protected void init() {
//TODO: Register your Rest services here:
// this.singletons.add(YourService.class);
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
您可以使用 RESTEasy Spring 启动器。以下是您的操作方法:
添加 POM 依赖项
将下面的 Maven 依赖项添加到您的 Spring 启动应用程序 pom 文件。
<dependency>
<groupId>com.paypal.springboot</groupId>
<artifactId>resteasy-spring-boot-starter</artifactId>
<version>2.1.1-RELEASE</version>
<scope>runtime</scope>
</dependency>
正在注册 JAX-RS 应用程序classes
只需将您的 JAX-RS 应用程序 class(应用程序的子class)定义为 Spring bean,它就会自动注册。请参见下面的示例。有关详细信息,请参阅 How to use 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 beans,它们就会自动注册。请注意,JAX-RS 资源可以是单例或请求范围的,而 JAX-RS 提供者必须是单例。