本文主要是介绍tomcate 发布 webservice,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
[一]、构建项目
1 | mvn archetype:create -DgroupId=com.micmiu.jaxws.web -DartifactId=jaxws-web-demo -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode= false |
转为 Eclipse web 项目:mvn eclipse:eclipse -Dwtpversion=1.0 导入Eclipse项目
创建配置相应的源目录、编译目录,目录结构如下:
D:\workspace_sun\jaxws-web-demo>tree /F
卷 work 的文件夹 PATH 列表
卷序列号为 2AF7-9BD9
D:.
│ .classpath
│ .project
│ pom.xml
│
├─.settings
│ .component
│ org.eclipse.jdt.core.prefs
│ org.eclipse.wst.common.project.facet.core.xml
│ org.maven.ide.eclipse.prefs
│
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─micmiu
│ │ │ └─jaxws
│ │ │ └─demo2
│ │ │ └─impl
│ │ │
│ │ ├─resources
│ │ └─webapp
│ │ │ index.jsp
│ │ │
│ │ └─WEB-INF
│ │ web.xml
│ │
│ └─test
│ └─java
└─target├─classes│ └─com│ └─micmiu│ └─jaxws│ └─demo2│└─test-classes
[二]、服务端编码
编码服务接口:HelloService.java
1 | package com.micmiu.jaxws.demo2; |
3 | import javax.jws.WebMethod; |
4 | import javax.jws.WebParam; |
5 | import javax.jws.WebService; |
6 | import javax.jws.soap.SOAPBinding; |
7 | import javax.jws.soap.SOAPBinding.Style; |
8 | import javax.jws.soap.SOAPBinding.Use; |
12 | * @author <a href="http://www.micmiu.com">Michael</a> |
14 | @WebService (serviceName = "HelloWorldService" ) |
15 | @SOAPBinding (style = Style.DOCUMENT, use = Use.LITERAL) |
16 | public interface HelloService { |
18 | String sayHello( @WebParam (name = "userName" ) String userName); |
服务接口的实现类:HelloServiceImpl.java
1 | package com.micmiu.jaxws.demo2.impl; |
3 | import javax.jws.WebService; |
5 | import com.micmiu.jaxws.demo2.HelloService; |
8 | * blog http://www.micmiu.com |
12 | @WebService (endpointInterface = "com.micmiu.jaxws.demo2.HelloService" ) |
13 | public class HelloServiceImpl implements HelloService { |
15 | public String sayHello(String userName) { |
16 | return "Hi," + userName |
17 | + " welcome to JAX-WS see more http://www.micmiu.com " ; |
[三]、创建:sun-jaxws.xml
在webapp/WEB-INF/ 下创建文件:sun-jaxws.xml
1 | <? xml version = "1.0" encoding = "UTF-8" ?> |
2 | < endpoints xmlns = "http://java.sun.com/xml/ns/jax-ws/ri/runtime" |
4 | < endpoint name = "HelloWorldService" implementation = "com.micmiu.jaxws.demo2.impl.HelloServiceImpl" |
5 | url-pattern = "/helloworld" /> |
[四]、 配置:web.xml
修改web.xml 文件如下:
1 | <!DOCTYPE web-app PUBLIC |
2 | "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" |
3 | "http://java.sun.com/dtd/web-app_2_3.dtd" > |
6 | < display-name >JAX-WS+Web@ www.micmiu.com</ display-name > |
9 | com.sun.xml.ws.transport.http.servlet.WSServletContextListener |
13 | < servlet-name >helloworld</ servlet-name > |
15 | com.sun.xml.ws.transport.http.servlet.WSServlet |
17 | < load-on-startup >1</ load-on-startup > |
20 | < servlet-name >helloworld</ servlet-name > |
21 | < url-pattern >/helloworld</ url-pattern > |
[五]、添加依赖库
如果是maven构建,只需要在 POM.xml 中添加如下内容:
2 | < groupId >javax.xml.ws</ groupId > |
3 | < artifactId >jaxws-api</ artifactId > |
4 | < version >2.2.8</ version > |
9 | < groupId >com.sun.xml.ws</ groupId > |
10 | < artifactId >jaxws-rt</ artifactId > |
11 | < version >2.2.6-2</ version > |
13 | < scope >compile</ scope > |
16 | < groupId >org.glassfish.gmbal</ groupId > |
17 | < artifactId >gmbal-api-only</ artifactId > |
18 | < version >3.2.0-b003</ version > |
20 | < scope >compile</ scope > |
23 | < groupId >org.glassfish.external</ groupId > |
24 | < artifactId >management-api</ artifactId > |
25 | < version >3.1.0-b001</ version > |
27 | < scope >compile</ scope > |
30 | < groupId >org.jvnet.staxex</ groupId > |
31 | < artifactId >stax-ex</ artifactId > |
32 | < version >1.7</ version > |
34 | < scope >compile</ scope > |
37 | < groupId >com.sun.xml.stream.buffer</ groupId > |
38 | < artifactId >streambuffer</ artifactId > |
39 | < version >1.5</ version > |
41 | < scope >compile</ scope > |
44 | < groupId >com.sun.xml.ws</ groupId > |
45 | < artifactId >policy</ artifactId > |
46 | < version >2.3.1</ version > |
48 | < scope >compile</ scope > |
51 | < groupId >com.sun.xml.bind</ groupId > |
52 | < artifactId >jaxb-impl</ artifactId > |
53 | < version >2.2.5</ version > |
55 | < scope >compile</ scope > |
或者去官网: http://jax-ws.java.net/ 下载相关的lib包。
[六]、验证服务发布
Eclipse中可以直接选择在 tomcat server(或Jetty)下启动项目:
....
2012-8-6 23:39:25 com.sun.xml.ws.transport.http.servlet.WSServletContextListener contextInitialized
信息: WSSERVLET12: JAX-WS 上下文监听程序正在初始化
2012-8-7 23:39:26 com.sun.xml.ws.transport.http.servlet.WSServletDelegate <init>
信息: WSSERVLET14: JAX-WS servlet 正在初始化
....
在浏览器中输入:http://localhost:8082/jaxws-web-demo/helloworld
看到类似上述截图的内容,表示服务已经发布成功了。
这篇关于tomcate 发布 webservice的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!