返回给调用者的响应方法为空,使用Java DSL。

huangapple 未分类评论43阅读模式
英文:

Response returned to caller Method is empty with Java DSL

问题

以下是翻译好的内容:

我正在使用 Spring Boot 项目,我的需求是从外部客户端调用 GET 请求,并将其发送到另一个内部的 Rest 服务。我在这方面使用了 Java DSL,但在处理响应时遇到了问题(一个服务调用另一个服务)。我在这里没有包含变量声明。
以下是代码片段:

// 这是一个类

// 类 A

@Gateway
public HttpRequestHandlerEndpointSpec getTestApi() {
    return Http.inboundGateway("/test")
            .requestChannel(MessageChannels.direct("testRequest").get())
            .replyChannel(MessageChannels.direct("testResponse").get())
            .errorChannel("errorChannel").replyTimeout(REPLY_TIMEOUT);
}

@Gateway
public HttpRequestHandlerEndpointSpec getFoobarApi() {
    return Http.inboundGateway("/foobar")
            .requestChannel(MessageChannels.direct("foobarRequest").get())
            .replyChannel(MessageChannels.direct("foobarResponse").get())
            .errorChannel("errorChannel").replyTimeout(REPLY_TIMEOUT);
}

// 这在另一个类中
// 类 B

@Override
protected IntegrationFlowDefinition<?> buildFlow() {
    return IntegrationFlows.from(httpGateway.getTestApi())
            .enrichHeaders(h -> h.header("Content-Type", "application/json"))
            .gateway(outboundFlow());
}

private IntegrationFlow outboundFlow() {
    return flow -> flow
            .handle(httpGateway.sendToHost("http://localhost:8080/foobar")
                    .httpMethod(HttpMethod.POST)
                    .expectedResponseType(JSONObject.class))
            .handle(jsonHelper, "handleJsonResponse"); // 返回的值是空对象
}

// 这在另一个类中
// 类 C

@Override
protected IntegrationFlowDefinition<?> buildFlow() {
    return IntegrationFlows.from(httpGateway.getFoobarApi())
            .handle(responsMsg); // 此方法获取 Json 响应
}

然而,在处理完 getFoobarApi 后,控制权转移到 handleJsonResponse 方法,但响应是一个空对象。你能告诉我我哪里出错了吗?

英文:

I am using Spring boot project and my requirement is to call get a request from external client and send it another internal Rest Service. I am using Java DSL for the same and stuck when it comes to response handling. (One service calling another service). I have not included variable declaration here.
Below is the code snippet.

 //This is one class
    
    //Class A
    
    @Gateway
    	public HttpRequestHandlerEndpointSpec getTestApi() {
    		return Http.inboundGateway(&quot;/test&quot;)
    				.requestChannel(MessageChannels.direct(&quot;testRequest&quot;).get())
    				.replyChannel(MessageChannels.direct(&quot;testResponse&quot;).get())
    				.errorChannel(&quot;errorChannel&quot;).replyTimeout(REPLY_TIMEOUT);
    	}
    
    @Gateway
    	public HttpRequestHandlerEndpointSpec getFoobarApi() {
    		return Http.inboundGateway(&quot;/foobar&quot;)
    				.requestChannel(MessageChannels.direct(&quot;foobarRequest&quot;).get())
    				.replyChannel(MessageChannels.direct(&quot;foobarResponse&quot;).get())
    				.errorChannel(&quot;errorChannel&quot;).replyTimeout(REPLY_TIMEOUT);
    	}
    
    
    //This is in another class
    //Class B
    
    @Override
    	protected IntegrationFlowDefinition&lt;?&gt; buildFlow() {
    		return IntegrationFlows.from(httpGateway.getTestApi())
    				.enrichHeaders(h -&gt; h.header(&quot;Content-Type&quot;, &quot;application/json&quot;))
    				.gateway(outboundFlow());
    	}
    
    
    
    private IntegrationFlow outboundFlow() {
    		return flow -&gt; flow
    				.handle(httpGateway.sendToHost(&quot;http://localhost:8080/foobar&quot;)
    						.httpMethod(HttpMethod.POST)
    						.expectedResponseType(JSONObject.class))
    				.handle(jsonHelper, &quot;handleJsonResponse&quot;); // The value returned is empty object
    		}
    
    
    //This is in another class
    //Class C
    
    @Override
    	protected IntegrationFlowDefinition&lt;?&gt; buildFlow() {
    		return IntegrationFlows.from(httpGateway.getFoobarApi())
    				.handle(responsMsg); // this method gets the Json Response
    	}

However when the control goes to handleJsonResponse Method after processing the getFoorbarApi, the response is an empty object.Can you please tell me where I am going wrong?

huangapple
  • 本文由 发表于 2020年7月24日 22:01:17
  • 转载请务必保留本文链接:https://java.coder-hub.com/63075172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定