import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class WebServiceClient {
public static void main(String[] args) throws Exception {
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(“http://192.168.0.105:8080/Axis2Demo_war_exploded/services/HelloService”);
options.setTo(targetEPR);
Object[] entryArgs = new Object[]{4, 2};
QName qName = new QName(“http://ws.apache.org/axis2”, “add”);
Object result = serviceClient.invokeBlocking(qName, entryArgs, new Class[]{int.class})[0];
qName = new QName(“http://ws.apache.org/axis2”, “send”);
serviceClient.invokeRobust(qName, new Object[]{“hello world!”});
}
}
Soap UI
Apache CXF
==========
Apache CXF是一个开源的、全功能的,容易使用的Web服务框架。CXF是两个项目的结合:由IONA技术公司开发的Celtix和由Codehaus主持的团队开发的XFire。
CXF支持的特性非常广泛,但特性主要在以下一些方面:
-
支持的Web服务标准包括: SOAP WS-Addressing WS-Policy WS-ReliableMessaging WS-Security WS-SecurityPolicy WS-SecureConversation
-
JAS-WS API,用于Web服务开发 WSDL优先支持工具 Java优先支持
-
JAX-RS(JSR 311 1.0)API,用于RESTful Web服务开发
-
…
⬆️内容摘自Wiki百科。
发布服务
====
使用 Maven 构建项目,POM文件内容如下:
<?xml version="1.0" encoding="UTF-8"?><project xmlns=“http://maven.apache.org/POM/4.0.0”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
org.example
CXFDemo
1.0-SNAPSHOT
org.apache.cxf
cxf-rt-frontend-jaxws
3.4.0
org.apache.cxf
cxf-rt-transports-http
3.4.0
org.apache.cxf
cxf-rt-transports-http-jetty
3.4.0
org.apache.cxf
cxf-rt-transports-http-jetty
3.4.0
编写一个服务接口,定义 sayHi 方法:
package org.example.services;
import javax.jws.WebService;
// 声明这是一个Ws服务接口
@WebService
public interface HelloWorld {
// 定义服务方法
String sayHi(String name);
}
编写一个服务接口的实现类:
package org.example.services;
import javax.jws.WebService;
@WebService(endpointInterface = “org.example.services.HelloWorld”, serviceName = “HelloWorld”)
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String name) {
return "hi, " + name;
}
}
再编写一个发布服务的主类 Main :
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.example.services.HelloWorld;
import org.example.services.HelloWorldImpl;
public class Main {
public static void main(String[] args) {
HelloWorldImpl implementor = new HelloWorldImpl();
JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
svrFactory.setServiceClass(HelloWorld.class);
svrFactory.setAddress(“http://localhost:9000/helloworld”);
svrFactory.setServiceBean(implementor);
svrFactory.create();
}
}
运行后即可访问
http://localhost:9000/helloworld?wsdl 查看该服务,从WSDL的描述文件中也能看出 HelloWorld 服务提供了 sayHi 方法,且该方法需要一个字符串参数,返回值也为字符串。
使用 SoapUI 调用该方法示例:
通过代码也可以实现客户端调用 Web Service :
import org.example.services.HelloWorld;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
public class WsClient {
private static final QName SERVICE_NAME = new QName(“http://services.example.org/”, “HelloWorld”);
private static final QName PORT_NAME = new QName(“http://services.example.org/”, “HelloWorldPort”);
public static void main(String[] args) {
Service service = Service.create(SERVICE_NAME);
String endpointAddress = “htt 《大厂前端面试题解析+Web核心总结学习笔记+企业项目实战源码+最新高清讲解视频》无偿开源 徽信搜索公众号【编程进阶路】 p://localhost:9000/helloworld”;
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
HelloWorld helloWorld = service.getPort(HelloWorld.class);
System.out.println(helloWorld.sayHi(“SearchNull”));
}
}
Spring整合CXF
===========
在Spring框架中可以集合 Apache CXF 框架发布 Web Service 服务,通过 Maven 或其他方式将CXF所需的Jar包导入项目中,编写 Web Service 接口及实现类即可。
package org.example.webservices;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
-
@WebService注解标记一个webservice接口
-
@param targetNamespace 指定命名空间,默认使用包名的反转.
-
@param serviceName 对外发布的服务名称
-
@param portName wsdl:portName,默认为服务名 + Port
*/
@WebService(
targetNamespace = “http://services.example.org/”,
serviceName = “HelloService”,
portName = “HelloServicePort”
)
public interface IHelloService {
/**
-
@WebResult注解指定从返回值至WSDL或XML元素的映射,将此注解应用于客户端或服务端接口(SEI)方法之上。
-
@param name 当返回值列示在 WSDL 文件中并且在连接上的消息中找到该返回值时,指定该返回值的名称。对于 RPC 绑定,这是用于表示返回值的
-
@param targetNamespace 指定返回值的 XML 命名空间。仅当操作类型为 RPC 或者操作是文档类型并且参数类型为 BARE 时才使用此参数。(字符串)
-
@WebMethod注解表示作为 Web Service 的方法,将此注解应用于服务端接口(SEI)的方法上。
-
@param operationName 指定与此方法相匹配的 wsdl:operation 的名称,默认值为Java方法的名称
-
@WebParam注解用于指定方法参数即Web Service方法的消息部件和XML元素的映射
-
@param name 参数的名称,如果操作是RPC(远程过程调用)类型并且未指定portName属性,则表示参数的wsdl:port属性的名称
*/
@WebResult(name = “helloRequest”, targetNamespace = “http://services.example.org/”)
@WebMethod(operationName=“sayHello”)
public String hello(@WebParam(name = “msg”)String name);
}
xxxxxxxxxxbr package org.example.webservices;brbrimport javax.jws.WebMethod;brimport javax.jws.WebParam;brimport javax.jws.WebResult;brimport javax.jws.WebService;brbr/**br * @WebService注解标记一个webservice接口br br * @param targetNamespace 指定命名空间,默认使用包名的反转.br * @param serviceName 对外发布的服务名称br * @param portName wsdl:portName,默认为服务名 + Portbr/brbr@WebService(br targetNamespace = “http://services.example.org/”,br serviceName = “HelloService”,br portName = "HelloServicePort"br)brpublic interface IHelloService {br /**br * @WebResult注解指定从返回值至WSDL或XML元素的映射,将此注解应用于客户端或服务端接口(SEI)方法之上。br *br * @param name 当返回值列示在 WSDL 文件中并且在连接上的消息中找到该返回值时,指定该返回值的名称。对于 RPC 绑定,这是用于表示返回值的br * @param targetNamespace 指定返回值的 XML 命名空间。仅当操作类型为 RPC 或者操作是文档类型并且参数类型为 BARE 时才使用此参数。(字符串)br *br * @WebMethod注解表示作为 Web Service 的方法,将此注解应用于服务端接口(SEI)的方法上。br *br * @param operationName 指定与此方法相匹配的 wsdl:operation 的名称,默认值为Java方法的名称br *br * @WebParam注解用于指定方法参数即Web Service方法的消息部件和XML元素的映射br * @param name 参数的名称,如果操作是RPC(远程过程调用)类型并且未指定portName属性,则表示参数的wsdl:port属性的名称br */br @WebResult(name = “helloRequest”, targetNamespace = “http://services.example.org/”)br @WebMethod(operationName=“sayHello”)br public String hello(@WebParam(name = “msg”)String name);br}br
Web Service 接口的实现类
package org.example.webservices.impl;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import org.springframework.stereotype.Service;
import org.exmaple.webservices.IHelloService;
@WebService(
endpointInterface = “org.example.services.IHelloService”
)
@SOAPBinding(style=Style.RPC)
public class IHelloServiceImpl implements IHelloService {
@Override
public String hello(String name) {
return "Hello, " + name;
}
}
xxxxxxxxxxbr package org.example.webservices.impl;brbrimport javax.jws.WebService;brimport javax.jws.soap.SOAPBinding;brimport javax.jws.soap.SOAPBinding.Style;brbrimport org.springframework.stereotype.Service;brimport org.exmaple.webservices.IHelloService;brbr@WebService(br endpointInterface = "org.example.services.IHelloService"br)br@SOAPBinding(style=Style.RPC)brpublic class IHelloServiceImpl implements IHelloService {br @Overridebr public String hello(String name) {br return "Hello, " + name;br }br}br
实现 Web Service 的服务端后则需要对Spring项目整合CXF的一些配置,在 web.xml 中配置CXF框架路由。