定义接口
packagecom.amwu.v1;
import java.util.Date;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style = Style.RPC)
public interface IWs {
@WebMethod
public abstract String say(String name);
/* @WebMethod
public abstract List<Date> getSomeDate(); //不支持集合类
*/
public String[] getNames();
public Date[] getDate();
public Man getMan();//支持自定义类型
}
实现接口
@WebService(endpointInterface="com.amwu.v1.IWs")
public class Ws implements IWs {
/* (non-Javadoc)
* @see com.amwu.v1.IWs#say(java.lang.String)
*/
public String say(String name) {
return "您好:" + name;
}
/* (non-Javadoc)
* @see com.amwu.v1.IWs#getSomeDate()
*/
public List<Date> getSomeDate(){
List<Date> res=new ArrayList<Date>();
for(int i=0;i<10000;i++){
res.add(new Date());
}
return res;
}
public Man getMan() {
// TODO Auto-generated method stub
return new Man("amwu",28,"M",false);
}
public String[] getNames() {
return Arrays.asList("amwu","xjg").toArray(new String[1]);
}
public Date[] getDate() {
Date[] d=new Date[2];
d[0]=new Date();
d[1]=new Date();
return d;
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Log4j
public class Man {
private String name;// 姓名
private int age;
private String sex;
private boolean haveGF;
public Man(Man man) {
this.age = man.age;
this.name = man.name;
this.sex = man.sex;
}
}
注:如果该实现类只是实现了一个接口,可以不定endpointInterface,如果实现了多个接口,而只是想返回一个接口的web服务方法就需要写一下了。
发布:
public static void main(String[] args) {
Endpoint.publish("http://127.0.0.1:9876/ts", new Ws()); //单例的
}
研究,在不增加jar包的前提共享 80 端口。
有些服务器由于安全,防火墙的原因只给开放了少数的端口
---------可以与tomcat一起共享80端口
package com.amwu;
import java.net.MalformedURLException;
import java.util.Enumeration;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.xml.ws.Endpoint;
public class WSListener implements ServletContextListener {
private static final long serialVersionUID = 1L;
Endpoint ws;
//ServletContextListener
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("停止webserive");
ws.stop();
}
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("启动webserive");
String ctx=sce.getServletContext().getContextPath();
String tomcatPort=sce.getServletContext().getInitParameter("tomcatPort");
System.out.println(tomcatPort);
String wsdl="http://127.0.0.1:"+tomcatPort+ctx+"/ws";
System.out.println("wsdl:"+wsdl);
ws = Endpoint.publish(wsdl, new Ws()); //单例的
}
}
动态代理