spring-boot整合apache cxf 编写webservice服务

最近项目中需要提供webservice服务供第三方调用,现将webService服务的实现总结一下,以便以后在工作中用到少走弯路。
1.引入依赖
compile group:‘org.apache.cxf’,name:‘cxf-spring-boot-starter-jaxws’,version:‘3.3.1’
2.编写WebService服务

package com.open_care.webservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
* webservice服务接口
*name 暴露服务名称
*targetNamespace 名称空间,一般是接口包名的倒序
*/
@WebService(name="IGrumbleWorkOrderService",targetNamespace="http://webservice.open_care.com")
public interface IGrumbleWorkOrderService {
	   @WebResult(name="String",targetNamespace="")
		public String hello(@WebParam(name="userName") String name);
		@WebMethod
	    public String getComplainWorkOrder(String xmlStr);
}

service接口实现
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService(serviceName="IGrumbleWorkOrderService",// 服务名称与接口中的名称保持一致
               targetNamespace="http://webservice.open_care.com",//命名空间与接口中保持一致
       endpointInterface="com.open_care.webservice.IGrumbleWorkOrderService"// 接口完整路径
@Component
public class IGrumbleWorkOrderServiceImpl implements IGrumbleWorkOrderService {
        @Orverride
        public String hello(String name) {
			  return "hello"+name;
		}
        
        public String getComplainWorkOrder(String xmlStr) {
				XmlUtils.xml2Bean();
				return "";
		}
}

3.编写cxf 配置类

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CxfWebServiceConfig {
		@Autowired
		private Bus bus;
		private IGrumbleWorkOrderService grumbleWorkOrderService;
		
		/**
		*  注意此处要标明Bean的名称,不然会出现cxf 找不到的错误
		*/
		@Bean(name="cxf")
		public SpringBus springBus() {
				return new SpringBus();
		}

		@Bean
		public Endpoint endpoint() {
			EndpointImpl endpoint=new EndpointImpl(springBus(),grumbleWorkOrderService);
			/*
			*  GrumbleWorkOrderService 为服务名称
			*发布服务,cxf将服务发布到tomcat容器,通过http://IP:端口/services/服务名称?wsdl
			*  访问
			*
			*/
			endpoint.publish("/GrumbleWorkOrderService");
				return endpoint;
		}
}

4.编写Webservice客户端
客户端有一个需要注意的地方,client端接口的包名必须要和Server端保持一致,否则会提示无法转换
public class WebServiceClient {

   public static void testCallService() {
   		//  这里tomcat端口改成了8090
   		//  webservice地址
		String address="http://localhost:8090/services/GrumbleWorkOrderService?wsdl";
		JaxWsProxyFactoryBean jaxWsProxyFactoryBean=new JaxWsProxyFactoryBean();
		jaxWsProxyFactoryBean.setAddress(address);
		jaxWsProxyFactoryBean.setServiceClass(IGrumbleWorkOrderService.class);
		IGrumbleWorkOrderService grumbleWorkOrderService=(IGrumbleWorkOrderService)jaxWsProxyFactoryBean.create();
		grumbleWorkOrderService.getComplainWorkOrder(xml);
	}

}
参考
http://www.leftso.com/blog/144
https://blog.lqdev.cn/2018/11/12/springboot/chapter-thirty-four/
https://www.xncoding.com/2017/07/13/spring/sb-cxf.html

发布了78 篇原创文章 · 获赞 20 · 访问量 43万+

猜你喜欢

转载自blog.csdn.net/tangyajun_168/article/details/98769193
今日推荐