一.创建SpringBoot项目
二.导入必需jar(pom.xml)
<!--webService依赖-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.4</version>
</dependency>
三. 服务接口
package com.joinforwin.hiswebservice; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; /** * @author gj * @date 2020/3/17 */ //指定命名空间,地址是所在包名的反写 @WebService(targetNamespace = "http://hiswebservice.joinforwin.com") public interface UserService { @WebMethod(operationName = "getHello")//在wsdl文档中显示的方法名,@WebMethod可不指定,默认与方法名相同 String getHello(@WebParam(name="name") String name );}
四.接口实现类
package com.joinforwin.hiswebservice;
import org.springframework.stereotype.Component;
/**
* @author : gj
* create at: 2020/3/17 9:59 AM
* @description:
*/
@Component
public class UserServiceImpl implements UserService {
@Override
public String getHello(String name) {
return "hello " + name;
}
}
五.服务发布配置类
package com.joinforwin.hiswebservice.config;
import com.joinforwin.hiswebservice.UserService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.*;
@Configuration
public class CxfConfig {
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
/**
* 此方法被注释后:wsdl访问地址为http://127.0.0.1:8080/services/user?wsdl
* 去掉注释后:wsdl访问地址为:http://127.0.0.1:8080/test/user?wsdl
*/
@Bean
public ServletRegistrationBean webServiceServlet(){
return new ServletRegistrationBean(new CXFServlet(),"/test/*");
}
@Autowired
private UserService userService;
/**
* 发布服务
* 指定访问url
* @return
*/
@Bean
public Endpoint userEndpoint(){
EndpointImpl endpoint = new EndpointImpl(springBus(),userService);
endpoint.publish("/user");
return endpoint;
}
}
六.项目启动后的wsdl信息

本文介绍了如何使用SpringBoot结合Cxf创建并发布WebService服务。从创建SpringBoot项目开始,逐步讲解了添加依赖、定义服务接口和实现类,以及配置服务发布的详细步骤,最后展示了项目启动后生成的wsdl信息。
1004

被折叠的 条评论
为什么被折叠?



