想要使用 jax-rs 读取 get 方法的输入 xml,我知道这很奇怪,但这是我的要求,如果用户发送 REST GET 方法的正文,我想向用户显示 400(错误请求)。因此,如果用户发送正文,要抛出错误请求,我需要首先读取正文。
从编写的代码中摘录:
@GET
@Path("{ids}")
@Consumes({ MediaType.APPLICATION_XML, MediaType.TEXT_XML, "multipart/form-data", "application/x-www-form-urlencoded" })
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML })
public Response read(InputStream inputStream, @PathParam("ids") String Ids, @Context HttpHeaders headers,
@Context HttpServletRequest httpServletRequest, @Context HttpServletResponse httpServletResponse,
@Context UriInfo uriInfo)
{.........//Code here
所以我尝试像这样读取 inputStream:
//Some Code here
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((line = reader.readLine()) != null)
sb.append(line).append("\n");
}
catch (Exception e)
{
//exception handling
}
//Some code here
无论请求是否包含body,读取inputStream后总是返回空白。
所以我的问题是如何检查 GET 请求中是否发送了 xml 正文?
最佳答案
InputStream 实际上读取请求的正文。现在,GET 请求实际上不应该有主体。所以, Jersey 可能只是剥离了 body 。另外,您使用什么客户端发出请求?
如果您需要正文,我建议根据您的情况使用 POST 或 PUT。
关于java - 如何使用 Jersey (REST) 读取 GET 请求的输入 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21175823/