2012-08-06 54 views
0

在剩余的世界中使用@PUT的最佳做法是什么?我有一个简单的方法将注释和Resteasy最佳实践

@PUT 
@Path("/") 
public boolean putShotComponentNote(String note) { 
    System.out.println(note); 
    return true; 
} 

,当我尝试使用插件访问此方法适用于Chrome一样简单的REST客户端或REST管理控制台,在我指定的URL和数据段,我把键值对

note=This is some note 

在我的方法中,我得到了键和值。理想的情况下我只想得到一个基于变量的名称,比如我可以在一些例子中看到来自http://docs.redhat.com/docs/en-US/JBoss_Enterprise_Web_Platform/5/html-single/RESTEasy_Reference_Guide/index.html

@PUT 
@Path("array") 
@Consumes("application/xml") 
public void putCustomers(Customer[] customers) 
{ 
    Assert.assertEquals("bill", customers[0].getName()); 
    Assert.assertEquals("monica", customers[1].getName()); 
} 

值,以便可以有人点我在正确的方向?

谢谢全部

难道这可能是困难的吗?

这是请求被发送

Request Url:  
http://localhost:8080/rest/ 
Request Method: PUT 
Status Code: 415 
Params: { 
    "note": "asd" 
} 

那么,如何访问笔记PARAM? @PathParam不会工作,也不会@QueryParam和只是普通的字符串不(如上所述)让我note = asd,我想避免这种情况。

所以这可能

+0

这个任何想法? – 2012-08-07 19:10:28

回答

0

这取决于你的参数是如何被派去。由于数据不被@QueryParam检测,您的数据可能是一个形式里面。

给@FormParam一试: http://docs.jboss.org/resteasy/docs/1.1.GA/userguide/html_single/index.html#_FormParam

在发送通过JQuery的Ajax功能的PUT请求我最近遇到了这一点。这里是你如何可以调用REST服务:

$.ajax({ 
    type:"PUT", 
    url:"/rest/", 
    data:{ "var1": "val1", 
      "var2" : "val2"} 
}); 

如果你只是希望得到的参数字符串值,具有的RESTEasy通过java.util.List的对象,让他们一个简单的方法。我用URL编码作为一个例子,但这是可以应用到其他人也:

@PUT 
@Consumes("application/x-www-form-urlencoded") 
public void Method1(@FormParam("var1") List<String> list1, 
        @FormParam("var2") List<String> list2){ 
    String v1 = list1.get(0); 
    String v2 = list1.get(0); 

    System.out.println(v1); 
    System.out.println(v2); 
} 

输出:

val1 
val2