随着近几年来,SOA,EAI等架构体系的日渐成熟,Webservice越来越炽手可热,尤其是在企业做异质平台整合时成为了首选的技术。
Java的Webservice技术更是层出不穷,比较流行的有:
Axis2,Spring WS以及Jaxws。
本人在日常工作和以往工程中,在使用了上述这些Webservice后进行了总结,比较,最终觉得jaxws是目前最标准,需要额外第三方插件最少,配置最少最灵活的webservice。
JAXWS适合几乎所有Webservice客户端的调用,因此不少巨头型的厂商如:IBM,Weblogic等,在他们的产品上都使用了以JAXWS为标准的Webservice接口。
下面就通过一个实例来走进jax-ws,先说说该实例的思路:
创建一个web项目少不了,因为webservice要通过web来访问。在服务器中加载该项目,启动服务器。
1、创建一个webservice接口:HelloWorld.java
2、开发其实现类:HelloWorldImpl.java,其中加入了客户端验证的功能。
3、以上两个创建好后,服务端的代码就完成了。再模拟一个webservice服务器,写一个java application,其实就是一个包含main函数的java类:Server.java
4、编写访问客户端:Client.java
代码如下:
HelloWorld.java
package com.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
/**
* @function webservice interface
* @author WangYang
* */
@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorld {
@WebMethod
public String getHelloWorldMessage();
@WebMethod
public String sayHello(String name);
}
HelloWorldImpl.java
package com.ws.impl;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import com.ws.HelloWorld;
/**
* Java6开发WebService入门
* @author WangYang
*/
@WebService(endpointInterface = "com.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
@Resource
WebServiceContext wsctx;
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public String getHelloWorldMessage() {
MessageContext mctx = wsctx.getMessageContext();
// 取得报文头,解析头文件
Map http_headers =
(Map) mctx.get(
MessageContext.HTTP_REQUEST_HEADERS);
List userList = (List) http_headers.get("Username");
List passList = (List) http_headers.get("Password");
String username = "";
String password = "";
if (userList != null) {
username = userList.get(0);
}
if (passList != null) {
password = passList.get(0);
}
if (username.equals("test") && password.equals("password")) {
return "Hello " + username +" to world of Jax WS - Valid User!";
} else {
return " User No Valid!";
}
}
@Override
public String sayHello(String name) {
return "hello,"+name;
}
}
Server.java
package com.ws.client;
import javax.xml.ws.Endpoint;
import com.ws.impl.HelloWorldImpl;
public class Server {
public static void main(String[] args){
Endpoint.publish("http://127.0.0.1:8080/baidumap/com.ws.HelloWorld", new HelloWorldImpl());
}
}
启动后,访问:http://127.0.0.1:8080/baidumap/com.ws.HelloWorld?wsdl
- - - - - - - - - -
Client.java
package com.ws.client;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.handler.MessageContext;
import com.ws.HelloWorld;
public class Client {
private static final String WS_URL = "http://127.0.0.1:8080/baidumap/com.ws.HelloWorld?wsdl";
public static void main(String[] args) throws Exception {
URL url = new URL(WS_URL);
/**
* QName(String namespaceURI,String localPart)
* @param namespaceURI 此处参考发布的webservice里面的targetNameSpace
* @param localPart 此参数参考发布的webservice里面的service name
* */
QName qname = new QName("http://impl.ws.com/", "HelloWorldImplService");
Service service = Service.create(url, qname);
HelloWorld hello = service.getPort(HelloWorld.class);
/**
* @function 以下的代码为用户名验证代码
* */
BindingProvider provider = (BindingProvider) hello;
Map req_ctx = provider.getRequestContext();
req_ctx.put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);
// 调用的用户名和密码,用map
Map> headers = new HashMap>();
headers.put("Username", Collections.singletonList("test"));
headers.put("Password", Collections.singletonList("password"));
//将用户名和密码信息放到头部文件中
req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
/**
* @function 调用方法
*
* */
System.out.println(hello.getHelloWorldMessage());
System.out.println(hello.sayHello("wzy"));
}
}
输出结果:
Hello test to world of Jax WS - Valid User!
hello,wzy
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/116910.html