使用RestEasyWebTarget从API收到错误请求

时间:2018-10-03 19:53:22

标签: java resteasy

我正在尝试使用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": ""
}
'

{“成功”:true,“消息”:“创建记录器”,“数据”:空}

因此,我试图轻松地模仿它。 这是我的代码

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);
        }   

0 个答案:

没有答案