相关jar包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--webService相关jar包 start-->
<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.11</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.11</version>
</dependency>
<!--webService相关jar包 end-->
webService接口
@WebService(name = "GetCharge", // 暴露服务名称
targetNamespace = "http://webService.charge.jdkj.com"// 命名空间
)
public interface GetCharge {
//标注该方法为webservice暴露的方法,用于向外公布,它修饰的方法是webservice方法
String getCharge(String msg);
}
接口的实现类
@WebService(serviceName = "GetCharge", //与接口中的serviceName一致
targetNamespace = "http://webService.charge.jdkj.com", //与接口中的targetNamespace一致
endpointInterface = "com.jdkj.charge.webService.GetCharge")
@Component
public class GetChargeImpl implements GetCharge {
@Override
public String getCharge(String msg) {
if (msg == null) {
return XMLUtil.convertToXml(new XmlMsgReturn("0", "接收的msg为空,请检查"));
}
return XMLUtil.convertToXml(new XmlMsgReturn("0", "调用完成。"));
}
}
cxf配置类编写
@Configuration
public class CxfConfig {
@Autowired
private GetCharge getCharge;
@Bean
public ServletRegistrationBean dispatcherServlet1() {
return new ServletRegistrationBean(new CXFServlet(), "/getCharge/*");
}
//此处必须使用此方式获取Bus类的对象,使用@Autowired注解注入会报错,那是因SpringBoot版本过高导致
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), getCharge);
endpoint.publish("/api");
return endpoint;
}
}
测试
