简体   繁体   English

如何从 REST web 服务中的 curl 获取参数值

[英]how to get parameters' value from curl in REST web service in java

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. 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 命令上传没有元数据的文件:

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这是我的 HttpPut 上传方法

@PUT
@Path("/{directory:.+}")
public Response doPut(@PathParam("directory")String data,byte[]contents)
{
..............
}

This is my curl command to upload file with metadata这是我的 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 

This is my HttpPut method for upload这是我的 HttpPut 上传方法

@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.但是当我添加元数据时,我无法从 doPut 方法中的 curl 命令中获取“--data”值。 How can I solve it?我该如何解决?

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 ).如果您需要在单个请求中同时提交二进制数据(jpg)和一些元数据(json 字符串),我建议您使用提交表单的请求(内容类型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:请求中的每个项目都使用@FormParam注释映射到方法参数,因此您将拥有如下内容:

@PUT
@Path("/{directory:.+}")
@Consumes("application/x-www-form-urlencoded")
public Response doPut(@PathParam("directory") String directory,
                      @FormParam byte[] image,
                      @FormParam String metadata) {
    ...
}

For details on how to continue testing this with curl, see the --data-urlencode argument.有关如何使用 curl 继续测试的详细信息,请参阅--data-urlencode参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM