总结一下sun 公司自己的 JAX-WS WebService 用法

JAX-WS WebService 是sun 公司自己推出的产品,给自己做个记录,好记性不如烂笔头,方便日后查询,废话不说直接干货。


第一种传统方式:

创建一个类如下:

package com.webservice.test;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Endpoint;

/** 
 *  @WebService - 它是一个注解,用在类上指定将此类发布成一个ws. 
    Endpoint – 此类为端点服务类,它的方法publish用于将一个已经添加了@WebService注解对象绑定到一个地址的端口上。 
  
 * @author lt 
 * 
 */ 
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class HelloWebService {
	
	@WebMethod
	public  String HellowWord(String name) {
		return "hellow:"+name;
	}
	
	@WebMethod
	public  String HellowWord2(String name) {
		return "hellow:"+name;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/** 
		 *参数1:服务的发布地址 
		 *参数2:服务的实现者 
		 */  
		System.out.println("发布服务...");
		Endpoint.publish("http://localhost:9080/JAXWebService/helloWord",new HelloWebService());  
	}

}





 
</pre><pre>

用 wsimport -keep  http://localhost:9080/JAXWebService/helloWord?wsdl 生成客户端(需要JDK 1.6以上)

然后再客户端直接调用即可,客户端调用代码片段:

<span style="white-space:pre">		</span>HelloWebService2Service hellow2S=new HelloWebService2Service();
		HelloWebService2 hellow=hellow2S.getHelloWebService2Port();
		System.out.println("11");
		System.out.println(hellow.hellowWord2("涛哥"));


第二种方式:不需要生成客户端(此方式为servlet方式),个人感觉生成客户端过于繁琐 。

在工程目录 web-inf 下面创建 sun-jaxws.xml 文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
 <endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
     version="2.0">
     <endpoint name="SayHiService"
         implementation="com.webservice.style2.HelloWebService2"
         url-pattern="/service/helloWordStyle2" />
 </endpoints>

同时在web.xml文件 中添加如下配置:

<!-- web service   -->
  
  <!-- ws 监听 -->
  <listener>  
         <listener-class>
             com.sun.xml.ws.transport.http.servlet.WSServletContextListener  
         </listener-class>
  </listener>
  <servlet>
     <servlet-name>SayHiService</servlet-name>  
     <servlet-class>
         com.sun.xml.ws.transport.http.servlet.WSServlet  
     </servlet-class>
  </servlet>  
  <servlet-mapping>  
        <servlet-name>SayHiService</servlet-name>  
        <url-pattern>/service/helloWordStyle2</url-pattern>  
   </servlet-mapping>


根据浏览器查看 http://localhost:9080/JAXWebService/service/helloWordStyle2?wsdl 文件


This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3-hudson-390-. 
-->
<!--
 Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3-hudson-390-. 
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://style2.webservice.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://style2.webservice.com/" name="HelloWebService2Service">
<types/>
<message name="HellowWord2">
<part name="arg0" type="xsd:string"/>
</message>
<message name="HellowWord2Response">
<part name="return" type="xsd:string"/>
</message>
<portType name="HelloWebService2">
<operation name="HellowWord2">
<input message="tns:HellowWord2"/>
<output message="tns:HellowWord2Response"/>
</operation>
</portType>
<binding name="HelloWebService2PortBinding" type="tns:HelloWebService2">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
<operation name="HellowWord2">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal" namespace="http://style2.webservice.com/"/>
</input>
<output>
<soap:body use="literal" namespace="http://style2.webservice.com/"/>
</output>
</operation>
</binding>
<service name="HelloWebService2Service">
<port name="HelloWebService2Port" binding="tns:HelloWebService2PortBinding">
<soap:address location="http://localhost:9080/JAXWebService/service/helloWordStyle2"/>
</port>
</service>
</definitions>



然后客户端调用代码:

package com.jaxws.ws.test;


import java.net.URL;

import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
/*
 * 直接通过网址调用
 */
public class CallWebServiceTool {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		try {
//			String endpoint = "http://localhost:9080/JAXWebService/service/helloWordStyle2?wsdl";
//			//直接引用远程的wsdl文件
//			Service service = new Service();
//			Call call = (Call) service.createCall();
//			call.setTargetEndpointAddress(endpoint);
//			call.setOperationName(new QName("http://style2.webservice.com/", "HellowWord2"));//WSDL里面描述的接口名称
//			call.addParameter("arg0", org.apache.axis.encoding.XMLType.XSD_DATE,
//			javax.xml.rpc.ParameterMode.IN);//接口的参数
//			call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型 
//			String temp = "涛哥1";
//			String result = (String)call.invoke(new Object[]{temp});
//			//给方法传递参数,并且调用方法
//			System.out.println("result is "+result);
//			}
//			catch (Exception e) {
//			System.err.println(e.toString());
//		}
		String endpoint = "http://localhost:9080/JAXWebService/service/helloWordStyle2?wsdl";
		String namespace="http://style2.webservice.com/";
		String method="HellowWord2";
		String paramater="arg0";
		String message="涛哥v5";
		
		callWebservice(endpoint, namespace, method, paramater, message);
	}
	
	public static void callWebservice(String endpoint,String namespace,String method,String paramater,String message) {
		try {
//			String endpoint = "http://localhost:9080/JAXWebService/service/helloWordStyle2?wsdl";
			//直接引用远程的wsdl文件
			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(endpoint));
			call.setOperationName(new QName(namespace, method));//WSDL里面描述的接口名称
			call.addParameter(paramater, org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);//接口的参数
			call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型 
			String temp = message;
			String result = (String)call.invoke(new Object[]{temp});
			//给方法传递参数,并且调用方法
			System.out.println("result is "+result);
			}
			catch (Exception e) {
			System.err.println(e.toString());
		}
	}

}

调用结果:result is 你好:涛哥v5


可能需要用到的jar包 服务端


客户端调用:








  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值