如何获取参数? java中REST Web服务中curl的值
有谁知道如何使用java从REST Web服务中的curl命令获取参数值。我已经在NetBean IDE中使用jersey框架和java编写了一个REST Web服务。 这是我的curl命令,用于上传没有元数据的文件:
curl -T C:\Users\Folders\a.jpg -H "Content-Type:application/vnd.org.snia.cdmi.dataobject" http://localhost:8080/users/folder/a.jpg
这是我的HttpPut上传方法
@PUT
@Path("/{directory:.+}")
public Response doPut(@PathParam("directory")String data,byte[]contents)
{
..............
}
这是我的curl命令,用于上传带有元数据的文件
curl --data " { "metadata" : "Username":"name"}" -T C:\Users\Folders\a.jpg -H "Content-Type:application/vnd.org.snia.cdmi.dataobject" http://localhost:8080/users/folder/a.jpg
这是我的HttpPut上传方法
@PUT
@Path("/{direcotry:.+}")
public Response doPut(@PathParam("directory")String data,byte[]contents,String meta)
{
..............
}
我的问题是,当我上传没有元数据的文件时,我可以成功上传。但是当我添加元数据时,我无法从 doPut 方法中的curl 命令获取“--data”值。我该如何解决?
Do anyone know how to get parameters' values from curl command in REST web service using java.I have write a REST web service using jersey framework and java in NetBean IDE.
This is my curl command to uplaod file without metadata:
curl -T C:\Users\Folders\a.jpg -H "Content-Type:application/vnd.org.snia.cdmi.dataobject" http://localhost:8080/users/folder/a.jpg
This is my HttpPut method for upload
@PUT
@Path("/{directory:.+}")
public Response doPut(@PathParam("directory")String data,byte[]contents)
{
..............
}
This is my curl command to upload file with metadata
curl --data " { "metadata" : "Username":"name"}" -T C:\Users\Folders\a.jpg -H "Content-Type:application/vnd.org.snia.cdmi.dataobject" http://localhost:8080/users/folder/a.jpg
This is my HttpPut method for upload
@PUT
@Path("/{direcotry:.+}")
public Response doPut(@PathParam("directory")String data,byte[]contents,String meta)
{
..............
}
My question is when i upload file without metadata i can upload successfully. But when i add metadata i can't get the "--data" values from curl command in doPut method. How can I solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要在单个请求中提交二进制数据 blob(jpg)和一些元数据(json 字符串),我建议使用提交表单的请求(内容类型
application/x-www-form-urlencoded
)。这允许您在请求正文中包含这两项。请求中的每个项目都使用 映射到方法参数
@FormParam
注释,因此您将得到如下所示的内容:有关如何继续使用curl 进行测试的详细信息,请参阅
--data-urlencode
参数。If you need to submit both a blob of binary data (the jpg) and some metadata (json string) in a single request, I'd recommend a request that submits a form (content type
application/x-www-form-urlencoded
). This allows you to include both items in the request body.Each item in the request is mapped to a method parameter using the
@FormParam
annotation, so you'd have something like this:For details on how to continue testing this with curl, see the
--data-urlencode
argument.