2011-01-25 69 views
1

来自netbeans,我使用内置的向导创建了一个新的REST webservice(使用球衣)。在容器资源类中,它创建了一个存根,运动衫(JSR-311),如何在容器模式下实现@POST

@POST 
@Consumes("application/json") 
@Produces("application/json") 
public Response postJson(Identity identity) { 
    identities.addIdentity(identity); 
    return Response.status(Status.OK).entity(identity).build(); 
} 

如何POST到此?我的理解是需要发布name = val对。球衣在这里期待什么?我将如何使用curl发布json到这个?这是我试过的,

#!/bin/bash 

DATA="{ \"id\": \"$1\", \"vcard\": \"$2\", \"location\": { \"latitude\": \"$3\", \"longitude\": \"$4\" } }" 
echo "posting: $DATA" 
HEADER='Content-Type:application/json' 
URL='http://localhost:8080/contacthi-proximity-service/resources/is' 
curl --data-binary "${DATA}" -H "${HEADER}" "${URL}" 

当我发布这个,看看身份对象进来,所有的字段都是空的?我怀疑我的JSON不正确。当我手动对象添加到我的容器,然后形成一个东西,我看到这样的结果,

{"identities":{"id":"Foo Bar","vcard":"VCARD123","location":{"latitude":"-1.0","longitude":"-1.0"}}} 

当我尝试后同样的事情,所有字段都为空。我也试过,

{"id":"Foo Bar","vcard":"VCARD123","location":{"latitude":"-1.0","longitude":"-1.0"}} 

同样的结果。 ?谢谢。

谢谢。

回答

1

将请求发送到使用curl这个方法,你将不得不使用这样的:

HEADER='--header Content-Type:application/json' 
URL='http://localhost:<port>/methodName' 
curl --data-binary request.json ${HEADER} ${URL} -D response.txt 

您可以将字符串传递给方法。上面的代码会从提到的文件中选择json字符串。样品JSON可能是:

{"userName":"test","timestamp":"2010-08-05T11:35:32.982-0800","userId":"0982"} 

为了创建响应,你可以使用这样的:使用

return Response.status(Status.OK).entity(responseString).build(); 

类是:

import javax.ws.rs.core.Response; 
import javax.ws.rs.core.Response.Status; 
+0

我想这一点,并据此编辑的问题。服务器似乎不喜欢那个请求。它从来没有得到我的代码。 – 2011-01-25 18:14:02

相关问题