首页 > 解决方案 > 使用 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);
        }   

标签: javaresteasy

解决方案


推荐阅读