Jersey(javax)REST MissingAnnotationException

时间:2015-02-14 03:22:52

标签: java rest java-ee jersey

我有一个包含多个字段和文件上传的HTML表单。

在Java方面,我收到的表格如下,并且有效:

@Component("org.phenotips.metabolites.FileUploaderResource")
@Path("/metabolites")

public class FileUploaderResource extends XWikiResource

{
  @GET
  @Path("/test")
  public Response test() {
      String response = "<html>Successful</html>";
      return Response.ok(response).build();
  }

  @POST
  @Path("/upload")
  @Consumes(MediaType.MULTIPART_FORM_DATA)
  public Response uploadFile(
      @FormDataParam("filepath") InputStream uploadedInputStream
  )
  {
      try {
          String errorMessage = "This is an error response";
          URI redirect = new URI("http://localhost:8080/");
          return Response.temporaryRedirect(redirect).header("error_msg", errorMessage).build();
      } catch (Exception ex) {
          return Response.serverError().build();
      }
  }
}

但这不是我需要的,例如uploadedInputStream包含的内容(示例)

-----------------------------184640265716083967621914753489
Content-Disposition: form-data; name="id"

0000002
-----------------------------184640265716083967621914753489
Content-Disposition: form-data; name="column_order"

yes,  no, 
-----------------------------184640265716083967621914753489
Content-Disposition: form-data; name="date"

05/02/2015
-----------------------------184640265716083967621914753489
Content-Disposition: form-data; name="filepath"; filename="phenotips_2014-08-28_10-35.csv"
Content-Type: text/csv

"History (code)","History","History (code and name)","observed"
"","","","HP:0001622"
-----------------------------184640265716083967621914753489--

正如您所看到的,表单中的文件数量多于文件。 但是,如果我将uploadFile的签名更改为

public Response uploadFile(
    @FormDataParam("date") String date,
    @FormDataParam("filepath") InputStream uploadedInputStream
)

我收到以下错误:

The root resource class org.phenotips.metabolites.FileUploaderResource is not a valid root resource class: The entity is already read. The 1. parameter requires one of the following annotations: [interface javax.ws.rs.core.Context, interface javax.ws.rs.HeaderParam, interface javax.ws.rs.MatrixParam, interface javax.ws.rs.QueryParam, interface javax.ws.rs.PathParam, interface javax.ws.rs.CookieParam]

更改为@FormParam("date")也无济于事,因为它最终未找到并返回NullPointerException

EDIT。我喜欢下面答案中提出的猜测。 我确实决定不直接提到一些东西(你可以在代码中看到它) - 我正在使用自定义框架,XWiki。完全有可能阅读身体,然后没有什么可读的。

1 个答案:

答案 0 :(得分:2)

这不是一个答案:只是看看OP可能没有向我们展示或告诉我们

我用Postman对此进行了测试,我没有问题。也许这是你没有向我们展示的其他东西。

@Path("/multipart")
public class MutlipartResource {
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response postMultiPart(@FormDataParam("date") String date, 
                                  @FormDataParam("filepath") InputStream stream) 
                                                               throws Exception {
        ImageIO.read(stream);
        return Response.ok(date).build();
    }
}

enter image description here


以下是我对错误的解释,虽然我可能错了,但这只是猜测。在达到该方法之前,似乎有些东西正在读取身体部位,因此没有什么可以阅读的。在这种情况下,错误可能会说,因为它不是一个可读部分,所以不应该将其定义为多部分而是另外一些形式,因为您只能有一个实体主体,date不能是body,但必须是查询参数,路径参数等(不是实体的东西)。这只是一个猜测。但也许你可以调查一下。