用@FormParam 注释的方法参数在 JAX-RS 中始终为 null
Method parameter annotated with @FormParam is always null in JAX-RS
我正在尝试使用 POST
将多个表单参数发送到我的 REST 服务。但是客户端发送的参数总是接收为null
.
@POST
@Path("/login")
@Produces({ "application/json" })
public LoginData userLogin(@FormParam("picture") String picture,
@FormParam("name") String name,
@FormParam("email") String email) {
...
}
当我像下面的代码一样删除所有参数时,它可以正常工作:
@POST
@Path("/login")
@Produces({ "application/json" })
public LoginData userLogin() {
...
}
我检查过,客户端发送的值不是null
。
是否有其他方式接收参数?
用 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
注释您的方法:
@POST
@Path("/login")
@Produces(MediaType.JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public LoginData userLogin(@FormParam("picture") String picture,
@FormParam("name") String name,
@FormParam("email") String email) {
...
}
并确保请求的 Content-Type
是 application/x-www-form-urlencoded
.
我正在尝试使用 POST
将多个表单参数发送到我的 REST 服务。但是客户端发送的参数总是接收为null
.
@POST
@Path("/login")
@Produces({ "application/json" })
public LoginData userLogin(@FormParam("picture") String picture,
@FormParam("name") String name,
@FormParam("email") String email) {
...
}
当我像下面的代码一样删除所有参数时,它可以正常工作:
@POST
@Path("/login")
@Produces({ "application/json" })
public LoginData userLogin() {
...
}
我检查过,客户端发送的值不是null
。
是否有其他方式接收参数?
用 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
注释您的方法:
@POST
@Path("/login")
@Produces(MediaType.JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public LoginData userLogin(@FormParam("picture") String picture,
@FormParam("name") String name,
@FormParam("email") String email) {
...
}
并确保请求的 Content-Type
是 application/x-www-form-urlencoded
.