【发布时间】:2016-03-28 20:49:31
【问题描述】:
以下代码在调用 getParameter() 时总是返回 null `
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;
import javax.ws.rs.Path;
@Path("/path")
public class MyResource {
protected ResponseBuilder response;
protected HttpServletRequest request;
public MyResource(@Context HttpServletRequest request) {
this.request = request;
this.response = Response.ok();
}
@POST
public Response postTest() {
JSONObject responseObj = new JSONObject();
String test = request.getParameter("test");
log.debug(test);
return response.entity(responseObj.toString()).build();
}
}
我正在使用这个 curl 来触发它:
curl -X POST -H "Cache-Control: no-cache" -H "Postman-Token: b7a3801f-b9d7-c7a1-004e-ac70b835d436" -H "Content-Type: application/x-www-form-urlencoded" -d 'test=12345' 'http://localhost:8180/path'
我做错了什么?
【问题讨论】:
-
request来自哪里?它是该方法来自的任何类中的实例变量吗? -
您在哪里/如何将
/path映射到postTest()方法? -
我正在使用
@Path("/path")(更新代码) -
只需使用 REST 样式的路径参数,而不是摆弄低级 Servlet API。