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()); } }