2015-07-30 24 views
6

Tôi có một ứng dụng web cũ (servlet thuần túy, không có Spring) mà tôi muốn chạy dưới dạng jar chất béo. Ứng dụng này cung cấp rất nhiều dịch vụ REST. Tôi không muốn sửa đổi mã cũ.
Cách định cấu hình ứng dụng Khởi động để tiếp tục sử dụng RestEasy?Cách cấu hình ứng dụng Spring-Boot để tiếp tục sử dụng RestEasy?

+0

Ứng dụng mùa xuân cũ của bạn có sử dụng mùa xuân được định cấu hình qua xml không? Bạn có thể thử tạo một lớp Cấu hình để nhập cấu hình xml của bạn. @Configuration @ImportResource ({"application-context.xml"}) lớp công khai Config {} –

+0

Câu hỏi liên quan: [Lồng ghép spring-boot với RESTEasy] (http://stackoverflow.com/questions/36730237/integrating-spring -boot-with-resteasy). –

Trả lời

5

Bạn có thể sử dụng trình khởi động khởi động RESTEasy. Đây là cách bạn làm điều đó:

Thêm POM phụ thuộc

Thêm phụ thuộc Maven dưới đây để mùa xuân Boot hồ sơ pom của bạn.

<dependency> 
    <groupId>com.paypal.springboot</groupId> 
    <artifactId>resteasy-spring-boot-starter</artifactId> 
    <version>2.1.1-RELEASE</version> 
    <scope>runtime</scope> 
</dependency> 

Đăng ký lớp học ứng dụng JAX-RS

Chỉ cần xác định lớp học của bạn JAX-RS ứng dụng (một lớp con của ứng dụng) như là một đậu mùa xuân, và nó sẽ được đăng ký tự động. Xem ví dụ bên dưới. Xem phần Phương thức đăng ký ứng dụng JAX-RS trong How to use RESTEasy Spring Boot Starter để biết thêm thông tin.

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 { 
} 

Đăng ký JAX-RS nguồn lực và các nhà cung cấp

Chỉ cần xác định chúng như đậu mùa xuân, và họ sẽ được đăng ký tự động. Lưu ý rằng các tài nguyên JAX-RS có thể là singleton hoặc yêu cầu phạm vi, trong khi các nhà cung cấp JAX-RS phải là các trình đơn.

Further information at the project GitHub page.

2

Nó không quá khó. Tôi chỉ đơn giản là viết lại cấu hình từ web.xml cũ của tôi bằng cách sử dụng các chú thích Spring.

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; 
    } 
} 
Các vấn đề liên quan