jax-ws开发的webservice集成到web项目中

前提条件:

1.官网下载jax-ws 网址:http://jax-ws.java.net

2.jdk版本1.5以上

1.新建一个web工程:jaxwsWeb,复制官网下载下来的jax-ws的lib中jar文件到WEB-INFO/lib


2.新建类:com.it.server.TimerCheck

package com.it.server;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;


@WebService
@SOAPBinding(style = Style.RPC)
public class TimerCheck {
	@WebMethod
	public String getNowTime() {
		System.out.println("==============>>>>web server invoke success!");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date());
	}
}

3.打开web.xml添加一个listener和servlet

web.xml:

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>jaxwsWeb</display-name>
  <listener>
        <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>timerCheck</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>timerCheck</servlet-name>
        <url-pattern>/nowtime</url-pattern>
    </servlet-mapping>
</web-app>

4.官网下载下来的jax-ws中的事例中copy一个sun-jaxws.xml修改里面内容,url-pattern必须和web.xml中的servleturl-pattern一样

sun-jaxws.xml:

<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime'
	version='2.0'>
	<endpoint name='annotations' implementation='com.it.server.TimerCheck'
		url-pattern='/nowtime' />
</endpoints>

5.web项目放到web容器中运行,访问:http://127.0.0.1:9051/jaxwsWeb/nowtime 出现以下信息

Web 服务

端点 信息
服务名: {http://server.it.com/}TimerCheckService
端口名: {http://server.it.com/}TimerCheckPort
地址: http://127.0.0.1:9051/jaxwsWeb/nowtime
WSDL: http://127.0.0.1:9051/jaxwsWeb/nowtime?wsdl
实现类: com.it.server.TimerCheck

6.现在可以得到WSDL文件了,有了wsdl文件即可以生成客户端代码进行调用了



版权声明:本文为博主原创文章,未经博主允许不得转载。

你可能感兴趣的:(Web,webservice,jax-ws)