如何在 Spring 集成中使用 int-http:outbound-gateway 发送 JSONP 请求?

How to send JSONP request using int-http:outbound-gateway in Spring Integration?

我正在尝试将如下调用中的请求转换为 Spring 集成异步调用

function sendToSomething(){
    var formattedDate = $.datepicker.formatDate('yy-mm-dd', new Date());
    var emailAddress = $("#prefferedemail").val();

    // Call to Send email address to something
        $.ajax(
            {
                global: false,
                type : "POST",
                url : "http://xyz.something.com/pub/rf?_ri_=X0Gzc2X"
                    + "& EMAIL_PERMISSION_STATUS_=I&email_address_=" + emailAddress + "&SOURCE_CODE=CPK&EMAIL_PREF_CH_DT=" + formattedDate,
                dataType : "jsonp",
            }   
        );
    }
}

关键是它应该使用 httpjsonp

我正在尝试配置如下内容

<int:gateway id="asyncSomeIntService"
        service-interface="com.staples.eoe.integration.AsyncSomeService" default-request-channel="someRequestChannel" />

<int-http:outbound-gateway url="http://xyz.something.com/pub/rf?_ri_=X0Gzc2X "&amp;EMAIL_PERMISSION_STATUS_=I&amp;email_address_={emailAddress}&amp;SOURCE_CODE=CPK&amp;EMAIL_PREF_CH_DT={formattedDate}"
     http-method="POST" expected-response-type="java.lang.String" request-factory="requestFactory" 
     request-channel="someRequestChannel"   reply-channel="someReplyChannel">
    <int-http:uri-variable name="emailAddress" expression="headers.emailAddress" />
    <int-http:uri-variable name="formattedDate" expression="headers.formattedDate" />
</int-http:outbound-gateway>

<bean id="requestFactory" class="org.springframework.http.client.SimpleClientHttpRequestFactory">
        <property name="connectTimeout" value="5000" />
        <property name="readTimeout" value="5000" />
</bean>

想法是从服务器层调用如下

Message<String> message = MessageBuilder
                    .withPayload("something")
                    .setHeader("formattedDate", "15-01-27")
                    .setHeader("emailAddress", customer.getPreferredEmail())
                    .build();

请求提交 URL 罚款, 但是我得到的部分回复如下 "It is likely that your input did not pass our validation process."

有没有办法使用 Spring 集成发送 JSONP 请求,就像上面的 ajax 调用一样?

好问题!

但是,如果我们转到 jQuery 文档,我们将看到 dataType : "jsonp" 的描述:

If jsonp is specified, $.ajax() will automatically append a query string parameter of (by default) callback=? to the URL. The jsonp and jsonpCallback properties of the settings passed to $.ajax() can be used to specify, respectively, the name of the query string parameter and the name of the JSONP callback function. The server should return valid JavaScript that passes the JSON response into the callback function. $.ajax() will execute the returned JavaScript, calling the JSONP callback function, before passing the JSON object contained in the response to the $.ajax() success handler.

因此,由于 Spring 集成 HTTP 支持不是基于 jQuery 的框架,我们应该使用标准 JSONP 规则并将 callback=? 参数添加到url.

但是...因为我们在 Java 中,所以我认为我们无法将返回的响应作为 Java 脚本执行。虽然,我们是否需要它,因为我们可以使用 Java JSON 能力在下游解析响应主体?