Ajax发布到RESTful Web服务

时间:2021-03-05 05:29:46

I am having an issue with an Ajax post to a RESTful web service in Java. The project utilizes a single servlet mvc model, with the Ajax post data being sent as JSON to the web service. The specific issue that is occuring is that I a unable to pull the data out of a HttpServletRequest object on the web service side. The POST goes directly to the web service, and I attempted to pull the data out with the following:

我在使用Java的RESTful Web服务的Ajax帖子中遇到了问题。该项目使用单个servlet mvc模型,Ajax发布数据作为JSON发送到Web服务。发生的具体问题是我无法从Web服务端的HttpServletRequest对象中提取数据。 POST直接进入Web服务,我尝试使用以下内容提取数据:

@Path(Ajax)
public AjaxResource(){
@Context
HttpServletRequest request;

@POST
@Produces("application/json")
@Consumes("application/json")
public Response postMethod(){

BufferedReader reader = request.getReader();
// additional code
}
}

I receive an IllegalStateException on the getReader() call on the request; from what I understand the input stream/reader can only be called once. I am unsure if this is due to the doPost method in the servlet doing a request.getParameter call as it seems to ago I'd hitting the servlet before this web service. Is there any other way to retrieve this data other than implementing HttpServletRequestWrapper in the servlet?

我在请求的getReader()调用上收到IllegalStateException;根据我的理解,输入流/阅读器只能被调用一次。我不确定这是否是由于servlet中的doPost方法正在执行request.getParameter调用,因为它似乎在我之前在这个Web服务之前访问了servlet。除了在servlet中实现HttpServletRequestWrapper之外,还有其他方法可以检索此数据吗?

1 个解决方案

#1


0  

You should use @Context HttpServletRequest request as an argument of the resource method. So it should be something like this:

您应该使用@Context HttpServletRequest请求作为资源方法的参数。所以它应该是这样的:

public Response postMethod(@Context HttpServletRequest request){

    // rest of the code

}

#1


0  

You should use @Context HttpServletRequest request as an argument of the resource method. So it should be something like this:

您应该使用@Context HttpServletRequest请求作为资源方法的参数。所以它应该是这样的:

public Response postMethod(@Context HttpServletRequest request){

    // rest of the code

}