day63-webservice 08.在web项目中配置带有接口的webservice服务

这个是配置带有接口的WebService的服务。

http://localhost:8080/cxf-web-server/service

带有接口的实现类也给它做好了.jaxws:endpoint是用在类上,jaxws:server是用在接口上.这就是带有接口的WebService.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd
            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
    <!-- 引入CXF Bean定义如下,早期的版本中使用 -->
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    <!-- 
     =========================配置类的形式webservice服务==================================
       address:tomcat的host http://ip:port/projectName/service/后面的一端路径
       implementor:指定具体的服务的类
     -->
     <!--  
     <jaxws:endpoint id="hello" address="/hello" implementor="com.rl.web.server.HelloService"> 
        -->
        <!-- 输入拦截器,打印输入的消息 -->
        <!--  
        <jaxws:inInterceptors>
          <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
        </jaxws:inInterceptors>
        <jaxws:outInterceptors>
           <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
        </jaxws:outInterceptors>
     </jaxws:endpoint>
     -->
          <!-- 
          =======================配置带有接口的webservice服务=========================================
           address:tomcat的host http://ip:port/projectName/service/后面的一端路径
           http://ip:port/projectName/service/bye
          implementor:指定具体的服务的类
          serviceClass:服务接口的类
          -->
          <jaxws:server address="/bye" serviceClass="com.rl.web.server.inter.ByeInter">     
          <!-- 
                      这里不是指定实例,而是指定实现类的类
                      服务接口的实现类
          -->
          <jaxws:serviceBean>
              <bean class="com.rl.web.server.inter.ByeInterImpl"></bean>
          </jaxws:serviceBean>
          <!-- 输入拦截器,打印输入的消息 --> 
        <jaxws:inInterceptors>
          <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
        </jaxws:inInterceptors>
        <jaxws:outInterceptors>
           <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
        </jaxws:outInterceptors>
     </jaxws:server>
</beans>
<?xml version="1.0" encoding="UTF-8"?> 
   <web-app  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
       id="WebApp_ID" version="2.5">
   <!-- 使用Spring的监听器 -->
   <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><!-- 初始化Spring的容器,cxf.xml本身就是一个Spring的容器.可以把cxf.xml作为Spring的容器进行加载. --> 
      <!-- 能加载Spring文件的类,这个类叫什么? -->
   </listener>
   <context-param>
      <param-name>contextConfigLocation</param-name><!-- param-name不能再指定config-location,而是要指定ContextLoaderListener里面读取Spring文件的那个Key -->
      <param-value>classpath:cxf.xml</param-value>
   </context-param>
   <servlet>
       <servlet-name>cxf</servlet-name>
       <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
       <!--  
       <init-param>
          <param-name>config-location</param-name>
          <param-value>classpath:cxf.xml</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
       -->
   </servlet>
   <servlet-mapping>
       <servlet-name>cxf</servlet-name>
       <url-pattern>/service/*</url-pattern>  <!-- 拦截这种请求 -->
   </servlet-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd
            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
    <!-- 引入CXF Bean定义如下,早期的版本中使用 -->
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    <!-- 
       =========================配置类的形式webservice服务==================================
       address:tomcat的host http://ip:port/projectName/service/后面的一端路径
       implementor:指定具体的服务的类
     -->
     <!--  
     <jaxws:endpoint id="hello" address="/hello" implementor="com.rl.web.server.HelloService"> 
        -->
        <!-- 输入拦截器,打印输入的消息 -->
        <!--  
        <jaxws:inInterceptors>
          <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
        </jaxws:inInterceptors>
        <jaxws:outInterceptors>
           <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
        </jaxws:outInterceptors>
     </jaxws:endpoint>
     -->
     <!-- 
          =======================配置带有接口的webservice服务=========================================
           address:tomcat的host http://ip:port/projectName/service/后面的一端路径
           http://ip:port/projectName/service/bye
          implementor:指定具体的服务的类
          serviceClass:服务接口的类
      -->
 
     <jaxws:server address="/bye" serviceClass="com.rl.web.server.inter.ByeInter">
          <!-- 
                      这里不是指定实例,而是指定实现类的类
                      服务接口的实现类
          -->
          <jaxws:serviceBean>
              <bean class="com.rl.web.server.inter.ByeInterImpl"></bean>
          </jaxws:serviceBean>
          <!-- 输入拦截器,打印输入的消息 -->
        <jaxws:inInterceptors>
          <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
        </jaxws:inInterceptors>
        <jaxws:outInterceptors>
           <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
        </jaxws:outInterceptors>
     </jaxws:server>
</beans>
package com.rl.web.server.inter;

import javax.jws.WebService;

@WebService
public interface ByeInter {
   
   public String sayBye(String name);    

}
package com.rl.web.server.inter;

public class ByeInterImpl implements ByeInter {

    @Override
    public String sayBye(String name) {
        // TODO Auto-generated method stub
        return name + " bye";
    }

}
package com.rl.cxf.web.client;

import com.rl.web.inter.ByeInter;
import com.rl.web.inter.ByeInterService;

public class WebInterClient {
    public static void main(String[] args) {
        
    
   ByeInterService  bs = new ByeInterService();
   ByeInter bi = bs.getByeInterPort();
   String result = bi.sayBye("八戒");
   System.out.println(result);
    }
}
原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/7682192.html