`
wujianjun
  • 浏览: 141845 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

JAX-WS 实现WebService发布

阅读更多

编写一个普通的类, 再加入一些注解即可.如:

package com.wujianjun.ws;

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

import com.wujianjun.domain.Department;
import com.wujianjun.domain.Person;

@WebService(serviceName = "FirstService")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class MyServicesImpl {

	@WebMethod()
	public String testSimple(String[] param) {
		String res = "";
		for(String s:param) 
			res+=","+s;
		return res;
	}

	@WebMethod()
	public String testSimple2(Department[] depts) {
		String res = "";
		for(Department s:depts) 
			res+=","+s;
		return res;
	}

	@WebMethod()
	public String testSimple3(Person[] persons) {
		String res = "";
		for(Person s:persons) 
			res+=","+s;
		return res;
	}

}

 直接写一个普通带Main的类运行以下语句就可以了

public static void main(String[] args) {
    Endpoint.publish("http://127.0.0.1/services",new MyServicesImpl());
}

再打开浏览器输入 发布的地址(http://127.0.0.1/services?wsdl)就可以看到对应的wsdl了。

如果用spring来管理对象。则只需要在发布的时候绑定的对象从spring容器里取得就可以了。代码如下:

public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Endpoint.publish("http://192.168.10.179/services", context.getBean("myServices"));
	}

 

这个服务的发布代码也可以放到一个servlet的init里去执行.再把servlet调为随服务器启动即调用.这样就把service在服务器启动时发布出去。

分享到:
评论
3 楼 lg_asus 2012-06-20  
我也是像你这样用ajx-ws发布一个WS,然后我用axis生成客户端代码进行调用,同时我在服务器端加了日志,其中服务器是立即执行客户端的请求,但客户端却隔很长时间才接收到结果。

我也用soapUI工具测试了下,很快就得到结果。

不知道你有没有遇到这种情况?

any help appreciated.
2 楼 wujianjun 2012-05-08  
我用的是jdk1.6

估计你ws发布时绑定的url有误吧
1 楼 liguanqun811 2012-05-04  
哥们,我下载了你的例子。直接运行WebServicePublisher 不行啊。报错啊。
Exception in thread "main" com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error: java.net.SocketException: Unresolved address

你用的jdk啥版本的啊?

相关推荐

Global site tag (gtag.js) - Google Analytics