我是MSF4J的新手,我需要编写一个REST API来通过POST接受大量的XML数据。我使用request.getMessegeBody()方法来获取数据。我发现它现在已经被弃用了,但我找不到它的新版本,所以我决定无论如何都要使用它。
问题是,当我第一次向微服务发送数据时,它没有获得全部数据。除第一个请求外,所有后续请求都将获得完整的消息体。
当我尝试通过ESB传递请求时,ESB接收整个正文,但当它到达端点时,它将被截断。
我也尝试过从不同的rest客户端发送请求,但第一次总是得到不完整的消息体
@POST
@Consumes({ "application/xml", "application/json", "text/xml" })
@Path("test/")
public Response getReqNotification(@Context Request request) throws Exception {
Response.ResponseBuilder respBuilder =
Response.status(Response.Status.OK).entity(request);
ByteBuf b = request.getMessageBody();
byte[] bb = new byte[b.readableBytes()];
b.duplicate().readBytes(bb);
System.out.println(new String(bb));
return respBuilder.build();
}我希望每次我发送请求时,它都会打印完整的消息(大约2000字节长),但是当我第一次运行微服务时,我只能得到大约800字节。
我希望我能在这里得到帮助。我在其他地方尝试过,但wso2没有太多文档(⌣_⌣“)
发布于 2019-04-30 19:28:53
我仍然不太明白我到底做错了什么,但是在this link的帮助下,我写出了下面的代码,并且运行良好。
主要的cha是我现在使用request.getMessageContentStream()而不是废弃的request.getMessageBody()
@Consumes({ "application/xml", "application/json", "text/xml" })
@Path("test/")
public Response getReqNotification(@Context Request request) throws Exception {
Response.ResponseBuilder respBuilder =
Response.status(Response.Status.OK).entity(request);
String data = "";
BufferedInputStream bis = new BufferedInputStream(request.getMessageContentStream());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
int d;
while ((d = bis.read()) != -1) {
bos.write(d);
}
data = bos.toString();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
}
}
System.out.println(data);
//////do stuff
return respBuilder.build();
}https://stackoverflow.com/questions/55865248
复制相似问题