我在Java中的RESTful web服务的Ajax帖子中遇到了一个问题。该项目使用单个servlet mvc模型,将Ajax post数据作为JSON发送到web服务。出现的具体问题是,我无法从web服务端的HttpServletRequest对象中提取数据。帖子直接转到web服务,我尝试使用以下内容提取数据:
@Path(Ajax)
public AjaxResource(){
@Context
HttpServletRequest request;
@POST
@Produces("application/json")
@Consumes("application/json")
public Response postMethod(){
BufferedReader reader = request.getReader();
// additional code
}
}我在请求的getReader()调用中收到一个IllegalStateException;据我所知,输入流/读取器只能被调用一次。我不确定这是不是因为servlet中的doPost方法执行了request.getParameter调用,就像以前我在此web服务之前访问servlet一样。除了在servlet中实现HttpServletRequestWrapper之外,还有其他方法可以检索这些数据吗?
发布于 2013-05-18 00:49:58
您应该使用@Context HttpServletRequest request作为资源方法的参数。所以它应该是这样的:
public Response postMethod(@Context HttpServletRequest request){
// rest of the code
}https://stackoverflow.com/questions/16601541
复制相似问题