zoukankan      html  css  js  c++  java
  • (十一)web服务与javaweb结合(2)

    一、解决问题及解决方法

    • 解决问题:上章节用监听器的方式是有缺陷的:web服务的端口和web工程的端口不能一致。
    • 解决方案:将webService绑定到web工程中,使得共用一个端口。

    二、案例

      2.1  创建一个web工程名:web_webService

      2.2  编写两个服务接口

    • 注意:
    1.   本例使用apache CXF3.2.0的框架来架构webService服务
    2.        在接口定义之前加上@WebService标注,表明这是一个WebService服务,否则在生成服务端时不能找到相应的接口;
    3.       这里@WebService标注的targetNamespace一定要填写内容,不然在生成WebService服务端的时候会报如下的错误,这个命名空间起始就是包名的倒序。
    package www.shyroke.com;
    
    import javax.jws.WebParam;
    import javax.jws.WebResult;
    import javax.jws.WebService;
    
    @WebService(targetNamespace="http://service.shyroke.com/")
    public interface IFirst {
    
        @WebResult(name="addResult")
        public int add(@WebParam(name="x")int x,@WebParam(name="y")int y);
    }
    package www.shyroke.com;
    
    import javax.jws.WebResult;
    import javax.jws.WebService;
    
    /**
     * 锟节讹拷锟斤拷锟接口凤拷锟斤拷
     * 锟斤拷锟节伙拷取锟斤拷前系统时锟斤拷
     * @author Administrator
     *
     */
    @WebService(targetNamespace="http://service.shyroke.com/")
    public interface ISecond {
        
        
        @WebResult(name="getSysTimeResult")
        public String getSysTime();
    }

      2.2  编写服务接口实现类

    package www.shyroke.com;
    
    import javax.jws.WebService;
    
    @WebService(endpointInterface="www.shyroke.com.IFirst")
    public class FirstImpl implements IFirst {
    
        @Override
        public int add(int x, int y) {
            return x+y;
        }
    
    }
    package www.shyroke.com;
    
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import javax.jws.WebService;
    
    @WebService(endpointInterface = "com.shyroke.service.ISecond")
    public class SecondImpl implements ISecond {
    
        @Override
        public String getSysTime() {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
            Date date = new Date();
    
            return dateFormat.format(date);
        }
    
        
        
    }

      2.3  发布服务并把服务绑定到web工程

    • 生成webService服务  : File->New->Other->Web Services->WebService,在Service implementation中选择提供服务的实现类。
    • 上述发布了第一个服务并绑定到了web工程,第二个服务的发布也按照上面图的操作来做即可。
    • 发布完两个服务的项目结构为下图:
    • 注意:    1. 到现在为止如果开启服务器会报错,因为工具在cxf-beans.xml中生成了如下,而我们在该目录下并没有,所以要把这三行去掉:
    <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" />
    •  注意:    2. 在cxf-beans.xml中把<jaxws:endpoint>属性里面有serviceName的属性和endpointName和address属性去掉,如下:
    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
        <jaxws:endpoint xmlns:tns="http://service.shyroke.com/"
            id="first" implementor="www.shyroke.com.FirstImpl" wsdlLocation="wsdl/firstimpl.wsdl">
            <jaxws:features>
                <bean class="org.apache.cxf.feature.LoggingFeature" />
            </jaxws:features>
        </jaxws:endpoint>
        <jaxws:endpoint xmlns:tns="http://service.shyroke.com/"
            id="second" implementor="www.shyroke.com.SecondImpl" wsdlLocation="wsdl/secondimpl.wsdl">
            <jaxws:features>
                <bean class="org.apache.cxf.feature.LoggingFeature" />
            </jaxws:features>
        </jaxws:endpoint>
    </beans>
    • 注意:3. 修改 firstimpl.wsdl和secondimpl.wsdl文件中的端口号,不能和tomcat工程一致(本例中的tomcat端口号为8080)
    • firstimpl.wsdl
    <?xml version="1.0" encoding="UTF-8"?>
    <wsdl:definitions name="FirstImplService" targetNamespace="http://com.shyroke.www/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://com.shyroke.www/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:ns1="http://service.shyroke.com/">
      <wsdl:import namespace="http://service.shyroke.com/" location="IFirst.wsdl">
        </wsdl:import>
      <wsdl:binding name="FirstImplServiceSoapBinding" type="ns1:IFirst">
        <soap12:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="add">
          <soap12:operation soapAction="" style="document"/>
          <wsdl:input name="add">
            <soap12:body use="literal"/>
          </wsdl:input>
          <wsdl:output name="addResponse">
            <soap12:body use="literal"/>
          </wsdl:output>
        </wsdl:operation>
      </wsdl:binding>
      <wsdl:service name="FirstImplService">
        <wsdl:port name="FirstImplPort" binding="tns:FirstImplServiceSoapBinding">
          <soap12:address location="http://localhost:8082/web_webService/services/FirstImplPort"/>
        </wsdl:port>
      </wsdl:service>
    </wsdl:definitions>
    • secondimpl.wsdl
    <?xml version="1.0" encoding="UTF-8"?>
    <wsdl:definitions name="SecondImplService" targetNamespace="http://com.shyroke.www/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://com.shyroke.www/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:ns1="http://service.shyroke.com/">
      <wsdl:import namespace="http://service.shyroke.com/" location="ISecond.wsdl">
        </wsdl:import>
      <wsdl:binding name="SecondImplServiceSoapBinding" type="ns1:ISecond">
        <soap12:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="getSysTime">
          <soap12:operation soapAction="" style="document"/>
          <wsdl:input name="getSysTime">
            <soap12:body use="literal"/>
          </wsdl:input>
          <wsdl:output name="getSysTimeResponse">
            <soap12:body use="literal"/>
          </wsdl:output>
        </wsdl:operation>
      </wsdl:binding>
      <wsdl:service name="SecondImplService">
        <wsdl:port name="SecondImplPort" binding="tns:SecondImplServiceSoapBinding">
          <soap12:address location="http://localhost:8081/web_webService/services/SecondImplPort"/>
        </wsdl:port>
      </wsdl:service>
    </wsdl:definitions>
    • 2.4      启动服务

    •  查看web.xml ,可知webservice的端口url-pattern
    <?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
      <display-name>web_webService</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <servlet>
        <description>Apache CXF Endpoint</description>
        <display-name>cxf</display-name>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/services/*</url-pattern>
      </servlet-mapping>
      <session-config>
        <session-timeout>60</session-timeout>
      </session-config>
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/cxf-beans.xml</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    </web-app>

       2.5  生成客户端

    •  用工具生成客户端(具体步骤查看前几章)

      2.6  测试

    package www.chyroke.com.test;
    
    import com.shyroke.service.IFirst;
    
    import www.shyroke.client.FirstImplService;
    
    public class TestkMain {
        public static void main(String[] args) {
            FirstImplService firstService=new FirstImplService();
            IFirst iFirst=firstService.getFirstImplPort();
            System.out.println("result==="+iFirst.add(5, 8));
        }
    }

    结果:

    •  缺陷:虽然将webservice和web项目绑定在了一起,但是还是不能共同一个端口。
  • 相关阅读:
    爱信诺面试总结
    项目进展日志6
    项目进展日志5
    项目进展日志4
    项目进展日志3
    项目进展日志2
    项目阶段总结
    项目进展日志
    事物的ACID特性
    5.27作业
  • 原文地址:https://www.cnblogs.com/shyroke/p/7668536.html
Copyright © 2011-2022 走看看