java - 在基于 JAX-RS 的 REST 应用程序中发送 POST 请求时 HTTP 500 请求失败
问题描述
我正在尝试实现一个基于 JAX-RS 的小型 REST 应用程序。它有一个后端服务器和一个小客户端。客户端基本上从表中读取数据并将表数据作为 POST 值发送到 REST 服务器。这是处理 POST 请求的资源方法。
@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public Response migrateToMinio(Attachment attachment, @Context UriInfo uriInfo) throws Exception {
Integer id = attachmentService.createNew(attachment);
UriBuilder builder = uriInfo.getAbsolutePathBuilder();
builder.path(Integer.toString(id));
return Response.created(builder.build()).build();
}
这是客户端代码:
public static void main(String[] args) throws Exception {
// Fire first (full?) update trigger here
fireInitialMigration();
// For subsequent (incremental?) updates, schedule an HTTP POST to occur at a fixed rate:
EXECUTOR.scheduleAtFixedRate(() -> fireSubsequentUpdate(), INITIAL_DELAY, UPDATE_RATE, TimeUnit.MILLISECONDS);
// Keep main thread alive
while (true) ;
}
private static void fireInitialMigration() throws Exception {
TiedostoService tiedostoService = new TiedostoService();
List<Tiedosto> tiedostoList = tiedostoService.getAllFiles();
Client client = ClientBuilder.newClient();
List<Response> responseList = new ArrayList<>();
for (Tiedosto tiedosto : tiedostoList){
Attachment attachment = new Attachment();
attachment.setCustomerId(tiedosto.getCustomerId());
attachment.setSize(tiedosto.getFileSize());
System.out.println(attachment.getCustomerId()+" "+attachment.getSize());
Response res = client.target("http://localhost:8080/myapp/attachments")
.request("application/json")
.post(Entity.entity(attachment, MediaType.APPLICATION_JSON), Response.class);
responseList.add(res);
}
System.out.println(responseList);
}
private static void fireSubsequentUpdate() {
// Similar to initialMigration(), but change Entity/set of HTTP POSTs according to your needs.
}
fireInitialMigration() 应该读取表并将值存储在 List<> 中。然后在 for 循环中迭代列表,提取值并将其分配给 Attachment 类的实例,最后作为 POST 发送。现在,当我运行客户端时,它一直工作到System.out.println(attachment.getCustomerId()+" "+attachment.getSize());
之后,客户端给出这样的 HTTP 500 请求失败错误
InboundJaxrsResponse{context=ClientResponse{method=POST, uri=http://localhost:8080/myapp/attachments, status=500, reason=Request failed.}}, InboundJaxrsResponse{context=ClientResponse{method=POST, uri=http://localhost:8080/myapp/attachments, status=500, reason=Request failed.}}, InboundJaxrsResponse{context=ClientResponse{method=POST, uri=http://localhost:8080/myapp/attachments, status=500, reason=Request failed.}}
当我尝试通过这样的后端的主要方法手动输入数据时,它工作正常。
public static void main(String[] args) {
AttachmentService attachmentService = new AttachmentService();
Integer id = null;
Attachment attachment = new Attachment();
attachment.setCustomerId(150);
attachment.setSize(8096);
try{
id = attachmentService.createNew(attachment);
} catch (Exception e) {
e.printStackTrace();
}
}
我已经坚持了一段时间了,我非常感谢任何帮助/指导。
解决方案
推荐阅读
- vue.js - 如何将 vuejs 代码嵌入其他网站
- ios - 在框架“absl”中找不到标头“base/config.h”
- python - 返回整个列表的第一个项目的位置,而不是每个项目的位置?
- javascript - 如何从带有观察者属性的javascript元素上添加类?
- c# - 如何在 Windows 10 中移动 Windows 资源管理器屏幕位置?
- python - 如何以pythonic方式将两个未排序的列表转换为字典?
- java - 如何为 JSON 和纯文本正确配置杰克逊转换器
- javascript - 在redux中推入对象内的数组?
- r - R:为热图使用数据框
- google-apps-script - 通过表格打开 Google 日历并允许用户使用表格中的信息创建条目