带有Jersey的JAX-RS:将表单参数传递给PUT方法以更新资源 [英] JAX-RS with Jersey: Passing form parameters to PUT method for updating a Resource

查看:99
本文介绍了带有Jersey的JAX-RS:将表单参数传递给PUT方法以更新资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须更新具有firstName和lastName的Person记录.用户应该能够从html表单更改它,并且在提交时应该对其进行更新.

I have to update a Person record having firstName and lastName. User should be able to change it from html form and on submit it should be updated.

这是我的代码.

    @PUT
    @Path("/{userId}")
    public Response updatingResource(@FormParam("firstName") String firstName, @FormParam("lastName ") String lastName , @PathParam("userId") String userId){
        System.out.println(firstName);
        System.out.println(lastName);
        return Response.ok().build();
    }

SOP语句输出null.我一直在使用Mozilla Firefox的Poster插件发送PUT请求.

the SOP statements prints null. I have been using Mozilla Firefox's Poster plugin to send PUT request.

我也尝试通过用@Consumes(MediaType.APPLICATION_FORM_URLENCODED)对其进行注释,但是仍然为每个值打印null.

I also tried by annotating it with @Consumes(MediaType.APPLICATION_FORM_URLENCODED) but still it is printing null for each values.

如何编写和调用接收这三个值的PUT方法.我偶然发现很多人要求使用JSON或XML.如何使用JSON?如果有人帮助我编写REST方法来更新资源,我会非常高兴

How to write and call PUT method that receives these three values. I stumble around lot and found people were asking to use JSON or XML. How can I consume JSON? I would be very greatfull if someone help me to write REST method to update a resource

如果我使用Firefox的 RESTClient 和Google的其他客户端我能够获取表单参数.这两个工具的主体部分都位于我放置firstName=Amit&lastName=Patel的位置.我还添加了标头Content-Type作为application/x-www-form-urlencoded.我认为Firefox的海报是越野车.谁能建议我还有其他方法可以验证代码,或者我可以信任前两个REST客户端吗?

If I send PUT request using Firefox's RESTClient and Google's rest-client I am able to get the form parameters. Both this tool has something like body section where I placed firstName=Amit&lastName=Patel. Also I added header Content-Type as application/x-www-form-urlencoded.I think Firefox's Poster is buggy. Can anyone suggest me is there any other way I should validate the code or I can trust on first two REST clients?

推荐答案

除了使用@Consumes(MediaType.APPLICATION_FORM_URLENCODED)注释方法之外,还必须将application/x-www-form-urlencoded作为内容类型发送.你做到了吗?

In addition to annotating your method with @Consumes(MediaType.APPLICATION_FORM_URLENCODED), you must send application/x-www-form-urlencodedas a content-type. Did you do it?

已您只能在POST中使用FormParams:

Edited: You can use FormParams only with POST:

SRV.4.1.1参数可用时以下是 将表单数据填充到之前必须满足的条件 参数集:

SRV.4.1.1 When Parameters Are Available The following are the conditions that must be met before post form data will be populated to the parameter set:

  1. 该请求是HTTP或HTTPS请求.
  2. HTTP方法是POST.
  3. 内容类型为application/x-www-form-urlencoded.
  4. 该Servlet已对请求对象上的任何getParameter方法族进行了初始调用.如果不符合条件 并且帖子表格数据不包含在参数集中,该帖子 数据必须仍然可以通过请求对象的servlet提供给servlet 输入流.如果满足条件,则邮寄表格数据将不再 可直接从请求对象的输入中读取 流.
  1. The request is an HTTP or HTTPS request.
  2. The HTTP method is POST.
  3. The content type is application/x-www-form-urlencoded.
  4. The servlet has made an initial call of any of the getParameter family of methods on the request object. If the conditions are not met and the post form data is not included in the parameter set, the post data must still be available to the servlet via the request object’s input stream. If the conditions are met, post form data will no longer be available for reading directly from the request object’s input stream.

这篇关于带有Jersey的JAX-RS:将表单参数传递给PUT方法以更新资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆