java - 使用 RestEasyWebTarget 从 API 获取错误请求
问题描述
我正在尝试使用 RestEasyWebTarget 连接到 api,当我使用 get 到某些端点时没有问题,但是现在我想连接到 POST 和 PUT 端点并且我收到了一些错误的请求。我想我可能做错了。
我用 curl 试过了,它有效
curl -u backendserver:password -X POST --header 'Accept:application/json; charset=utf-8' --header 'Content-Type:application/json; charset=utf-8' http://localhost:8087/v2/servers/_defaultServer_/vhosts/_defaultVHost_/applications/ramona/instances/_definst_/streamrecorders/jdc -d '
{
"instanceName": "",
"fileVersionDelegateName": "",
"serverName": "",
"recorderName": "jdc",
"currentSize": 0,
"segmentSchedule": "",
"startOnKeyFrame": true,
"outputPath": "",
"currentFile": "",
"saveFieldList": [
""
],
"recordData": false,
"applicationName": "",
"moveFirstVideoFrameToZero": false,
"recorderErrorString": "",
"segmentSize": 0,
"defaultRecorder": false,
"splitOnTcDiscontinuity": false,
"version": "",
"baseFile": "",
"segmentDuration": 0,
"recordingStartTime": "",
"fileTemplate": "",
"backBufferTime": 0,
"segmentationType": "",
"currentDuration": 0,
"fileFormat": "",
"recorderState": "",
"option": ""
}
'
{"success":true,"message":"Recorder Created","data":null}
所以我试着用resteasy来模仿这个。这是我的代码
public void grabar(final String streamName) throws InterruptedException {
System.out.println("Empezar a grabar");
clientREST = new ResteasyClientBuilder().build();
clientREST.register(new BasicAuthentication(userWowza, passWowza));
String targetStr = "http://" + host + ":" + portEndpoint + endPoint + app + "/instances/_definst_/streamrecorders/"+streamName;
//http://localhost:8087/v2/servers/_defaultServer_/vhosts/_defaultVHost_/applications/live/instances/_definst_/streamrecorders/myStream
System.out.println("target: " + targetStr);
ResteasyWebTarget target = clientREST.target(targetStr);
target.register(new BasicAuthentication(userWowza, passWowza));
StreamRecorderConfigDTO recorder = new StreamRecorderConfigDTO();
recorder.setRecorderName(streamName);
recorder.setCurrentSize(0);
recorder.setStartOnKeyFrame(true);
recorder.setRecordData(false);
recorder.setSegmentSize(0);
recorder.setDefaultRecorder(false);
recorder.setMoveFirstVideoFrameToZero(false);
recorder.setSplitOnTcDiscontinuity(false);
recorder.setSegmentDuration(0);
recorder.setBackBufferTime(0);
recorder.setCurrentDuration(0);
recorder.setFileFormat("MP4");
recorder.setApplicationName("ramona");
recorder.setBaseFile(streamName+".mp4");
Response response = target.request().
post(Entity.entity(recorder, "application/json; charset=utf-8"));
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
response.close();
}
但每次我收到 400 个错误请求。所以我想一切都表明我没有正确传递我的 StreamRecorderConfigDTO 对象。我也应该发布我的 StreamRecorderConfigDTO 课程吗?
*****更新
我更改了目标以添加标题
Response response = target.request().header("Content-Type", "application/json; charset=utf-8").accept(MediaType.APPLICATION_JSON).post(Entity.entity(recorder, MediaType.APPLICATION_JSON));
int status = response.getStatus();
response.close();
if (status != 200) {
throw new RuntimeException("Failed : HTTP error code : " + status);
}
解决方案
推荐阅读
- excel - Excel VBA:如何将粗体格式应用于“:”之前的所有单词?
- python - SpaCy lemmatizer 删除大写
- python - 如何构建指向已保存的 python 回溯的异常?
- python - 用glom怎么说“收集所有”?
- jquery - 如何将自定义参数传递给 jQuery AJAX 方法的回调方法?
- javascript - 尝试访问 hypixel api 中的“pricePerUnit”时,出现错误:TypeError: Cannot read property 'pricePerUnit' of undefined
- javascript - 如何让 Puppeteer 基于 page.$eval 函数在 PDF 中渲染 MathJax?
- javascript - 数据库中的数据未返回
- python - 如何从另一个python文件运行函数
- java - 卸载应用程序时,内部存储“getFilesDir()”中的文件不会被删除