简体   繁体   English

Ajax发布到RESTful Web服务

[英]Ajax post to RESTful web service

I am having an issue with an Ajax post to a RESTful web service in Java. 我在Ajax发布到Java中的RESTful Web服务时遇到问题。 The project utilizes a single servlet mvc model, with the Ajax post data being sent as JSON to the web service. 该项目使用单个servlet mvc模型,并将Ajax发布数据作为JSON发送到Web服务。 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. 发生的特定问题是我无法从Web服务端的HttpServletRequest对象中提取数据。 The POST goes directly to the web service, and I attempted to pull the data out with the following: 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; 我在请求的getReader()调用上收到IllegalStateException; 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. 我不确定这是否是由于servlet中的doPost方法执行request.getParameter调用所致,就像我之前在此Web服务之前打过servlet一样。 Is there any other way to retrieve this data other than implementing HttpServletRequestWrapper in the servlet? 除了在Servlet中实现HttpServletRequestWrapper之外,还有其他方法可以检索此数据吗?

You should use @Context HttpServletRequest request as an argument of the resource method. 您应该使用@Context HttpServletRequest request作为资源方法的参数。 So it should be something like this: 所以应该是这样的:

public Response postMethod(@Context HttpServletRequest request){

    // rest of the code

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM