1. 程式人生 > >如何寫一個webService介面

如何寫一個webService介面

第一次寫介面的時候,感覺太過籠統,壓根不知道介面是個什麼東東,,後來自己也查了好多資料,才發現其實介面可以就認為是一個方法,自己多寫幾種以後就會發現挺簡單的,自己整理了一下資料,純屬增強自己的記憶,也方便更多像我這樣的菜鳥們參考。。。

下面是我自己寫的一個案例,用來同步檔案用的這裡我用的流,也可以用mutipartfile:

1.呼叫介面方法:

package com.weichai.test;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import sun.misc.BASE64Encoder; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Test1 { public static void main(String[] args) throws Exception { //1,獲取一個工廠例項 JaxWsDynamicClientFactory FACTORY = JaxWsDynamicClientFactory.newInstance
(); //2,生成針對指定服務介面URL的客戶端 Client client = FACTORY.createClient("http://localhost:80/ws/analysisResultService?wsdl"); //3,呼叫指定的方法,注意入參第一個為方法名稱,第二個為方法的引數(可以傳入一個引數列表,一般為陣列) String jsonStr=getPicStr("F://111.txt"); System.out.println(jsonStr); String loginName ="111"
; String pwd ="2222"; String[] paraArray={loginName,pwd,jsonStr}; Object[] objs = client.invoke("getTaskResultInfo",paraArray); System.out.print(objs[0].toString()); } public static String getPicStr(String imgFile) { InputStream inputStream = null; byte[] data = null; try { inputStream = new FileInputStream(imgFile); data = new byte[inputStream.available()]; inputStream.read(data); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data); } }


2.介面程式碼編寫:

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

import com.alibaba.fastjson.JSON;
import com.weichai.common.mapper.JsonMapper;
import com.weichai.modules.redpacket.entity.RpCodeBinging;
import com.weichai.modules.redpacket.service.RpCodeBingingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder;

import javax.jws.WebService;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebService(endpointInterface = "com.weichai.modules.redpacket.webservice.service.AnalysisResultService", serviceName = "analysisResultService",
        targetNamespace="http://service.webservice.redpacket.modules.weichai.com/")
@Component
public class AnalysisResultServiceImpl implements AnalysisResultService {
    @Autowired
    private RpCodeBingingService rpCodeBingingService;

        /**
         * 同步二維碼webservice介面
         * @param
         * @param
         * @param
         * @return
         */
    public String getTaskResultInfo(String loginName,String pwd , String fileStr){

        //判斷使用者名稱及密碼
        if(!("111".equals(loginName))||!("2222".equals(pwd))){
            return JsonMapper.toJsonString("使用者名稱或密碼錯誤!");
        }
        if (null==fileStr){
            return JsonMapper.toJsonString("檔案為空");
        }
        //建立檔案目錄
        String path = "c://code";
        File file = new File(path);
        if (!file.exists()){
            file.mkdir();
        }

        try {
            String name="112.txt";
            //呼叫方法儲存檔案到本地
            bingingFile(name,path,fileStr);
            long l = System.currentTimeMillis();
            //new日期物件
            Date date = new Date(l);
            //轉換提日期輸出格式
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            name = dateFormat.format(date)+".txt";
            path = "c://codeB";
            File fileB = new File(path);
            if (!fileB.exists()){
                fileB.mkdir();
            }
            //備份,呼叫方法儲存檔案
            bingingFile(name,path,fileStr);
            //呼叫方法獲取物件list
            List<RpCodeBinging> list = getRpCodeBingingsList();
            //呼叫service將檔案儲存到資料庫中
            rpCodeBingingService.saveList(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonMapper.toJsonString("成功!");
    }

    /**
     * 封裝儲存檔案方法
     * @param name
     * @param path
     * @param fileStr
     */
    private static void bingingFile(String name,String path,String fileStr){
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] b = new byte[0];
        try {
            b = decoder.decodeBuffer(fileStr);
            String picPath = path+File.separator+name;
            OutputStream out = new FileOutputStream(picPath);
            out.write(b);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 封裝方法,解析.txt檔案,返回實體物件list集合
     * @return
     * @throws IOException
     */
    private static List<RpCodeBinging> getRpCodeBingingsList() throws IOException {
        InputStream in = new FileInputStream("c://code/112.txt");
        byte[] dat = new byte[in.available()];
        BufferedReader reader = new BufferedReader(new InputStreamReader(in,"utf-8"));
        StringBuffer buffer = new StringBuffer();
        String data="";
        while ((data=reader.readLine())!=null){
            buffer.append(data);
        }
        String text= buffer.toString();
        List<RpCodeBinging> list= new ArrayList<RpCodeBinging>();
        list = JSON.parseArray(text,RpCodeBinging.class);
        return list;
    }

}