【问题标题】:Using Jersey to read form data使用 Jersey 读取表单数据
【发布时间】:2012-02-20 09:04:41
【问题描述】:

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

<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

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

谢谢!

【问题讨论】:

    标签: java google-app-engine jersey


    【解决方案1】:

    试试这个:

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

    Multipart 稍有不同,请参阅 jersey 示例 multipart-webapp 或参阅 http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html。您的网络表单没有生成它,因此 Jersey 正确返回 415 - Unsupported media type,因为您没有任何资源正在处理“application/x-www-form-urlencoded”媒体类型。

    【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多