我在适配器中尝试了post请求的示例。
在这个适配器中,我正在尝试调用另一个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在内部执行json转换,但是在这里如何实现从一个适配器到适配器的POST。我看到了仅为GET请求提供的示例。对帖子有什么建议吗?
发布于 2016-09-05 22:20:22
我根据你的例子写了一个简短的例子:
@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;
}我希望这能对你有所帮助。
https://stackoverflow.com/questions/39331250
复制相似问题