JAX-WS手动配置实例

2022-09-14 12:59:39 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

随着近几年来,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

代码语言:javascript复制
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

代码语言:javascript复制
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<String> userList = (List) http_headers.get("Username");
		List<String> 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

代码语言:javascript复制
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

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8"?>

<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->

<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
-<definitions name="HelloWorldImplService" targetNamespace="http://impl.ws.com/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://impl.ws.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"> <import location="http://127.0.0.1:8080/baidumap/com.ws.HelloWorld?wsdl=1" namespace="http://ws.com/"/> -<binding type="ns1:HelloWorld" name="HelloWorldImplPortBinding" xmlns:ns1="http://ws.com/"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> -<operation name="getHelloWorldMessage"> <soap:operation soapAction=""/> -<input> <soap:body namespace="http://ws.com/" use="literal"/> </input> -<output> <soap:body namespace="http://ws.com/" use="literal"/> </output> </operation> -<operation name="sayHello"> <soap:operation soapAction=""/> -<input> <soap:body namespace="http://ws.com/" use="literal"/> </input> -<output> <soap:body namespace="http://ws.com/" use="literal"/> </output> </operation> </binding> -<service name="HelloWorldImplService"> -<port name="HelloWorldImplPort" binding="tns:HelloWorldImplPortBinding"> <soap:address location="http://127.0.0.1:8080/baidumap/com.ws.HelloWorld"/> </port> </service> </definitions>

Client.java

代码语言:javascript复制
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<String, Object> req_ctx = provider.getRequestContext();
		req_ctx.put(
		BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);
		// 调用的用户名和密码,用map
		Map<String, List<String>> headers = new HashMap<String, List<String>>();
		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"));
	}

}

输出结果:

代码语言:javascript复制
Hello test to world of Jax WS - Valid User!
hello,wzy

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/159702.html原文链接:https://javaforall.cn

0 人点赞