一)在pom.xml文件中引入webservice需要的jar
<!-- spring webservice -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!-- CXF webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.7</version>
</dependency>
二)、创建一个webservice接口类和实现类
package com.oysept.springboot.webservices;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
* 浏览器直接输入访问地址: http://localhost:8080/webservices/WebServices?wsdl
* @author ouyangjun
*/
@WebService(name = "WebServices", // 暴露服务名称
targetNamespace = "http://webservices.springboot.oysept.com/" // 命名空间,一般是接口的包名倒序
)
public interface WebServices {
@WebMethod
@WebResult(name = "helloWebServices", targetNamespace = "")
public String helloWebServices(@WebParam(name = "param") String param);
}
package com.oysept.springboot.webservices.impl;
import javax.jws.WebService;
import org.springframework.stereotype.Component;
import com.oysept.springboot.webservices.WebServices;
@WebService(serviceName = "WebServices", // 与接口中指定的name一致
targetNamespace = "http://webservices.springboot.oysept.com/", // 与接口中的命名空间一致,一般是接口的包名倒序
endpointInterface = "com.oysept.springboot.webservices.WebServices"// 接口地址
)
@Component
public class WebServicesImpl implements WebServices {
@Override
public String helloWebServices(String param) {
return "Hello This is " + param + "!";
}
}
三、由于用到了cxf,新增一个cxf配置类
package com.oysept.springboot.config;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.oysept.springboot.webservices.WebServices;
@Configuration
public class WebServicesConfig {
@Autowired
private Bus bus;
@Autowired
private WebServices webServices;
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, webServices);
endpoint.publish("/WebServices");
return endpoint;
}
}
四)、在application.properties文件中添加一个cxf配置
server.port=8080
cxf.path=/webservices
五)、添加一个webservice测试了,该文章是用代码方式调用webservice,还可以使用常用的一些测试工具。
package com.oysept.springboot.test;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.oysept.springboot.webservices.WebServices;
public class WebServicesTest {
public static void main(String[] args) {
// 接口地址
String address = "http://localhost:8080/webservices/WebServices?wsdl";
// 代理工厂
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 设置代理地址
jaxWsProxyFactoryBean.setAddress(address);
// 设置接口类型
jaxWsProxyFactoryBean.setServiceClass(WebServices.class);
// 创建一个代理接口实现
WebServices cs = (WebServices) jaxWsProxyFactoryBean.create();
// 调用webservices
String result = cs.helloWebServices("WebServices");
// 打印
System.out.println(result);
}
}
六)、项目结构图
springboot启动类:
package com.oysept.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* springboot启动类
* @author ouyangjun
*/
@SpringBootApplication
public class WebservicesApplication {
public static void main(String[] args) {
SpringApplication.run(WebservicesApplication.class, args);
}
}
最后启动WebservicesApplication类,然后再启动webservice测试类,控制台会打印信息。
识别二维码关注个人微信公众号
本章完结,待续,欢迎转载!
本文说明:该文章属于原创,如需转载,请标明文章转载来源!