使用Jax-R上的注释Post发送而不发送文件

时间:2015-08-20 17:32:22

标签: java jax-rs

我正在开发一个Jax-Rs服务,我必须使用和不使用文件创建一些东西。

 @POST
    @Path("makesomething")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.TEXT_PLAIN)
       public String makesomething(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("variable") String variable, @Context HttpServletRequest request) throws IOException {
        //make something
        }

如果我没有发送文件,则会收到错误消息,说发送文件是必要的。如何在不发送文件的情况下进行此操作?

1 个答案:

答案 0 :(得分:1)

André是对的,这样的事情应该可以正常工作(假设当你没有上传文件时,你只是张贴表格数据):

@POST
@Path("makesomething")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public String makesomething(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("variable") String variable, @Context HttpServletRequest request) throws IOException {
    makeSomethingImpl(uploadedInputStream, fileDetail, variable, request);
}

@POST
@Path("makesomething")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
public String makesomething(@FormDataParam("variable") String variable, @Context HttpServletRequest request) throws IOException {
    makeSomethingImpl(null, null, variable, request);
}

private String makeSomethingImpl(final InputStream uploadedInputStream, final FormDataContentDisposition fileDetail, final String variable, final HttpServletRequest request) {
    // make something
}