耿直的程序员,直接上代码了!基础知识自行脑补吧!
- 启动类添加需要发布的服务名称
String url = "http://192.168.31.204:8081/wsServeice";
Endpoint.publish(url,new WebServiceImpl());
System.out.println("发布webService成功!");
2.定义service
package com.chinatechstar.webservice.server;
import javax.jws.WebMethod;
@javax.jws.WebService public interface WebService {
@WebMethod String sayHello(String user);
}
定义impl
package com.chinatechstar.webservice.server;
import java.util.Date;
@javax.jws.WebService
public class WebServiceImpl implements WebService {
@Override public String sayHello(String user) {
// user = "============================";
return user+" ======测试======== at "+new Date();
}
}
3.客户端调用
package com.chinatechstar.webservice.client;
import com.alibaba.fastjson.JSONObject;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.boot.SpringApplication;
import javax.xml.namespace.QName;
public class WsClient {
public static void main(String[] args) {
JaxWsDynamicClientFactory dcflient=JaxWsDynamicClientFactory.newInstance();
Client client=dcflient.createClient("http://192.168.31.204:8081/wsServeice?wsdl");
try{
Object[] objects=client.invoke("sayHello","老王");
System.out.println("sayHello 调用结果:"+objects[0].toString());
/* Object[] objectall=client.invoke("getUsers"); System.out.println("getUsers调用部分结果:"+objectall[0].toString());*/
}catch (Exception e){ e.printStackTrace();
}
}
}
项目pom文件添加jar包webservice依赖:
<!-- 服务端-start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId> </dependency>
<!-- 服务端-end -->
<!-- 客户端-start -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version> </dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
<!-- 客户端-end -->