转自:https://blog.csdn.net/qq_41027942/article/details/83149959
转自:https://blog.csdn.net/wangyuanjun008/article/details/79121687
服务发布:
pom:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
代码:Application 类
package com.zjxt.webservicedemo;
import com.zjxt.webservicedemo.service.impl.WebServiceImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.xml.ws.Endpoint;
@SpringBootApplication
public class WebservicedemoApplication {
public static void main(String[] args) {
SpringApplication.run(WebservicedemoApplication.class, args);
String url = "http://localhost:8081/wsServeice";
Endpoint.publish(url,new WebServiceImpl());
System.out.println("发布webService成功!");
}
}
service类:
package com.zjxt.webservicedemo.service;
import javax.jws.WebMethod;
@javax.jws.WebService
public interface WebService {
@WebMethod
String sayHello(String user);
}
package com.zjxt.webservicedemo.service.impl;
import com.zjxt.webservicedemo.service.WebService;
import java.util.Date;
@javax.jws.WebService
public class WebServiceImpl implements WebService {
@Override
public String sayHello(String user) {
return user+" sayHello at "+new Date();
}
}
然后运行主类,启动程序,控制台打印出发布webservice成功!访问 http://localhost:8081/wsServeice?wsdl(注意一定要加wsdl),浏览器显示一个xml文档,表示webservice服务发布成功了。
服务访问:
pom:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<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>
</dependencies>
测试类:
package com.zjxt.gpsdataservice.test;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
public class Test1 {
public static void main(String[] args) {
// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
// Client client = dcf.createClient("http://localhost:8081/wsServeice?wsdl");
Client client = dcf.createClient("http://localhost:8081/wsServeice?wsdl");
// 需要密码的情况需要加上用户名和密码
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("sayHello", "老王");
System.out.println("返回数据:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
}
测试类需要与服务发布不在同一个项目中,否则会报错,目前我还没解决。