::solonx-webservices
<plugin>
<groupId>org.noear</groupId>
<artifactId>solonx-webservices</artifactId>
<version>1.0.0</version>
</plugin>
1、描述
此插件基于 apache.cxf 适配,提供方便的 webservices 体验支持
示例源码详见:
2、服务端示例
服务端需要与 solon.boot.jetty 或者 solon.boot.unertow 或者 war 部署方式(目前基于 servlet 实现),配合使用。
- 添加配置(指定 web service 路径段)
server.webservices.path: "/ws/" #默认为 ws
- 添加服务代码
public class ServerTest {
public static void main(String[] args) {
Solon.start(ServerTest.class, args);
}
//@BindingType(SOAPBinding.SOAP12HTTP_BINDING) //可选,申明版本特性
@WebService(serviceName = "HelloService", targetNamespace = "http://demo.solon.io")
public static class HelloServiceImpl {
public String hello(String name) {
return "hello " + name;
}
}
}
启动后,可以通过 http://localhost:8080/ws/HelloService?wsdl
查看 wsdl 信息。
3、客户端示例
- 手写模式
public class ClientTest {
public static void main(String[] args) {
String wsAddress = "http://localhost:8080/ws/HelloService";
HelloService helloService = WebServiceHelper.createWebClient(wsAddress, HelloService.class);
System.out.println("rst::" + helloService.hello("noear"));
}
@WebService(serviceName = "HelloService", targetNamespace = "http://demo.solon.io")
public interface HelloService {
@WebMethod
String hello(String name);
}
}
- 容器模式
使用 @WebServiceReference
注解,直接注入服务。
//测试控制器
@Controller
public class DemoController {
@WebServiceReference("http://localhost:8080/ws/HelloService")
private HelloService helloService;
@Mapping("/test")
public String test() {
return helloService.hello("noear");
}
}
//配置 WebService 接口
@WebService(serviceName = "HelloService", targetNamespace = "http://demo.solon.io")
public interface HelloService {
@WebMethod
String hello(String name);
}
//启动 Solon
public class ClientTest {
public static void main(String[] args) {
Solon.start(ClientTest.class, args);
}
}