java - IBM MF8 适配器混搭 - POST 请求

标签 java ibm-mobilefirst apache-httpcomponents

我在 IBM MF8 Java 适配器中尝试了发布请求的示例。

在此适配器内,我尝试调用另一个 Java 适配器 SampleAdapter,并希望使用 userDetails 作为参数执行 POST

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/balanced")
@OAuthSecurity(enabled = false)
public JSONObject generate(UserDetails userDetails , HttpRequest request,  HttpSession session) throws UnsupportedEncodingException {

    String messages = null;

    String getProcedureURL = "/SampleAdapter/resource";
    StringEntity requestEntity = new StringEntity(userDetails.toString(),ContentType.APPLICATION_JSON);

    HttpPost httpPost = new HttpPost(getProcedureURL);
    httpPost.setEntity(requestEntity);
    JSONObject jsonObj = null;

    HttpResponse response;
    try {

        response = adaptersAPI.executeAdapterRequest(httpPost);
        jsonObj = adaptersAPI.getResponseAsJSON(response);
        messages = (String)jsonObj.get("subscriptionMessage");

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JSONObject json = new JSONObject();
    json.put("value", messages);

    return json;
}

SampleAdapter 必须获取对象 userDetails。这样我就可以在后端使用它来进行一些操作。

但是,在这里我无法将数据获取到 SampleAdapter 中。另外,我尝试从 SampleAdapter 返回一些字符串。

我收到以下错误

{"responseText":"","error":"Response cannot be parsed to JSON"}

我知道 IBM MF 在内部进行 json 转换,但是这里怎么可能从一个适配器到另一个适配器执行 POST。 我看到仅为 GET 请求提供的示例。 对于 POST 有什么建议吗?

最佳答案

我根据你的例子给你写了一个简短的例子:

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/balanced")
@OAuthSecurity(enabled = false)
public JSONObject generate() throws UnsupportedEncodingException {

    String messages = null;

    String getProcedureURL = "/SampleAdapter/resource/hello";
    StringEntity requestEntity = new StringEntity("world", ContentType.APPLICATION_JSON);

    HttpPost httpPost = new HttpPost(getProcedureURL);
    httpPost.setEntity(requestEntity);
    JSONObject jsonObj = null;

    HttpResponse response;
    try {

        response = adaptersAPI.executeAdapterRequest(httpPost);
        jsonObj = adaptersAPI.getResponseAsJSON(response);
        messages = "Hello " + (String)jsonObj.get("name");

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JSONObject json = new JSONObject();
    json.put("value", messages);

    return json;
}

这是 POST 端点:

@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("/hello")
@OAuthSecurity(enabled = false)
public Map<String, String> hello(String name) {
    Map<String, String> result = new HashMap<String, String>();
    result.put("name", name);
    return result;
}

希望这对您有帮助。

关于java - IBM MF8 适配器混搭 - POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39331250/

相关文章:

java - 在 java 11 上的 SSL 通信期间发生 SocketException 时,如何使用 DefaultHttpRequestRetryHandler(HTTPClient) 重试?

java - Jackson库的ANDROID使用: How to load object with indexes - range from to

java - 使用 sun/security/x509/X500Name 启动 Android 应用程序失败

java - 如何在 Apache HttpComponents 中将 Content-Length header 添加到 HttpPost?

android - IBM 工作灯 : UI Performance

javascript - MobileFirst - 调用适配器 - ERR_CONNECTION_RESET

java - 多个 HttpAsyncClient 被认为是一种不好的做法吗?

java - Netty客户端多个请求

java - 组织.jooq.impl.ParserException : Token ')' expected: [1:11] select (@i[*]:=@i+1)

java - 在 Android 上保存字符串输入