書寫webservice接口

1.書寫Service       webservice註解java

package com.weichai.modules.webservice.service;

import javax.jws.WebService;

/**
 * 實驗結果分析webservice
 * @author ltx
 * @version 2017-11-13
 */
@WebService
public interface AnalysisResultService {

    /**
     * 給分析系統調用接口
     * @param id
     * @param imgSuffix
     * @param imgStr
     * @return
     */
    String getTaskResultInfo(String id,String imgSuffix ,String imgStr);

}

2.書寫imp實現類   webservice註解web

package com.weichai.modules.webservice.service.impl;

import com.weichai.common.config.Global;
import com.weichai.common.mapper.JsonMapper;
import com.weichai.common.utils.IdGen;
import com.weichai.common.utils.StringUtils;
import com.weichai.modules.act.service.ActTaskService;
import com.weichai.modules.sys.entity.User;
import com.weichai.modules.sys.utils.DictUtils;
import com.weichai.modules.sys.utils.UserUtils;
import com.weichai.modules.task.dwrUtil.ProtocolDwrUtils;
import com.weichai.modules.task.entity.BenchResourceInfo;
import com.weichai.modules.task.service.BenchResourceInfoService;
import com.weichai.modules.webservice.entity.JsonResult;
import com.weichai.modules.webservice.service.AnalysisResultService;
import com.weichai.modules.webservice.service.BenchOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder;

import javax.jws.WebService;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 臺架預定webservice
 * @author lxm
 * @version 2017-09-13
 */
@WebService(endpointInterface = "com.weichai.modules.webservice.service.AnalysisResultService", serviceName = "analysisResultService",
        targetNamespace="http://service.webservice.modules.weichai.com/")
@Component
public class AnalysisResultServiceImpl implements AnalysisResultService {

    /**
     * 日誌對象
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());
    private String baseUrl = Global.getConfig("dir");
    /**
     * 實驗結果webservice接口
     * @param  id
     * @param imgSuffix:圖片擴展名
     * @param imgStr :base64編碼
     * @return
     */
    public String getTaskResultInfo(String id,String imgSuffix , String imgStr){
        logger.info("調用實驗結果webservice接口-------------------start");
        if (null==imgStr){
            return JsonMapper.toJsonString(new JsonResult(false,"圖片爲空!"));
        }
        if(null==id){
            return JsonMapper.toJsonString(new JsonResult(false,"id爲空!"));
        }
        String picurl = DictUtils.getDictLabel("06", "file_url", "");
        String picName= String.valueOf(System.currentTimeMillis());
        //實驗結果圖片存放路徑
        String path = baseUrl+ File.separator+picurl;
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }
        String picPath = path+File.separator+picName+"."+imgSuffix;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(picPath);
            out.write(b);
            out.flush();
            out.close();
        } catch (Exception e) {
                return JsonMapper.toJsonString(new JsonResult(false,e.getMessage()));
        }
        logger.info("調用實驗結果webservice接口-------------------end");
        return JsonMapper.toJsonString(new JsonResult(true,"操做成功!"));
    }

}

3.spring_context.xml中配置spring

<!-- webService 接口 -->
<import resource="classpath:META-INF/cxf/cxf.xml"/>      //cxf jar包
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>  //cxf jar包

<jaxws:endpoint id="AnalysisResultService"
            implementor="com.weichai.modules.webservice.service.impl.AnalysisResultServiceImpl" address="/analysisResultService" >
</jaxws:endpoint>

4.web.xml中配置apache

<servlet>
   <description>CXF Endpoint</description>
   <display-name>cxf</display-name>
   <servlet-name>cxf</servlet-name>
   <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
   <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
   <servlet-name>cxf</servlet-name>
   <url-pattern>/ws/*</url-pattern>
</servlet-mapping>

5.啓動項目後,在瀏覽器上調用,看看是否生成的wsdl文件正確瀏覽器

http://localhost:8080/ws/analysisResultService?wsdlapp

備註:curl

@WebService(endpointInterface= "com.weichai.modules.webservice.service.AnalysisResultService", serviceName="analysisResultService",targetNamespace="http://service.webservice.modules.weichai.com/")編碼

定義說明書的顯示方法
1.url

@WebService(serviceName="PojoService", portName="PojoPort", name="PojoPortType", targetNamespace="http//:Pojo")
  serviceName 對應 <service name="PojoService">
  portName 對應 <service>下的 <port name="PojoPort">          
      name 對應 <portType name="PojoPortType">spa

     targetNamespace 對應 targetNamespace="http//:Pojo"

2.

    一、serviceName: 對外發布的服務名,指定 Web Service 的服務名稱:wsdl:service。缺省值爲 Java 類的簡單名稱 + Service。(字符串)

    二、endpointInterface: 服務接口全路徑, 指定作SEI(Service EndPoint Interface)服務端點接口

    三、name:此屬性的值包含XML Web Service的名稱。在默認狀況下,該值是實現XML Web Service的類的名稱,wsdl:portType 的名稱。缺省值爲 Java 類的簡單名稱 + Service。(字符串)

    四、portName:  wsdl:portName。缺省值爲 WebService.name+Port。

    五、targetNamespace:指定你想要的名稱空間,認是使用接口實現類的包名的反綴

    六、wsdlLocation:指定用於定義 Web Service 的 WSDL 文檔的 Web 地址。Web 地址能夠是相對路徑或絕對路徑。(字符串)

    注意:實現類上能夠不添加Webservice註解  

相關文章
相關標籤/搜索