首页 > hot资讯 > 使用tomcat发布Web Service

使用tomcat发布Web Service

更新时间:2020-09-18 17:18 浏览70次 来源:动力节点

Web Service用于开发分布式的交互操作的应用程序,是Web开发中经常用到的一门技术。本文我们一起来使用tomcat发布Web Service

下面是tomcat发布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}\shared\lib目录下,其实也就是把jaxws RI lib下的包复制到了tomcat安装目录下shared\lib里面。

3、设置Eclipse中的tomcat

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

打开后为(节选):

[html] view plaincopy

# Licensed to the Apache Software Foundation (ASF) under one or more

# contributor license agreements. See the NOTICE file distributed with

# this work for additional information regarding copyright ownership.

# The ASF licenses this file to You under the Apache License, Version 2.0

# (the "License"); you may not use this file except in compliance with

# the License. You may obtain a copy of the License at

#

# http://www.apache.org/licenses/LICENSE-2.0

#

# Unless required by applicable law or agreed to in writing, software

# distributed under the License is distributed on an "AS IS" BASIS,

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

# See the License for the specific language governing permissions and

# limitations under the License.

#

# List of comma-separated packages that start with or equal this string

# will cause a security exception to be thrown when

# passed to checkPackageAccess unless the

# corresponding RuntimePermission ("accessClassInPackage."+package) has

# been granted.

package.access=sun.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.

#

# List of comma-separated packages that start with or equal this string

# will cause a security exception to be thrown when

# passed to checkPackageDefinition unless the

# corresponding RuntimePermission ("defineClassInPackage."+package) has

# been granted.

#

# by default, no packages are restricted for definition, and none of

# the class loaders supplied with the JDK call checkPackageDefinition.

#

package.definition=sun.,java.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.

#

#

# List of comma-separated paths defining the contents of the "common"

# classloader. Prefixes should be used to define what is the repository type.

# Path may be relative to the CATALINA_HOME or CATALINA_BASE path or absolute.

# If left as blank,the JVM system loader will be used as Catalina's "common"

# loader.

# Examples:

# "foo": Add this folder as a class repository

# "foo/*.jar": Add all the JARs of the specified folder as class

# repositories

# "foo/bar.jar": Add bar.jar as a class repository

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

package com.zxuqian.webservice;

import javax.jws.WebMethod;

import javax.jws.WebService;

@WebService

public interface HelloService {

@WebMethod

String greetings (String name);

}

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

[java] view plaincopy

package com.zxuqian.webservice.impl;

import javax.jws.WebService;

import com.zxuqian.webservice.HelloService;

@WebService (endpointInterface = "com.zxuqian.webservice.HelloService" )

public class HelloServiceImpl implements HelloService {

@Override

public String greetings(String name) {

return "Hello: " + name;

}

}

5、添加sun-jaxws.xml

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

[html] view plaincopy

<endpoint implementation="com.zxuqian.webservice.impl.HelloServiceImpl" name="HelloWorld" p="" <="">

url-pattern="/hello" />


各个节点的具体说明请参考下载的jaxws ri包里面的docs文档,在这里简单说明一下,endpoint需要指定

web service服务的接口和实现类,以及它的url相对路径

6、配置web.xml

内容如下:

[html] view plaincopy

 

 

  webservice_web 

    

   

    com.sun.xml.ws.transport.http.servlet.WSServletContextListener 

   

   

    hello 

    com.sun.xml.ws.transport.http.servlet.WSServlet 

    1 

   

    

   

    hello 

    /hello 

   

  


7、测试

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

以上就是tomcat发布Web Service的整个过程,小伙伴们学会了吗?还有疑惑的小伙伴可以在本站的java视频教程中找到相关课程,慢慢学习。


热门课程推荐

全部班型支持免费试学

动力节点在线报名表(此信息已加密,请放心填写)

返回顶部