Spring+CXF的webservice接口的搭建

1.引入包

經過測試引入以下包可運行

2.編寫service接口和實現類

package com.px.train.service;

import javax.jws.WebMethod;
import javax.jws.WebService;

/**
 * @description:培訓上崗申請
 * @author:weiwei
 * @date:2018/8/6 16:14
 */
@WebService
public interface ApplyPostService {
    
    /**
     * 培訓結束用戶列表
     */
    @WebMethod
    public String applyUserList();
}

package com.px.train.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebService;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.px.train.model.TrainClassStudent;
import com.px.train.service.ApplyPostService;
import com.px.train.service.TrainClassService;

@WebService(endpointInterface="com.px.train.service.ApplyPostService",serviceName="applyService")  
public class ApplyPostServiceImpl implements ApplyPostService {
    private static Logger LOGGER = LoggerFactory.getLogger(ApplyPostServiceImpl.class);
    @Autowired
    private TrainClassService trainClassService;
    @WebMethod(operationName= "applyUserList")
    public String applyUserList() {
        // TODO Auto-generated method stub
        //List<TrainClassStudent> userList=trainClassService.getSudentList();
        List<TrainClassStudent> userList=new ArrayList<TrainClassStudent>();
        for (int i=0;i<4;i++){
            TrainClassStudent po=new TrainClassStudent();
            po.setUserId(i+1+"Id");po.setName("name"+i);
            po.setJobNumber("jobNumber"+i);po.setOrgId("orgId"+i);
            po.setSex(i%2);po.setStuTel("stuTel"+i+""+i);
            po.setEmail("email"+i);po.setCertificateId("certificateId"+i);
            po.setRealEndDate(new Date());
            userList.add(po);
        }
        String xmlStr=getUsersXml(userList);
        System.out.println(xmlStr);
        return xmlStr;
    }
    @WebMethod(exclude= true)
    public String getUsersXml(List<TrainClassStudent> userList) {
        // TODO Auto-generated method stub
        Document document = DocumentHelper.createDocument();
        //添加以根節點
        Element root = document.addElement("userList");

        for (TrainClassStudent user : userList) {
            Element area = root.addElement("user");
            area.addElement("id").setText(StringUtils.isEmpty(user.getUserId())?"":user.getUserId());
            area.addElement("name").setText(StringUtils.isEmpty(user.getName())?"":user.getName());
            area.addElement("code").setText(StringUtils.isEmpty(user.getJobNumber())?"":user.getJobNumber());
            area.addElement("orgId").setText(StringUtils.isEmpty(user.getOrgId())?"":user.getOrgId());
            area.addElement("sex").setText(StringUtils.isEmpty(user.getSex().toString())?"":user.getSex().toString());
            area.addElement("phone").setText(StringUtils.isEmpty(user.getStuTel())?"":user.getStuTel());
            area.addElement("email").setText(StringUtils.isEmpty(user.getEmail())?"":user.getEmail());
            area.addElement("certiCode ").setText(StringUtils.isEmpty(user.getCertificateId())?"":user.getCertificateId());
            area.addElement("ifTrain  ").setText("true");
            area.addElement("trainTime  ").setText(StringUtils.isEmpty(user.getRealEndDate().toString())?"":user.getRealEndDate().toString());
        }

        return document.asXML();
    }
    
}

webservice默認public方法都會公開當設置@WebMethod(exclude= true)時方法不公開

3.測試類編寫

package com.px.train.util;

import javax.xml.ws.Endpoint;

import com.px.train.service.impl.ApplyPostServiceImpl;

public class MyServer {

    public static void main(String[] args) {
        //發佈服務
        ApplyPostServiceImpl service=new ApplyPostServiceImpl();
        System.out.println("star");
        String result=service.applyUserList();
        System.out.println(result);
        Endpoint.publish("http://localhost:8080/px/applyServer", service);
        System.out.println("webservice發佈成功!");
    }

}

運行如以下表示發佈成功:

在瀏覽器上輸入地址:http://10.155.66.171:8080/px/applyServer?wsdl

 4,實現自動發佈微博service,本人使用的時CXF,webservice發佈方式有sop,axis等幾種可自行選擇發佈方式

首先修改web.xml配置文件

<!-- 繼承主配置文件 -->

   <context-param>

     <param-name>contextConfigLocation</param-name>

     <param-value>

            classpath:applicationContext.xml,

            classpath:applicationContext-shiro.xml

        </param-value>

   </context-param>

<!—CXFServlet控制器 -->

   <servlet>

     <servlet-name>CXFServlet</servlet-name>

     <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

      <load-on-startup>1</load-on-startup>

   </servlet>

   <servlet-mapping>

     <servlet-name>CXFServlet</servlet-name>

     <url-pattern>/webservice/*</url-pattern>

   </servlet-mapping>

然後修改applicationContext.xml配置文件:

紅線標記爲必須的

xmlns:jaxws="http://cxf.apache.org/jaxws

http://cxf.apache.org/jaxws

http://cxf.apache.org/schemas/jaxws.xsd

<!-- cxf的一些核心配置 -->

      <import resource="classpath:META-INF/cxf/cxf.xml" />

      <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <!-- webservice地址 -->

    <jaxws:endpoint id="applyService" implementor="com.px.train.service.impl.ApplyPostServiceImpl" address="/applyService" />

發佈項目,訪問地址http://localhost:8080/px/webservice/applyService?wsdl

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章