java - 如何将 json 对象发布到 Jersey 客户端的 rest 端点
问题描述
我使用 Jersey 过滤器来获取请求信息,如 sessionID、TimeStamp ......
现在我想将这些值转换为 JSON 对象并发布到另一个休息端点。我尝试使用以下代码,但它不起作用。
private String SessionID;
private String TimeStamp;
private String Path;
private String UserName;
@Override
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
System.out.println("response:" + responseContext.getEntity());
}
@Override
public void filter(ContainerRequestContext requestContext) throws
IOException {
Date requestDate = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
TimeStamp = simpleDateFormat.format(requestDate);
SessionID = requestInfo.getRequestedSessionId();
UserName = requestInfo.getRemoteUser();
Path = requestContext.getUriInfo().getPath();
Map<String, String> map = new HashMap<>();
map.put("timestamp", TimeStamp);
map.put("sessionId", SessionID);
map.put("username", UserName);
map.put("path", Path);
JSONObject json = new JSONObject();
json.putAll(map);
sendRequest(json);
System.out.println("========================================");
System.out.println(json.toJSONString());
}
private void sendRequest(JSONObject jsonObject) {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/test");
Response res = target.request(MediaType.APPLICATION_JSON).post(Entity.json(jsonObject));
if (res.getStatus() == 200) {
System.out.println("ok");
}
}
我得到了 org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException
/test 只是一个简单的测试服务,它接受 JSON 并返回一个字符串
解决方案
推荐阅读
- python - TypeError:“函数”对象不可下标 - 长度问题
- python - Web Scraping 使用 Beautifulsoup 从内部获取文本
- c# - 使用套接字时的 AllowAutoRedirect 功能
- android - 如何使用 android 模拟器更改 vendor.img 内容?
- azure-active-directory - 使用 API 权限创建 Azure CLI SP
- regex - 如何使用正则表达式获取一个单词组合的第一部分和最后一部分
- python - 如何根据最大值删除元组列表的重复值?
- api - POST 请求在 Postman 中工作,但在 Android Studio 中不工作
- javascript - 如何使用 VSCode 导入 fetch?
- python - 如何用pyarmor和虚拟环境打包python代码?