你是否遇到过 protobuf 接口难易测试?可以按照以下代码去实现(部分工具代码未贴出可以自己实现):
import com.google.protobuf.ByteString;
import com.google.protobuf.GeneratedMessageV3;
import com.googlecode.protobuf.format.JsonFormat;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.cglib.reflect.FastClass;
import org.springframework.cglib.reflect.FastMethod;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class PbUtils {
public static void potoPost(HttpPost httpPost, String token, InputStreamEntity inputStreamEntity, Class pbClass) throws IOException {
DefaultHttpClient httpclient = new DefaultHttpClient();
httpPost.addHeader("Content-Type", "application/x-protobuf");
httpPost.addHeader("Accept", "application/x-protobuf");
httpPost.addHeader("UserAgent", "Android/localDevelop version/7.1.0");
if (StringUtils.isNotEmpty(token)) {
httpPost.addHeader("userToken", token);
}
if (pbClass == null) {
System.out.println("pbClass不能为空");
return;
}
httpPost.setEntity(inputStreamEntity);
HttpResponse response = httpclient.execute(httpPost);
JSONObject resultData = new JSONObject();
try {
HttpEntity entity = response.getEntity();
ByteArrayOutputStream buf = new ByteArrayOutputStream();
entity.writeTo(buf);
ResultResponse.Result result = ResultResponse.Result.parseFrom(buf.toByteArray());
resultData.put("code", result.getCode());
resultData.put("msg", result.getMsg());
System.out.println("result1 : " + ItvJsonUtil.writeValue(resultData) + "\n\n");
try {
FastClass fastClass = BaseController.getFastClass(pbClass);
ByteString dataValue = result.getData().getValue();
FastMethod parseFrom = fastClass.getMethod("parseFrom", new Class[]{ByteString.class});
GeneratedMessageV3 invoke = (GeneratedMessageV3) parseFrom.invoke(null, new Object[]{dataValue});
//reponse 中 data的值
JSONObject jsonObject = JSONObject.fromObject(new JsonFormat().printToString(invoke));
//System.out.println("jsonObject : " + ItvJsonUtil.writeValue(jsonObject));
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(jsonObject.opt("image")).append(" ").
append(jsonObject.opt("playFrequency")).append(" ").
append(jsonObject.opt("requestFrequency")).
append("\r\n");
resultData.put("data", jsonObject);
System.out.println("\n\n解析结果result2:{" + ItvJsonUtil.writeValue(resultData) + "}\n\n");
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpclient.getConnectionManager().shutdown();
}
}
}
使用方法如下:
@Test
public void tpverify(){
try {
String url = serviceUrl() + "f/tp/verify";
HttpPost httpPost = new HttpPost(url);
GirlTpVerify.Request.Builder builder = GirlTpVerify.Request.newBuilder();
builder.setContent("xxx是");
InputStream inputStream = new ByteArrayInputStream(builder.build().toByteArray());
InputStreamEntity inputStreamEntity = new InputStreamEntity(inputStream, builder.build().toByteArray().length);
PbUtils.potoPost(httpPost, token, inputStreamEntity, GirlTpVerify.Response.class);
} catch (IOException e) {
e.printStackTrace();
}
}