需要的依赖:
<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>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.4</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
service:
@WebService(name = "DemoService", // 暴露服务名称
targetNamespace = "http://service.prj.sharePlatform.hnykx.com"// 命名空间,一般是接口的包名倒序
)
public interface DemoService {
@WebMethod
@WebResult(name = "String",targetNamespace = "")
public String hello(@WebParam(name = "str")String str);
}
serviceimpl:
@WebService(serviceName = "DemoService", // 与接口中指定的name一致
targetNamespace = "http://service.prj.sharePlatform.hnykx.com", // 与接口中的命名空间一致,一般是接口的包名倒
endpointInterface = "com.hnykx.sharePlatform.prj.service.DemoService"// 接口地址
)
public class DemoServiceImpl implements DemoService {
@Override
public String hello(String str) {
return "hello" + str;
}
}
CxfConfig:
@Configuration
public class CxfConfig {
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public DemoService demoService() {
return new DemoServiceImpl();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());
endpoint.publish("/api");
return endpoint;
}
}