从EJB调用RestApi端点资源



我一直在寻找如何从EJB客户端调用在Spring引导中编写的Restful服务(部署在不同的服务器/ip中)的示例代码。

我找不到一个简单的示例或参考来指导我如何实现一个可以调用restful服务(部署在不同的服务器/ip中)的EJB客户端。你能给我指一份文件或例子,说明或描述两者如何相互连接/交谈吗?

我需要通过传递两个头参数进行身份验证来调用端点,如果身份验证成功,则只从Rest检索详细信息并将响应发送回EJB客户端。

我用这样的东西,试试

`public void calExternal() throws ProtocolException,
MalformedURLException,
IOException,
NoSuchAlgorithmException,
InvalidKeyException {
URL myurl = new URL("API END POINT URL");
ObjectMapper mapper = new ObjectMapper();
HttpURLConnection conn = (HttpURLConnection) myurl.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
String payLoad = mapper.writeValueAsString("your payload here");
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("AUTHORIZATION-TYPE", "HMAC");
try {
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(payLoad);
wr.flush();
InputStream in = null;
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
in = conn.getInputStream();
} else {
in = conn.getErrorStream();

}
String encoding = conn.getContentEncoding() == null ? "UTF-8" : conn.getContentEncoding();
String response = IOUtils.toString(in, encoding);
} catch (Exception e) {
e.printStackTrace();
}
}

最新更新