使用apache cxf和spring安静的Web服务

时间:2013-04-26 07:08:22

标签: restful-url

我有两个类,我希望使用apache cxf和spring作为一个宁静的Web服务公开但是我无法公开两个不同类的两个Web服务。我在调用第二个服务时遇到了运行时异常。这是我的网站。 xml,applicationcontext.xml和我在project.plz中使用的两个类,任何一个修复此问题

  `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;

}

    }

1 个答案:

答案 0 :(得分:0)

尝试在类级别使用@Path注释来区分这两个服务类。

在应用程序上下文中定义服务bean时,两个bean的地址都是“/”。

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

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