zoukankan      html  css  js  c++  java
  • tomcat发布web service教程

    这几天一直在准备找工作,自学了关于web service的一些基本的内容,也遇到了不少问题。现在就把我自己学到的知识和大家分享一下,由于是初学,所以有什么错误的地方请大家帮忙指正,感激不尽~~!!大笑

    1、下载jax-ws依赖包

    因tomcat没有jax-ws所需的依赖环境,所以第一步先下载Jax-ws RI,即jax-ws reference implemantation, 地址:http://jax-ws.java.net

     
     

    2、安装jax-ws RI到tomcat服务器

    先下载ant与tomcat,设置环境变量ANT_HOME与CATALINA_HOME,然后在path下引入各自的bin目录打开命令提示符,在jax-ws ri包的目录下运行ant install。

    此命令会直接把需要的包导入到${tomcat}sharedlib目录下,其实也就是把jaxws RI lib下的包复制到了tomcat安装目录下sharedlib里面。

    3、设置Eclipse中的tomcat

    由于eclipse是自己定义的tomcat配置文件,所以需要加些东西,把sharedlib加入进来,打开ctalina.properties文件。

     

    打开后为(节选):

    [html] view plaincopy
     
    1. # Licensed to the Apache Software Foundation (ASF) under one or more  
    2. # contributor license agreements.  See the NOTICE file distributed with  
    3. # this work for additional information regarding copyright ownership.  
    4. # The ASF licenses this file to You under the Apache License, Version 2.0  
    5. # (the "License"); you may not use this file except in compliance with  
    6. # the License.  You may obtain a copy of the License at  
    7. #  
    8. #     http://www.apache.org/licenses/LICENSE-2.0  
    9. #  
    10. # Unless required by applicable law or agreed to in writing, software  
    11. # distributed under the License is distributed on an "AS IS" BASIS,  
    12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
    13. # See the License for the specific language governing permissions and  
    14. # limitations under the License.  
    15.   
    16. #  
    17. # List of comma-separated packages that start with or equal this string  
    18. # will cause a security exception to be thrown when  
    19. # passed to checkPackageAccess unless the  
    20. # corresponding RuntimePermission ("accessClassInPackage."+package) has  
    21. # been granted.  
    22. package.access=sun.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.  
    23. #  
    24. # List of comma-separated packages that start with or equal this string  
    25. # will cause a security exception to be thrown when  
    26. # passed to checkPackageDefinition unless the  
    27. # corresponding RuntimePermission ("defineClassInPackage."+package) has  
    28. # been granted.  
    29. #  
    30. # by default, no packages are restricted for definition, and none of  
    31. # the class loaders supplied with the JDK call checkPackageDefinition.  
    32. #  
    33. package.definition=sun.,java.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.  
    34.   
    35. #  
    36. #  
    37. # List of comma-separated paths defining the contents of the "common"  
    38. # classloader. Prefixes should be used to define what is the repository type.  
    39. # Path may be relative to the CATALINA_HOME or CATALINA_BASE path or absolute.  
    40. # If left as blank,the JVM system loader will be used as Catalina's "common"  
    41. # loader.  
    42. # Examples:  
    43. #     "foo": Add this folder as a class repository  
    44. #     "foo/*.jar": Add all the JARs of the specified folder as class  
    45. #                  repositories  
    46. #     "foo/bar.jar": Add bar.jar as a class repository  
    47. common.loader=${catalina.home}/shared/lib/*.jar,${catalina.home}/shared/lib,${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar  

    找到common.loader配置项增加${catalina.home}/shared/lib/*.jar,${catalina.home}/shared/lib 这两个路径即可

    4、建立项目

    新建一个web项目,webservice_web,目录结构如下

     

    HelloService.java是提供web service的一个接口,代码如下:

    [java] view plaincopy
     
    1. package com.zxuqian.webservice;  
    2.   
    3. import javax.jws.WebMethod;  
    4. import javax.jws.WebService;  
    5.   
    6. @WebService  
    7. public interface HelloService {  
    8.        
    9.       @WebMethod  
    10.      String greetings (String name);  
    11.   
    12. }  

    HelloServiceImpl.java是实现类,代码如下:

    [java] view plaincopy
     
    1. package com.zxuqian.webservice.impl;  
    2.   
    3. import javax.jws.WebService;  
    4.   
    5. import com.zxuqian.webservice.HelloService;  
    6.   
    7. @WebService (endpointInterface = "com.zxuqian.webservice.HelloService" )  
    8. public class HelloServiceImpl implements HelloService {  
    9.   
    10.       @Override  
    11.       public String greetings(String name) {  
    12.             return "Hello: " + name;  
    13.      }  
    14.   
    15. }  

    5、添加sun-jaxws.xml

    sun-jaxws.xml是通过web方式发布web service应用的描述文件,内容如下:

    [html] view plaincopy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">  
    3.     <endpoint name="HelloWorld" implementation="com.zxuqian.webservice.impl.HelloServiceImpl"   
    4.         url-pattern="/hello" />  
    5. </endpoints>  

    各个节点的具体说明请参考下载的jaxws ri包里面的docs文档,在这里简单说明一下,endpoint需要指定
    web service服务的接口和实现类,以及它的url相对路径

    6、配置web.xml

    内容如下:

    [html] view plaincopy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <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">  
    3.   <display-name>webservice_web</display-name>  
    4.     
    5.   <listener>  
    6.     <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>  
    7.   </listener>  
    8.   <servlet>  
    9.     <servlet-name>hello</servlet-name>  
    10.     <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>  
    11.     <load-on-startup>1</load-on-startup>  
    12.   </servlet>  
    13.     
    14.   <servlet-mapping>  
    15.     <servlet-name>hello</servlet-name>  
    16.     <url-pattern>/hello</url-pattern>  
    17.   </servlet-mapping>  
    18. </web-app>  

    7、测试

    启动tomcat,在浏览器中输入web service地址 http://localhost:8088/webservice_web/hello我的tomcat的端口号是8088,大家根据自己的端口号进行相应的修改。

    8、参考文献

    http://www.mkyong.com/webservices/jax-ws/deploy-jax-ws-web-services-on-tomcat/

     
  • 相关阅读:
    如何成为一名数据科学家
    暑假反思
    暑假计划(7月23日-8月21日)
    ACM数论模板
    Nelder–Mead method
    Introduction to Data Mining
    51_1037最长循环节 (miller rabin算法 pollard rho算法 原根)
    乘法逆元(转)
    51_1228 序列求和(伯努利数)(转)
    清除input中内容的简单方法
  • 原文地址:https://www.cnblogs.com/toSeeMyDream/p/4518498.html
Copyright © 2011-2022 走看看