Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How configure Spring-Boot app to continue to use RestEasy?

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.
How configure Spring-Boot app to continue to use RestEasy?

like image 501
Karol Król Avatar asked Jul 30 '15 09:07

Karol Król


People also ask

How do I auto start spring boot?

To enable your Spring Boot application to automatically restart when you make a change to your Java code, add the following dependency to your pom. xml file. Here is an example of how a very simple pom. xml file looks like when a spring-boot-devtools dependency is added to the list of dependencies.

What is Jax-Rs RESTEasy?

Overview. JAX-RS, JSR-311, is a new JCP specification that provides a Java API for RESTful Web Services over the HTTP protocol. Resteasy is an portable implementation of this specification which can run in any Servlet container.


2 Answers

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.

like image 139
Fabio Avatar answered Sep 18 '22 13:09

Fabio


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;
    }
}
like image 28
Karol Król Avatar answered Sep 17 '22 13:09

Karol Król