I have an old web application (pure servlet, without Spring) that I want to run as fat-jar.
This app provides a lot of REST services. I don\'t want to modified old code.
You can use RESTEasy Spring Boot starter. Here is how you do it:
Adding POM dependency
Add the Maven dependency below to your Spring Boot application pom file.
<dependency>
<groupId>com.paypal.springboot</groupId>
<artifactId>resteasy-spring-boot-starter</artifactId>
<version>2.1.1-RELEASE</version>
<scope>runtime</scope>
</dependency>
Registering JAX-RS application classes
Just define your JAX-RS application class (a subclass of Application) as a Spring bean, and it will be automatically registered. See the example below. See section JAX-RS application registration methods in How to use RESTEasy Spring Boot Starter for further information.
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 {
}
Registering JAX-RS resources and providers
Just define them as Spring beans, and they will be automatically registered. Notice that JAX-RS resources can be singleton or request scoped, while JAX-RS providers must be singletons.
Further information at the project GitHub page.
It wasn't so difficult. I simply rewrote configuration from my old web.xml using Spring annotations.
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;
}
}