Spring与RESTEasy集成

时间:2015-05-22 14:42:32

标签: jboss spring-integration resteasy

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

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

1)GET的异步HTTP请求处理 2)POST的异步作业服务

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

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

我们可以使用一个组件将RESTEasy服务与spring集成集成吗?

2 个答案:

答案 0 :(得分:0)

没有明确的组件存在,这是所有要完成的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

答案 1 :(得分:-1)

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.