繁体   English   中英

Ajax发布到RESTful Web服务

[英]Ajax post to RESTful web service

我在Ajax发布到Java中的RESTful Web服务时遇到问题。 该项目使用单个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
}
}

我在请求的getReader()调用上收到IllegalStateException; 据我了解,输入流/阅读器只能调用一次。 我不确定这是否是由于servlet中的doPost方法执行request.getParameter调用所致,就像我之前在此Web服务之前打过servlet一样。 除了在Servlet中实现HttpServletRequestWrapper之外,还有其他方法可以检索此数据吗?

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

public Response postMethod(@Context HttpServletRequest request){

    // rest of the code

}

暂无
暂无

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

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