泽西 (javax) REST MissingAnnotationException

Jersey (javax) REST MissingAnnotationException

我有一个 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") 也无济于事,因为它最终没有被发现并且 returns NullPointerException

编辑。我喜欢下面答案中提出的猜测。 我确实决定不直接提及某些内容(尽管您可以在代码中看到它)——我使用的是自定义框架 XWiki。完全有可能正文都读完了,然后就没东西看了。

这不是答案:只是看看 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();
    }
}


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