config类:
package com.cxf.config;
import javax.xml.ws.Endpoint;
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.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.cxf.service.HelloService;
import com.cxf.service.HelloServiceImpl;
@Configuration
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
@Override
public void customize(ConfigurableServletWebServerFactory server) {
server.setPort(9090);
}
/**
* @return
* 使用此方法和上面的功能一致,只不过可配置项增多,只要其中一个即可
*/
@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.setPort(9090);
return factory;
}
@Bean
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new CXFServlet(), "/services/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus()
{
return new SpringBus();
}
@Bean
public HelloService helloService()
{
return new HelloServiceImpl();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint=new EndpointImpl(springBus(), helloService());//绑定要发布的服务
endpoint.publish("/hello"); //显示要发布的名称
return endpoint;
}
//本地输入http://localhost:9090/services/hello?wsdl就可以了
}
service类
package com.cxf.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface HelloService {
@WebMethod
String sayHello(@WebParam(name = "name") String name);
}
serviceImpl类:
package com.cxf.service;
import javax.jws.WebService;
//备注说明:接口实现类名称前的注解targetNamespace是当前类实现接口所在包名称的反序(PS:加上反斜线),endpointInterface是当前需要实现接口的全称;
@WebService(targetNamespace="http://service.cxf.com/",endpointInterface="com.cxf.service.HelloService",serviceName="HelloService")
public class HelloServiceImpl implements HelloService{
@Override
public String sayHello(String name) {
System.out.println("your name is:"+name);
return "hello "+ name;
}
}
启动服务:
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Starter {
public static void main(String[] args) {
SpringApplication.run(Starter.class, args);
System.out.println("启动成功.....");
}
}
客户端调用:
package com.cxf.client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
public class HelloClient {
public static void main(String args[]) throws Exception {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:9090/services/hello?wsdl");
Object[] objects = client.invoke("sayHello", "wangsong");
// 输出调用结果 *****hello wangsong
System.out.println("*****" + objects[0].toString());
}
}