使用Rest Webservice下载文件

时间:2015-11-12 21:09:08

标签: java web-services rest post

我已经使用rest Web服务(GET)Method.its下载了一个完美的文件。 但我想知道,有没有办法使用POST METHOD实现下载功能。所以我可以传递一些输入值。 对于GET METHOD,我在URL中传递查询参数。我希望传递JSON格式的值。所以请你建议我,我怎样才能实现这个目标?

FROM alpine
RUN adduser -D myuser
USER myuser

1 个答案:

答案 0 :(得分:0)

我不确定,如何以JSON格式传递值。下面是使用HTML表单输入的示例:

@POST
@Consumes("application/x-www-form-urlencoded")
@Produces("application/pdf")
@Path("/downloaddocument")
public Response downloadDocument(@FormParam("filename") String filename) {
    // Return your PDF document as a Responce here
}

更新:要将输入作为JSON传递,您需要使用

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces("application/pdf")
@Path("/downloaddocument")
public Response downloadDocument(MyObject jsonObject) {
   String name = jsonObject.getName();
   // Return you PDF document as a response here
}

其中jsonObject是一个对象,它为JSON输入建模

public class MyObject  {
    String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

假设您的JSON文件具有以下结构

{
    "name" : "myfile.pdf"
}