CXF发布webService(一)

介绍
Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 容器中或者嵌入式的jetty中。CXF 支持以下 Web 服务标准:
Java API for XML Web Services (JAX-WS)

SOAP
Web 服务描述语言(Web Services Description Language ,WSDL)
消息传输优化机制(Message Transmission Optimization Mechanism,MTOM)
WS-Basic Profile
WS-Addressing
WS-Policy
WS-ReliableMessaging
WS-Security
利用CXF框架发布webService时,可以选择使用本地jetty(CXF内建jetty容器),也可以选择web容器。

使用tomcat发布webservice
使用web容器发布webservice时,可以借助spring(spring集成了CXF)更方便发布。
先需要在web.xml中进行对cfx的接口进行拦截

<servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/ws/*</url-pattern>
</servlet-mapping>
注: 这里,配置了springmvc和cxf两个servlet。如果只使用cxf发布webService,那么配CXFServlet即可。

pom.xml文件:引入依赖cxf

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>${cxf.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>${cxf.version}</version>
 </dependency>

在这里插入图片描述

2、服务类、实现类:
在这里插入图片描述
@Service
@WebService(
endpointInterface = “com.jushi.procurement.ws.IProductInfoErpService”,
serviceName = “ProductInfoErpGT”
)
/**

  • 从erp里面拉取商品数据
    */
    public class ProductInfoErpServiceImpl implements IProductInfoErpService {
    private final Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private ProductInfoMapper productInfoMapper;
    @Autowired
    private IProductInfoService productInfoService;

    @Autowired
    private CategoryMapper categoryMapper;

    public ResponseMessage syncProductsToErp(List products){
    ProductInfo productInfo=new ProductInfo();
    ResponseMessage rm = new ResponseMessage();
    StringBuffer sb = new StringBuffer();
    StringBuffer categorysb = new StringBuffer();
    int error=0;
    String industryType=null;
    for(ProductInfoErp productInfoErp:products){
    try {
    productInfo.setPrdNo(productInfoErp.getProductCode());
    //productInfo1==null表示数据库里面没有这条数据要插入其他情况通过erp物料主键对商品数据进行更改
    ProductInfo productInfo1=productInfoMapper.selectErpProductId(productInfoErp.getErpProductId());
    //对照ERP类别编码在我们系统里面是否存在
    /* Category category=categoryMapper.selectByCategoryName(productInfoErp.getCategoryName());*/

             Category category=null;
             if(StringUtils.isNotBlank(productInfoErp.getCategoryCode())) {
                 category=categoryMapper.selectByCategoryCode(productInfoErp.getCategoryCode());
                 if(null==category){
                     error=1;
                 }else{
                    industryType=categoryMapper.selectByIindustryType(category.getCategoryCode());
                     productInfo.setPurcNo(category.getCategoryCode());
                 }
             }
             if(StringUtils.isBlank(productInfoErp.getProductName()) || StringUtils.isBlank(productInfoErp.getProductCode()) || StringUtils.isBlank(productInfoErp.getErpProductId())){
                 log.info("productInfoErp.getCategoryCode()"+productInfoErp.getCategoryCode());
                 sb.append(productInfoErp.getErpProductId()+",");
                 continue;
             }
             if(StringUtils.isNotBlank(productInfoErp.getConversionRates())) {
                 BigDecimal bd = new BigDecimal(productInfoErp.getConversionRates());
                 productInfo.setConversionRates(bd);
             }
             if(StringUtils.isNotBlank(productInfoErp.getUnitCode())) {
                 productInfo.setUnitCode(productInfoErp.getUnitCode());
             }
             if(StringUtils.isNotBlank(productInfoErp.getCategoryCode())) {
                 productInfo.setPurcNo(productInfoErp.getCategoryCode());
             }
             if(StringUtils.isNotBlank(productInfoErp.getSubUnitCode())) {
                 productInfo.setSubUnitCode(productInfoErp.getSubUnitCode());
             }
            if(1==error){
                log.info("productInfoErp.getCategoryCode()1"+productInfoErp.getCategoryCode());
                sb.append(productInfoErp.getErpProductId()+",");
             }else{
                 if(null==productInfo1){
                     productInfo.setErpProductId(productInfoErp.getErpProductId());
                     productInfo.setName(productInfoErp.getProductName());
                     productInfo.setPrdNo(productInfoErp.getProductCode());
                     productInfo.setIndustryType(industryType);
                     this.productInfoService.insertSelective(RequestHelper.newEmptyRequest(), productInfo);
                 }else{
                     productInfo.setErpProductId(productInfoErp.getErpProductId());
                     productInfo.setName(productInfoErp.getProductName());
                     productInfo.setProductId(productInfo1.getProductId());
                     productInfo.setPrdNo(productInfoErp.getProductCode());
                     productInfo.setLastUpdateDate(new Date());
                     productInfo.setIndustryType(industryType);
                     this.productInfoService.updateByPrimaryKey(RequestHelper.newEmptyRequest(),productInfo);
                 }
             }
         } catch (NumberFormatException e){
             e.printStackTrace();
             sb.append(productInfoErp.getErpProductId()+",");
         }catch (Exception e){
             e.printStackTrace();
             sb.append(productInfoErp.getErpProductId()+",");
    
         }
     }
     List<String>  errorList=stringToList(sb.toString());
    /* List<String>  errorList1=stringToList(categorysb.toString());*/
    

    /* if(sb.length()>0&&products.size()!=errorList.size()){
    rm.setReturnCode(ResponseMessage.SYSTEM_PART_ERROR);
    // rm.setReturnDesc(ResponseMessage.SYSTEM_PART_ERROR_DESC);//
    rm.setReturnData(sb.toString());
    // if(errorList1.size()>0){
    rm.setReturnCategoryCode(“这些三级类别名称系统不存在”+categorysb.toString());
    }//
    } else*/ if(sb.length()>0&&products.size()==errorList.size()){
    rm.setReturnCode(ResponseMessage.SYSTEM_ERROR);
    /* rm.setReturnDesc(ResponseMessage.SYSTEM_ERROR_DESC);/
    rm.setReturnData(sb.toString());
    /
    if(errorList1.size()>0){
    rm.setReturnCategoryCode(“这些三级类别名称系统不存在”+categorysb.toString());
    }/
    }else{
    rm.setReturnCode(ResponseMessage.SUCCESS);
    /
    rm.setReturnDesc(ResponseMessage.SUCCESS_DESC);*/
    }

     return  rm;
    

    }
    public List stringToList(String data){
    List list= new ArrayList();
    if (!data.trim().equals("")) {
    String[] listIds = data.split(",");
    for (int i = 0; i < listIds.length; i++) {
    list.add(listIds[i]);
    }
    }
    return list;
    }

}

最主要的地方就是cfx的配置 可以不用Endpoint和JaxWsServerFactoryBean 两种方式对webservice的发布
就不需要这种发布方式了在这里插入图片描述
在这里插入图片描述
直接把项目上所有的接口都配置一个cxf接口就好了

<?xml version="1.0" encoding="UTF-8"?>

<!-- WS-Security拦截器 校验用户名密码 -->
<bean id="serverInInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
    <constructor-arg>
        <map>
            <entry key="action" value="UsernameToken"/>
            <!-- 密码类型,PasswordText表示明文 ,PasswordDigest加密-->
            <entry key="passwordType" value="PasswordText"/>
            <entry key="passwordCallbackRef">
                <!-- 回调函数引用 -->
                <bean id="serverPasswordCallback" class="com.itl.iap.demo.ws.ServerPasswordCallback">
                    <!--校验用户名和密码-->
                    <property name="username" value="admin"></property>
                    <property name="password" value="admin"></property>
                </bean>
            </entry>
        </map>
    </constructor-arg>
</bean>
<bean id="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor">

</bean>
<bean id="outMessageInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor">

</bean>
<bean id="service1" class="com.itl.iap.demo.ws.HelloWorldImpl"/>
<jaxws:endpoint id="hello" implementor="#service1" address="/HelloWorldService" >
    <jaxws:inInterceptors>
           <ref bean="serverInInterceptor"></ref>
    </jaxws:inInterceptors>
</jaxws:endpoint>

<bean id="productInfoErpService" class="com.jushi.procurement.ws.impl.ProductInfoErpServiceImpl"/>
<jaxws:endpoint id="productInfo" implementor="#productInfoErpService" address="/IProductInfoErpService" >
    <!-- <jaxws:inInterceptors>
            <ref bean="serverInInterceptor"></ref>
     </jaxws:inInterceptors>-->
</jaxws:endpoint>

<!--供应商拉取接口-->
<bean id="supplierErpService" class="com.jushi.supplier.ws.impl.SupplierErpServiceImpl"/>
<jaxws:endpoint id="supplierErp" implementor="#supplierErpService" address="/ISupplierErpService" >
</jaxws:endpoint>


<!--供应商状态修改接口-->
<bean id="supplierStatusService" class="com.jushi.supplier.ws.impl.SupplierStatusServiceImpl"/>
<jaxws:endpoint id="supplierStatus" implementor="#supplierStatusService" address="/ISupplierStatusService" >
    <jaxws:inInterceptors>
        <ref bean="serverInInterceptor"></ref>
    </jaxws:inInterceptors>
</jaxws:endpoint>

<!--采购方案状态修改接口-->
<bean id="programOAService" class="com.jushi.procurement.ws.impl.ProgramOAServiceImpl"/>
<jaxws:endpoint id="programStatus" implementor="#programOAService" address="/IProgramOAService" >
    <jaxws:inInterceptors>
        <ref bean="serverInInterceptor"></ref>
    </jaxws:inInterceptors>
</jaxws:endpoint>
 </beans>

这样就简单完成了cxf对webservice的发布
在这里插入图片描述

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐