昨天晚上整理了一下webservice发不端相关,整理了一个测试小例,在这里记录并分享一下:
需要webservice发布相关jar包的小伙伴儿可以来这里下载:
https://pan.baidu.com/s/10XwFVM6PcLrRiEVtjUcZag
提取码为:pv9u
1、首先需要在eclipse建立一个web项目
2、建立一个接口,接口内定义了两个方法:
package com.webservice;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* 服务的webservice接口
* @author wxy_jdhk
*
*/
@WebService
public interface ITest {
//无返回值
@WebMethod(action="hello")
public void hello(@WebParam(name="data") String data);
//有返回值
@WebMethod(action="testHello")
public String testHello(@WebParam(name="name") String name);
}
3、建立接口相关的实现类
package webservice.impl;
import javax.jws.WebParam;
import javax.jws.WebService;
import webservice.ITest;
/**
* 服务端webservice实现类
* @author wxy_jdhk
*
*/
@WebService(serviceName="hello", endpointInterface="com.webservice.ITest")
public class Test implements ITest{
@Override
public void hello(@WebParam(name="data") String data) {
System.out.println("对外接收数据" + data);
}
@Override
public String testHello(@WebParam(name="name") String name) {
return name;
}
}
4、接着再写一个带有main函数的测试类来测下上面的劳动成果:
package main;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import webservice.impl.Test;
/**
* 测试main类
* @author wxy_jdhk
*
*/
public class MainTest {
public static void main(String[] args){
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(Test.class);
//服务地址+serviceName
factory.setAddress("http://localhost:8080/ws/hello");
//通过create方法发布接口
Server server = factory.create();
server.start();
System.out.println("web service server start ...");
}
}
5、最后到浏览器里检验一下,在浏览器输入“http://localhost:8080/ws/hello?wsdl”地址,效果如下: