<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.3.3</version>
</dependency>
服务端:
@WebService(targetNamespace = "http://acct.com")
public interface HelloWorld{
@WebMethod
@WebResult(name = "responseInfo")
public String say(@WebParam(name = "requestInfo") String
requestInfo);
}
@Component
@WebService(serviceName="acctSoap",targetNamespace="http://acct.com",endpointInterface = "com.acct.webservice.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
@Override
public String say(String RequestInfo) {
System.out.println(RequestInfo);
}
}
发布服务:
@Configuration
public class CxfConfigServicee {
@Autowired
private HelloWorld helloWorld;
@Bean
public ServletRegistrationBean getDispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/cxf/*");// 发布服务名称 localhost:8080/cxf
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public Endpoint helloWorld() {
EndpointImpl endpoint = new EndpointImpl(springBus(),helloWorld);
endpoint.publish("/helloWorld"); // 接口访问地址
endpoint.getInInterceptors().add(new ServerNameSpaceInterceptor());//忽略命名空间
return endpoint;
}
}
客户端:
public static void main(String[] args) throws Exception{
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8080/cxf/helloWorld?wsdl");
Object[] objects;
String str="你好";
objects = client.invoke("say", str);
System.out.println(objects[0].toString());
}