定义接口类:
package com.jtang;
import javax.jws.WebService;
@WebService(targetNamespace="http://com.jtang")
public interface IHello {
public String hello(String str);
public int add(int a, int b);
}
实现类:
package com.jtang;
import javax.jws.WebService;
@WebService(targetNamespace="http://com.jtang")
public class HelloImpl implements IHello{
@Override
public String hello(String str){
return "Hello " + str;
}
@Override
public int add(int a, int b){
return a + b;
}
}
发布类:
package publish;
import javax.xml.ws.Endpoint;
import com.jtang.HelloImpl;
public class Publish {
/**
* @param args
*/
public static void main(String[] args) {
// Endpoint.publish("http://192.168.3.87:9998/getName", new GetUsernameImpl());
Endpoint.publish("http://192.168.3.86:9998/hello", new HelloImpl());
}
}
网页输入:
http://192.168.3.86:9998/hello?wsdl就能访问
调用webservice(通过cxf生成的代码)
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
@WebService(targetNamespace="" ,name = "ISearch")//ISearch是wsdl中端口类型名称
public interface ISearch {
@WebResult(name = "name", targetNamespace = "")//name是wsdl中的输出参数
// @RequestWrapper(localName = "getName", targetNamespace = "", className = "GetName")
// @WebMethod(action = "/getName")
// @ResponseWrapper(localName = "getNameResponse", targetNamespace = "" , className = "GetNameResponse")
public java.lang.String getName(
@WebParam(name = "name", targetNamespace = "")
java.lang.String name//"name" 输入参数名
);
}
调用
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
/**
* 调用webservice测试
* @author Administrator
*
*/
public class webserviceTest {
public static void main(String[] args) {
//调用webservice(通过cxf生成的代码) ,http://192.168.3.86:9999/search是通过ESB发布的webservice
JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();
bean.setAddress("http://192.168.3.86:9999/search");
bean.setServiceClass(ISearch.class);
ISearch searchIn = (ISearch) bean.create();
System.out.println(searchIn.getName("cccc"));
}
}