如何在JAX-RS方法中获取POST参数?

use*_*269 26 java parameters rest post jax-rs

我正在使用Jersey开发RESTful服务,它可以很好地处理GET方法.但我只能null使用该POST方法获取参数.这是我项目的示例代码.

HTML

<form action="rest/console/sendemail" method="post">
  <input type="text" id="email" name="email">
  <button type="submit">Send</button>
</form> 
Run Code Online (Sandbox Code Playgroud)

Java的

@POST
@Path("/sendemail")
public Response sendEmail(@QueryParam("email") String email) {
    System.out.println(email);
    return  new Response();
}
Run Code Online (Sandbox Code Playgroud)

我从帖子收到的电子邮件始终为空.有人有想法吗?

我将QueryParam更改为FormParam,我得到的参数仍为null.

小智 36

在通过提交一个表单POST,email不是一个@QueryParam/sendemail?email=me@example.com.

如果您form通过提交HTML POST,email则是@FormParam.

编辑:

这是一个可以处理HTML表单的最小JAX-RS资源.

package rest;

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/console")
public class Console {

    @POST
    @Path("/sendemail")
    @Produces(MediaType.TEXT_PLAIN)
    public Response sendEmail(@FormParam("email") String email) {
        System.out.println(email);
        return Response.ok("email=" + email).build();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • +1但严格来说只适用于表单编码; 如果提交的文档是以XML形式出现的,那么您只想将其反序列化为一个对象(某些类)_without_任何注释该参数. (3认同)