首页 > 解决方案 > DataFlowTemplate 响应不返回正文

问题描述

考虑到 test07 流已经创建,下面的代码片段不会在异常堆栈跟踪中获得响应体。

    try {
        URI dataFlowUri = URI.create("http://localhost:9393");
        DataFlowOperations dataFlowOperations = new DataFlowTemplate(dataFlowUri);
        StreamDefinition streamDefinition = Stream.builder(dataFlowOperations)
                .name("test07")
                .definition("time|log")
                .create();

    }
    catch (Exception ex){
        ex.printStackTrace();
    }

org.springframework.web.client.HttpClientErrorException$Conflict: 409 : [no body] at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:125) at org.springframework.web.client.DefaultResponseErrorHandler.handleError( DefaultResponseErrorHandler.java:184) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:125) at org.springframework.cloud.dataflow.rest.client.VndErrorResponseErrorHandler.handleError(VndErrorResponseErrorHandler.java:62) at org org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:782) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate .java:740) 在 org.springframework.web.client.RestTemplate。在 org.springframework.cloud.dataflow.rest.client.StreamTemplate.createStream(StreamTemplate.java:121) 的 org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:418) 执行(RestTemplate.java:674)在 org.springframework.cloud.dataflow.rest.client.dsl.StreamDefinition.(StreamDefinition.java:60) 在 org.springframework.cloud.dataflow.rest.client.dsl.Stream$StreamDefinitionBuilder.create(Stream.java:400 )

另一方面,当 Post 直接请求时

http://localhost:9393/streams/definitions?name=test07&definition=time%20%7C%20log&description=test07

响应如下,状态码为 409

[ { "logref": "DuplicateStreamDefinitionException", "message": "无法创建流 test07,因为已经创建了另一个同名流" } ]

我想在发生异常时获得响应正文,
这样如果我在这里遗漏了什么,任何人都可以提供帮助?

标签: spring-cloudspring-cloud-streamspring-cloud-dataflow

解决方案


我必须覆盖默认的休息模板才能根据需要获取响应正文。

        ClientHttpRequestFactory factory = new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory());
        RestTemplate restTemplate = new RestTemplate(factory);

        URI dataFlowUri = URI.create("http://localhost:9393");
        DataFlowOperations dataFlowOperations = new DataFlowTemplate(dataFlowUri, restTemplate);
        StreamDefinition streamDefinition = Stream.builder(dataFlowOperations)
                .name("test07")
                .definition("time|log")
                .create();

谢谢


推荐阅读