您的位置:首页 > 编程语言 > Java开发

java API接口文档方法调用

2016-06-20 15:11 435 查看
1.创建.properties文件,将请求的url写入该文件,这样更改请求路径时不用更改代码。

notify.exception.url=http://ip:port/context/exception/happen
notify.recover.url=http://ip:port/context/exception/recover


2.获取配置文件中的url,调用url.openConnection实现接口调用

package com.wondersgroup.cuteinfo.notify.sender;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.gson.Gson;
import com.wondersgroup.test.monitor.persistence.bo.TaskResult;
import com.wondersgroup.test.notify.NotifySender;
import com.wondersgroup.test.notify.model.REST_CallResult;
import com.wondersgroup.test.task.persistence.bo.TaskDefinition;

public class ExceptionNotifySender implements NotifySender{
private static Log log = LogFactory.getLog(ExceptionNotifySender.class);

@Override
public void send(TaskDefinition task, TaskResult taskResult) {
Properties p = new Properties();
try {
//加载配置文件
p.load(ExceptionNotifySender.class.getResourceAsStream("/nitify.rest.properties"));

//获取配置文件中的请求路径,并进行相应的设置
URL localURL = new URL(p.getProperty("notify.exception.url"));
URLConnection connection = localURL.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
//这里是post请求
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setConnectTimeout(30000);

//获取参数
String taskName = task.getName();
Long taskDefId = task.getId();
Long taskResultId = taskResult.getId();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = format.format(new Date());
//传参方式,多个参数
String content = "taskName="+ taskName + "&taskDefId=" + taskDefId + "&taskResultId=" + taskResultId + "&time=" + time + "&message=123";
BufferedOutputStream outBuf = new BufferedOutputStream(connection.getOutputStream());
outBuf.write(content.getBytes());
outBuf.flush();

//获取返回值
byte[] bu = new byte[1024];
int readLength = -1;
InputStream inputStream = httpURLConnection.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((readLength = inputStream.read(bu, 0, bu.length)) != -1) {
out.write(bu, 0, readLength);
}
byte[] comfirm = out.toByteArray();
//关闭IO流
outBuf.close();
inputStream.close();
out.close();

String string = new String(comfirm, "UTF-8");//返回值
REST_CallResult CallResult = new REST_CallResult();
//使用new Gson().fromJson反序列化成类
REST_CallResult fromJson = new Gson().fromJson(string, CallResult.getClass());
//日志打印
log.info("异常发生调用返回----"+fromJson.toString());
} catch (IOException e) {
e.printStackTrace();
log.error("ExceptionNotifySender--->>>send----调用失败");
}

}

public static void main(String[] args){
TaskDefinition taskDefinition = new TaskDefinition();
taskDefinition.setId((long) 1);
taskDefinition.setName("task1");

TaskResult taskResult = new TaskResult();
taskResult.setId((long) 1);

ExceptionNotifySender exceptionNotifySender = new ExceptionNotifySender();
exceptionNotifySender.send(taskDefinition,taskResult);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息