2013-04-26 96 views
0

我有兩個類,我想公開作爲使用apache cxf和spring的restful web服務,但我無法公開兩個不同類的兩個web服務。我調用第二個service.here是我的web.xml,applicationContext.xml中和兩班,我用我project.plz任何一個解決這個問題apache cxf和spring寧靜的webservices

`enter code here` web.xml 
    -------- 

    <!DOCTYPE web-app PUBLIC 
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/dtd/web-app_2_3.dtd" > 

    <web-app> 
    <display-name>ApacheCXF Sample Application</display-name> 

     <!-- Add for Spring support --> 
     <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 




    <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-  class> 
</listener> 



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

     <init-param> 
    <param-name>com.sun.jersey.config.property</param-name> 
     <param-value>com.iton.restExamples.ServiceOneImpl</param-value> 
    </init-param> 

    <load-on-startup>1</load-on-startup> 
    </servlet> 


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

    <init-param> 
     <param-name>com.sun.jersey.config.property.packages</param-name> 
    <param-value>com.iton.restExamples.ServiceTwoImpl</param-value> 
    </init-param> 

    <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>CXFServlet</servlet-name> 
    <url-pattern>/base/*</url-pattern> 
    </servlet-mapping> 



<servlet-mapping> 
    <servlet-name>AnotherCXFServlet</servlet-name> 
    <url-pattern>/another/*</url-pattern> 
</servlet-mapping> 

</web-app> 


    applicatin context.xml 
    ------------------------ 

    <?xml version="1.0" encoding="UTF-8"?> 
    <beans default-autowire="byName" 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:util="http://www.springframework.org/schema/util" 
    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 
    http://www.springframework.org/schema/util   http://www.springframework.org/schema/util/spring-util-3.0.xsd 
      http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> 

<context:component-scan base-package="com.example" /> 

<!-- <import resource="classpath:META-INF/cxf/cxf.xml" /> 
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" /> 
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 
--> 
    <jaxrs:server id="restContainer" address="/"> 
    <jaxrs:serviceBeans> 

     <ref bean="serviceOne"/> 
     <ref bean="serviceTwo"/> 
     </jaxrs:serviceBeans> 


</jaxrs:server> 






    <bean id="serviceOne" class="com.iton.restExamples.ServiceOneImpl"></bean> 
    <bean id="serviceTwo" class="com.iton.restExamples.ServiceTwoImpl"></bean> 
    </beans> 


    ServiceOneImpl.java 
    --------------- 

    package com.iton.restExamples; 


    import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.QueryParam; 

public class ServiceOneImpl implements ServiceOne { 

public ServiceOneImpl() { 
    // TODO Auto-generated constructor stub 
} 

@Override 
@GET 
@Produces("text/plain") 
@Path("/one") 
public String mesgTwo(@QueryParam("msg") String mesgTwo) { 
    System.out.println(mesgTwo); 
    System.out.println("welcome message"); 

    return mesgTwo; 

} 

    } 


    SeriveTwoImpl.java 
    ------------------- 

    package com.iton.restExamples; 

    import javax.ws.rs.GET; 
    import javax.ws.rs.Path; 
    import javax.ws.rs.Produces; 

    public class ServiceTwoImpl implements ServiceTwo { 

public ServiceTwoImpl() { 
    // TODO Auto-generated constructor stub 
} 

@Override 
@GET 
@Produces("text/plain") 
@Path("/two") 
public String mesgTwo(String mesg) { 
    mesg="Welcome"; 
    System.out.println(mesg); 
    System.out.println("welcome message in Serive two"); 

    return mesg; 

} 

    } 

回答

0

嘗試使用@Path註釋類型水平兩個服務之間的區別類。

當您在應用程序上下文中定義服務bean時,這兩個bean的地址都是「/」。

 @Path("/ServiceOne") 
     public class ServiceOneImpl implements ServiceOne {....} 

     @Path("/ServiceTwo") 
     public class ServiceTwoImpl implements ServiceTwo {....}