使用Jersey读取表单数据

Jav*_*ano 6 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>
Run Code Online (Sandbox Code Playgroud)

在服务器端,我们正在使用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();
}
Run Code Online (Sandbox Code Playgroud)

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

HTTP ERROR 415

Problem accessing /login/create-user. Reason:

Unsupported Media Type
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?我究竟做错了什么?

谢谢!

Pav*_*cek 16

试试这个:

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

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

  • 你还需要[jersey-multipart](http://search.maven.org/#search|ga|1|a%3A%22jersey-multipart%22). (2认同)