使用Jersey读取表单数据

时间:2012-02-20 09:04:41

标签: java google-app-engine jersey

我正在开发一个网络应用程序,我有一个类似的表单

<form name="form" action="create-user" method="post">
   <input name="accept" type="checkbox"><span>{{acceptLegalTerms}}</span><br>
   <input type="submit" value="{{Continue}}" class="primary fright"/>
</form>

在服务器端,我们正在使用Jersey(在GAE上)。这就是我试图用来读取POST值

的内容
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("create-user")
public Response createUser(@FormDataParam("accept") boolean acceptForm) {
   return Response.ok().entity(acceptForm).build();
}

但它不起作用......它让我回来......

HTTP ERROR 415

Problem accessing /login/create-user. Reason:

Unsupported Media Type

有什么想法吗?我做错了什么?

谢谢!

2 个答案:

答案 0 :(得分:15)

试试这个:

@Path("test")
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String testForm(@FormParam("accept") String accept) {
    return accept;
}

Multipart略有不同,请参阅jersey sample multipart-webapp或查看http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html。您的Web表单没有生成它,因此Jersey正确返回415 - 不支持的媒体类型,因为您没有任何处理“application / x-www-form-urlencoded”媒体类型的资源。

答案 1 :(得分:0)

只是为了保持简单:如果它是唯一的请求处理程序映射到特定的URL(在这种情况下是“test”)并使用特定的HTTP方法(POST),您可以避免使用@Consumes!