首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IBM适配器Mashup - POST请求

IBM适配器Mashup - POST请求
EN

Stack Overflow用户
提问于 2016-09-05 21:05:20
回答 1查看 89关注 0票数 0

我在适配器中尝试了post请求的示例。

在这个适配器中,我正在尝试调用另一个Java适配器SampleAdapter,并希望使用userDetails作为参数进行POST

代码语言:javascript
复制
@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返回一些字符串。

我得到以下错误

代码语言:javascript
复制
{"responseText":"","error":"Response cannot be parsed to JSON"}

我知道IBM在内部执行json转换,但是在这里如何实现从一个适配器到适配器的POST。我看到了仅为GET请求提供的示例。对帖子有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2016-09-05 22:20:22

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

代码语言:javascript
复制
@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端点:

代码语言:javascript
复制
@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;
}

我希望这能对你有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39331250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档