WebService就是Http+XML的文本模式传输
webservice必须是Web服务器,所以sping-web是必须的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.6</version>
</dependency>
application.properties配置一个端口号(webservice本质是Http)
server.port=8081
WebService 服务端
1) 定义接口,argetNamespace是必须要的
@WebService(targetNamespace = "http://zs.example.com")
public interface ServerService {
@WebMethod
String hello(@WebParam String data);
}
2) 实现接口
@Service
@WebService(name = "ServerService",
serviceName = "ServerServiceImpl",
portName = "ServerServicePort",
targetNamespace = "http://zs.example.com",
endpointInterface = "com.example.zs.ServerService")
public class ServerServiceImpl implements ServerService {
@Override
public String hello(String data) {
return "hello " + data + " welcome !!!";
}
}
name = "ServerService" 映射成 <wsdl:portType name="ServerService">
serviceName = "ServerServiceImpl" 映射成 <wsdl:service name="ServerServiceImpl">
portName = "ServerServicePort" 映射成 <wsdl:portType name="ServerService">
endpointInterface是接口完整包名.类名
targetNamespace是反着的包名
3)发布WebService个服务
@Configuration
public class WebServiceConfig {
@Autowired
private ServerService serverService;
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
/**
* 发布到/ws/api
* @return
*/
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), serverService);
endpoint.publish("/ws/api");
return endpoint;
}
}
客户端调用WebService服务
1)直接调用地址
@Test
public void simple()
{
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://127.0.0.1:8081/services/ws/api?wsdl");
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("hello", "zs");
System.out.println("返回数据:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
2)使用@WebServiceClient注解,代理成对象调用
@javax.xml.ws.WebServiceClient(targetNamespace = "http://zs.example.com",
name="ServerService",
wsdlLocation = "http://127.0.0.1:8081/services/ws/api?wsdl")
public class WebServiceClient extends Service {
private static final QName SERVICE_NAME;
private static final QName SERVICE_PORT;
static {
SERVICE_NAME = new QName("http://zs.example.com", "ServerServiceImpl");
SERVICE_PORT = new QName("http://zs.example.com", "ServerServicePort");
}
// 注意,这里初始化的是SERVICE_NAME
public WebServiceClient() throws MalformedURLException {
super(new URL("http://127.0.0.1:8081/services/ws/api?wsdl"), SERVICE_NAME);
}
// 注意,这里按照SERVICE_PORT获取代理对象
@WebEndpoint(name = "ServerServiceProxy")
public ServerService getServerServiceProxy(){
return super.getPort(SERVICE_PORT, ServerService.class);
}
public String call(String s){
ServerService serverService = getServerServiceProxy();
return serverService.hello("zs");
}
}
@Test
public void webserviceClientTest() throws Exception{
WebServiceClient webServiceClient = new WebServiceClient();
String res = webServiceClient.call("zs");
System.out.println(res);
}