Spring 与 RESTEasy 集成

Spring Integration with RESTEasy

在我们现有的集成中,我们计划用 RESTEasy 服务替换 Queue(我们集成处理的入口点)。

我们正在处理如下 HTTP 请求:

1) GET 的异步 HTTP 请求处理 2) POST

的异步作业服务

我了解 spring 集成为 HTTP 请求提供和。但这不是我们想要的,因为请求处理是由 RESTEasy 处理的。

软件堆栈: RESTEasy 3.0.9 框架 Spring 集成 4.1.2.RELEASE JBOSS EAP 6.4.

是否有我们可以用来将 RESTEasy 服务与 spring 集成的组件?

不存在显式组件,所有 API 工作都需要完成。您需要使用依赖jar文件和集成代码

以下是使用 ant 或 maven 在工作区环境中作为路径的最小 Jar 文件:

org.jboss.resteasy:resteasy-jaxrs:3.0.10.Final
org.jboss.resteasy:resteasy-spring:3.0.10.Final
org.springframework.boot:spring-boot-starter-web:1.2.2.RELEASE
org.jboss.resteasy:resteasy-jackson2-provider:3.0.10.Final

待完成 web.xml 中的以下侦听器条目:

   <context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/project</param-value>
   </context-param>
   <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
   </context-param>
   <listener>
    <listener- class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
    </listener>
   <listener>
    <listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
    </listener>
    <servlet>
    <servlet-name>RESTEasyService</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
     <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.concretepage.Application</param-value>
     </init-param>
    </servlet>
    <servlet-mapping>
     <servlet-name>RESTEasyService</servlet-name>
     <url-pattern>/project/*</url-pattern>
    </servlet-mapping>

Dispatch Servlet可以放入WEB-INF

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
       <context:component-scan base-package="com.concretepage" />
</beans>

样本Java服务代码:

import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Path("/manage" )
@Component
public class ExService {
    @Autowired
    private ExRepository repository;

    @GET
    @Path("/{id}")
    @Produces("application/json")
    public Response getEmp(@PathParam("id") String id) {
        Map<String,String> map = repository.getEmpDetail(id);
        return Response.ok(map).build();
    }
}

详情可参考http://docs.jboss.org/resteasy/docs/3.0.9.Final/userguide/html_single/index.html#RESTEasy_Spring_Integration

Use  <int:gateway> to do the integration with spring. 

    <int:gateway id="providerGateway" service-interface="com.stack.overflow.TestInterface"
        default-request-channel="requestChannel">
        <int:method name="getDataByID" request-channel="requestChannel"/>
        <int:method name="postDataByID" request-channel="requestChannel"/>
    </int:gateway>

Where com.stack.overflow.TestInterface is the Resource Interface see below:

    @Path(RestConstants.SERVICES)
    public interface TestInterface {

    @Path(RestConstants.TEST1)
    @GET
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public Response getDataByID();



    @Path(RestConstants.TEST2)
    @POST
    @Produces({ MediaType.TEXT_PLAIN  })
    public Response postDataByID(String edi);

}

You can have different message channel if desired (see the gateway above)  for a request. e.g. a request to getDataByID, will be put on the  requestChannel. You can read from this channel and do the required processing as you require and then send a response back.