使用Spring在CXF中进行多个resouceBean配置

我正在使用带有Spring 3.0.6-RELEASE的CXF RS 2.5.1.我想为“单个端点”提供多个实现类.我看到这个问题被报告并修复了https://issues.apache.org/jira/browse/CXF-2439,但是,当我尝试这样做时,CXF只是从jaxrs:serviceBeans标签中选择第一个资源类.对于其他请求,我在tomcat控制台上看到此消息为“找不到匹配请求路径/帐户/休息/传输的操作”.以下是配置.感谢任何输入.

web.xml中

    <listener>
        <listener-class> org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:account-servlet.xml</param-value>
    </context-param>

      <servlet>
    <servlet-name>CXF Servlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
       </servlet>

      <servlet-mapping>
    <servlet-name>CXF Servlet</servlet-name>
    <url-pattern>/*</url-pattern>
      </servlet-mapping>

帐户servlet.xml中

    <jaxrs:server id="accountService" address="/rest">
        <jaxrs:serviceBeans>
            <ref bean="transferService" />
                <ref bean="balanceService"/>
        </jaxrs:serviceBeans>
        <jaxrs:extensionMappings>
            <entry key="xml" value="application/xml" />
        </jaxrs:extensionMappings>
    </jaxrs:server>


    <bean id="transferService" class="com.mycompany.service.TransferService"/>
    <bean id="balanceService" class="com.mycompany.service.BalanceService"/>

BalanceService.java(省略导入)

        package com.mycompany.service;
        @Path("/")
         @Produces("application/xml")
        public class BalanceService{

    @GET
    @Path("/balance")
    public String getBalance() {
        StringBuilder response = new StringBuilder(128);
        response.append("<Balance>")
            .append("<amount>").append("250.00").append("</amount>")
            .append("</Balance>");
        return response.toString();
    }
       }

TransferService.java(导入省略)

package com.mycompany.service;

@Path("/")
@Produces("application/xml")
public class TransferService {

    @GET
    @Path("/transfer")
    public String getTransfer() {

        StringBuilder response = new StringBuilder(128);
        response.append("<Transfer>")
            .append("<amount>").append("350.00").append("</amount>")
            .append("</Transfer>");
        return response.toString();
    }
  }

请忽略任何编程违规/标准,因为它只是POC的示例应用程序.

解决方法:

我通过将部分@Path映射移动到服务bean类来解决了这个问题.在你的情况下:

BalanceService

@Path("/balance")
@Produces("application/xml")
public class BalanceService {
    @GET
    @Path("/{id}")
    public String getBalance(@PathParam("id") long id) {
        ...
    }
}

TransferService

@Path("/transfer")
@Produces("application/xml")
public class TransferService {

    @GET
    @Path("/{id}")
    public String getTransfer(@PathParam("id") long id) {
       ...      
    }
}
上一篇:使用Apache Camel如何解组通过CXF端点传入的反序列化对象?


下一篇:java – 注释@XmlElement只写?