当前位置: 代码迷 >> Web前端 >> jdk1.6公布webservice
  详细解决方案

jdk1.6公布webservice

热度:200   发布时间:2013-03-12 11:19:35.0
jdk1.6发布webservice
测试环境jdk1.6。
IFruitMgtService.java
package service;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface IFruitMgtService {
	public static final String APPLE="apple";
	public static final String ORANGE="orange";
	public static final String PLUM="plum";
	
	public Double getPrice(String fruitName);
}

FruitMgtServiceImpl.java
package service.impl;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import service.IFruitMgtService;
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class FruitMgtServiceImpl implements IFruitMgtService {

	@WebMethod
	@Override
	public Double getPrice(String fruitName) {
		if(APPLE.equals(fruitName)){
			return 5d;
		}else if(ORANGE.equals(fruitName)){
			return 10d;
		}else if(PLUM.equals(fruitName)){
			return 20d;
		}else {
			throw new RuntimeException("Sorry,can not calculate the fruit price!");
		}
	}
}

ClientFactory.java
package util;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import service.IFruitMgtService;

public class ClientFactory extends Service{
	private static String NAME_SPACE_URL="http://impl.service/";
	private static URL WSDL_DOCUMENT_LOCATION=null;
	private static QName serviceName=new 
			       QName(NAME_SPACE_URL,"FruitMgtServiceImplService");
	
	static{
		try {
			WSDL_DOCUMENT_LOCATION = new 
			URL("http://localhost:8888/JDK/FruitMgtService?wsdl");
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
	}
	private static ClientFactory instance=new 
				   ClientFactory(WSDL_DOCUMENT_LOCATION,serviceName);
	
	protected ClientFactory(URL wsdlDocumentLocation, QName serviceName) {
		super(wsdlDocumentLocation, serviceName);
	}
	
	public static IFruitMgtService getFruitMgtService(){
		return instance.getPort(
		new QName(NAME_SPACE_URL,"FruitMgtServiceImplPort"),IFruitMgtService.class);
	}
}

发布webservice测试代码
JDKServerMain.java
package main;
import service.impl.FruitMgtServiceImpl;
import com.sun.xml.internal.ws.transport.http.server.EndpointImpl;
public class JDKServerMain {
	public static void main(String[] args) {
		EndpointImpl.publish("http://localhost:8888/JDK/FruitMgtService", 
		new FruitMgtServiceImpl());
	}
}

客户端调用webservice测试代码
JDKClientMain.java
package main;
import service.IFruitMgtService;
import util.ClientFactory;
public class JDKClientMain {
	public static void main(String[] args) throws Exception {
		IFruitMgtService fruitMgtService= ClientFactory.getFruitMgtService();
		System.out.println(fruitMgtService.getPrice(IFruitMgtService.APPLE));
	}
}


Note:IFruitMgtService.java和FruitMgtServiceImpl.java中有相同注解@SOAPBinding(style = SOAPBinding.Style.RPC)。注意2处注解的style属性必须相同。这里style都为SOAPBinding.Style.RPC。如果将style设为SOAPBinding.Style.DOCUMENT则还要进行以下处理
1)用dos切换到classes目录启用wsgen
wsgen -wsdl -keep -cp . service.impl.FruitMgtServiceImpl
在classes/service/impl中生成
jaxws/GetPrice.class
jaxws/GetPrice.java
jaxws/GetPriceResponse.class
jaxws/GetPriceResponse.java
将jaxws/GetPrice.java和jaxws/GetPriceResponse.java拷贝到src/service
纠正编译错误
2)修改IFruitMgtService.java。设置WebService注解的targetNamespace属性如下:
@WebService(targetNamespace="http://impl.service/")
再次运行JDKClientMain.java可以成功获得结果。
记注:在接口上和实现类上的注解SOAPBinding的style属性必须一致。



SOAPBinding.Style.DOCUMENT SOAPBinding.Style.RPC区别:
当使用SOAPBinding.Style.RPC时,不需要一些额外处理(如上所述)。同时,如果通过wsgen对SOAPBinding.Style.RPC的Endpoint处理时,不会生成jaxws/...相关文件

当使用SOAPBinding.Style.DOCUMENT时,需要一些额外处理(如上所述)。同时,如果通过wsgen对SOAPBinding.Style.DOCUMENT的Endpoint处理时,会生成jaxws/...相关文件
  相关解决方案