springboot整合cxf

 


     org.springframework.boot
     spring-boot-starter-web-services


 
     org.apache.cxf
     cxf-spring-boot-starter-jaxws
     3.3.3

服务端:

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

 

 

你可能感兴趣的:(SpringBoot)