tyt*_*ytk 3 java http jetty jersey
我有一个基本上是服务代理的应用程序.该应用程序本身建在泽西岛上,由Jetty提供服务.我有这个资源方法:
@POST
@Path("/{default: .*}")
@Timed
@Consumes("application/x-www-form-urlencoded")
public MyView post(@Context UriInfo uriInfo, @Context HttpServletRequest request) {
...
}
Run Code Online (Sandbox Code Playgroud)
用户提交POST表单.所有POST请求都通过此方法.除了一个细节之外,UriInfo和HttpServletRequest被适当地注入:似乎没有参数.这是我从终端发送的请求:
POST /some/endpoint HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 15
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Host: localhost:8010
User-Agent: HTTPie/0.9.2
foo=bar&biz=baz
Run Code Online (Sandbox Code Playgroud)
POST主体显然包含2个参数:foo和biz.但是当我尝试在我的代码(request.getParameterMap)中获取它们时,结果是一个大小为0的地图.
如何从资源方法中访问这些参数或此参数字符串?如果重要,那么使用的实现HttpServletRequest是org.eclipse.jetty.server.Request.
三种选择
@FormParam("<param-name>")gt个人参数.防爆.
@POST
@Consumes("application/x-www-form-urlencoded")
public Response post(@FormParam("foo") String foo
@FormParam("bar") String bar) {}
Run Code Online (Sandbox Code Playgroud)使用a MultivaluedMap来获取所有参数
@POST
@Consumes("application/x-www-form-urlencoded")
public Response post(MultivaluedMap<String, String> formParams) {
String foo = formParams.getFirst("foo");
}
Run Code Online (Sandbox Code Playgroud)使用Form让所有的PARAMS.
@POST
@Consumes("application/x-www-form-urlencoded")
public Response post(Form form) {
MultivaluedMap<String, String> formParams = form.asMap();
String foo = formParams.getFirst("foo");
}
Run Code Online (Sandbox Code Playgroud)使用a @BeanParam和个别@FormParams来获取bean中的所有单个参数.
public class FormBean {
@FormParam("foo")
private String foo;
@FormParam("bar")
private String bar;
// getters and setters
}
@POST
@Consumes("application/x-www-form-urlencoded")
public Response post(@BeanParam FormBean form) {
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
2241 次 |
| 最近记录: |