未在POST方法REST Java中传递参数
因此,我正在开发一个web服务,当我在表单提交上调用api/更新操作时,jsp中的两个输入“data”和“id”应该传递,我希望在POST方法块中对其执行操作 以下是调用该方法的表单:未在POST方法REST Java中传递参数,java,rest,http,content-type,Java,Rest,Http,Content Type,因此,我正在开发一个web服务,当我在表单提交上调用api/更新操作时,jsp中的两个输入“data”和“id”应该传递,我希望在POST方法块中对其执行操作 以下是调用该方法的表单: <form id="myForm" action="api/update" method="post"> id: <input type="text" id="id" name="id"> <br/> <textarea id="data" name="data" row
<form id="myForm" action="api/update" method="post">
id: <input type="text" id="id" name="id">
<br/>
<textarea id="data" name="data" rows="30" cols="100" placeholder="Empty File"></textarea>
<br/>
<button id="submit" type="submit">Submit</button>
</form>
身份证件:
提交
以下是剩余代码:
@Path("update")
public class UpdateResource {
@Context
private UriInfo context;
public UpdateResource() {
}
@Context
private HttpServletRequest request;
@POST
public void putHtml() {
String data = request.getParameter("data");
String id = request.getParameter("id");
System.out.println(id); //<---- line1
System.out.println(data); //<---- line2
//further operations
}
}
@Path(“更新”)
公共类更新源{
@上下文
私有信息上下文;
公共UpdateResource(){
}
@上下文
私有HttpServletRequest;
@职位
public-void-putHtml(){
字符串数据=request.getParameter(“数据”);
String id=request.getParameter(“id”);
System.out.println(id);//使用
原因是实体主体已被读取,导致HttpServletRequest
中的参数为空,html表单的结束标记在哪里?在chrome中运行测试,并在“网络”选项卡上打开开发人员工具,确认数据实际上正在发送到服务器。结束标记没有被删除问题中显示ng。已更正。正在发送数据。我已从“网络”选项卡验证。@KyleM数据正在发送。附加图像使用@FormParam
,而不是尝试从HttpServletRequest获取参数。即putHtml(@FormParam(“id”)字符串id)
@peeskillet它起作用了。你能把它作为一个答案发布,这样我就可以接受了。你的答案看起来是正确的,但是你有没有关于“它已经被读取,导致参数为空”的来源?通常,只要属性没有被覆盖或删除,就可以从请求中读取任意次数的属性。@KyleM看了一下。我在其他一些JAX-RS/Jersey相关文档中也读到了一些内容,但我似乎记不起在哪里看到过它
@POST
public void putHtml(@FormParam("id") String id,
@FormParam("data") String data) {
}