如何写一个webService接口
2018-12-18 23:45 GarfieldEr007 阅读(2155) 评论(0) 编辑 收藏 举报第一次写接口的时候,感觉太过笼统,压根不知道接口是个什么东东,,后来自己也查了好多资料,才发现其实接口可以就认为是一个方法,自己多写几种以后就会发现挺简单的,自己整理了一下资料,纯属增强自己的记忆,也方便更多像我这样的菜鸟们参考。。。
下面是我自己写的一个案例,用来同步文件用的这里我用的流,也可以用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;
}
}
---------------------
作者:小小枫叶y叶
来源:CSDN
原文:https://blog.csdn.net/zhang_521521/article/details/79462168
版权声明:本文为博主原创文章,转载请附上博文链接!