编写一个普通的类, 再加入一些注解即可.如:
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在服务器启动时发布出去。