Java 在ESB mule中发送POST请求

Java 在ESB mule中发送POST请求,java,groovy,mule,esb,Java,Groovy,Mule,Esb,我尝试使用ESB mule将post请求发送到API。因此,我创建了这样的流 <?xml version="1.0" encoding="UTF-8"?> <mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core"

我尝试使用ESB mule将post请求发送到API。因此,我创建了这样的流

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8110" doc:name="HTTP Listener Configuration"/>
    <http:request-config name="HTTP_Request_Configuration" protocol="HTTPS" host="api.bonanza.com" port="443" doc:name="HTTP Request Configuration"/>
    <flow name="bonanza_fetchtoken_ceFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/fetchtoken" allowedMethods="GET" doc:name="HTTP"/>
        <message-properties-transformer doc:name="Message Properties">
            <add-message-property key="X-BONANZLE-API-DEV-NAME" value="t*****I"/>
            <add-message-property key="X-BONANZLE-API-CERT-NAME" value="l*****F"/>
        </message-properties-transformer>
        <set-payload value="fetchTokenRequest" doc:name="Set Payload"/>
        <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
        <http:request config-ref="HTTP_Request_Configuration" path="/api_requests/secure_request" method="POST" doc:name="HTTP">
            <http:success-status-code-validator values="0..599"/>
        </http:request>
    </flow>
</mule>
我不是java专家。在上面的示例中,他们是如何用Java发送post有效负载(fetchTokenRequest)的。如何在mule ESB中发送相同的负载


如果是stream post负载,我如何在ESB mule中发送stream post负载?

在您的示例中,http侦听器侦听的任何负载都将是post请求的负载。要使用不同的负载,请在出站http之前使用

内容类型设置为
应用程序/json

设置有效载荷
之后,使用

Hi@Simbu,可以在mule anypoint studio的调色板中找到一个设置有效载荷。使用该选项将有效负载设置为“fetchTokenRequest”。感谢您的回复。我尝试过如下设置有效负载,但它抛出了一个API错误<代码>
我已尝试设置有效负载<代码>它向我抛出一个API错误。在这段时间里,我试图点击邮递员工具的请求。这也将抛出与mule esb相同的错误。在postman中,我将体内的like设置为“raw”,并将内部的有效负载设置为“fetchTokenRequest”。不知道如何像上面的java程序那样发送有效负载。我使用了like
。也编辑了我的原始流程。这是与API相关的错误<代码>{“确认”:“失败”,“版本”:“1.0beta”,“时间戳”:“2016-03-09T09:53:50.000Z”,“错误消息”:{“消息”:“无法确定您发出的请求类型。通常这可能是由于数据在传递到API之前未转义造成的。如果您传递的数据带有引号或其他特殊字符,则应将其转换为JSON,然后转义,然后再通过API发送。”}查看我的更新,外部服务期望
应用程序/json
而不是文本
内容类型
是。我尝试了相同的API错误。在post中更新了我的esb mule流配置。此外,我在原始post中放置了完整的java程序。此java程序为我返回令牌。
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.HttpURLConnection;

public class FetchToken {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            String devId = "t******I";
            String certId = "l*******F";

            URL url = new URL("https://api.bonanza.com/api_requests/secure_request");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestProperty("X-BONANZLE-API-DEV-NAME", devId);
            connection.setRequestProperty("X-BONANZLE-API-CERT-NAME", certId);

            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());

            String requestName = "fetchTokenRequest";

            writer.write(requestName);
            writer.flush();
            writer.close();

            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String response = in.readLine();

            JSONObject jsonResponse = new JSONObject(response);

            if (jsonResponse.optString("ack").equals("Success") 
                    && jsonResponse.optJSONObject("fetchTokenResponse") != null) {
                // Success! Now read more keys from the json object
                JSONObject fetchTokenJson = jsonResponse.optJSONObject("fetchTokenResponse");

                System.out.println("Your token: " + fetchTokenJson.optString("authToken"));
                System.out.println("Token expiration time: " + fetchTokenJson.optString("hardExpirationTime"));
                System.out.println("Authentication URL: " + fetchTokenJson.optString("authenticationURL"));
            }


        } catch (Exception e) {
            System.out.println(e);
        }
    }
}