Jersey REST WS错误:“缺少方法的依赖性...在索引X处的参数”

Jersey REST WS错误:“缺少方法的依赖性...在索引X处的参数”

问题描述:

我收到以下错误:

Apr 09, 2013 12:24:26 PM com.sun.jersey.spi.inject.Errors processErrorMessages
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.package.ImportService.specifyLocalFile(java.lang.String,java.lang.String,java.lang.String,java.lang.String) at parameter at index 0
SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.package.ImportService.specifyLocalFile(java.lang.String,java.lang.String,java.lang.String,java.lang.String) at parameter at index 1
SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.package.ImportService.specifyLocalFile(java.lang.String,java.lang.String,java.lang.String,java.lang.String) at parameter at index 2
SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.package.ImportService.specifyLocalFile(java.lang.String,java.lang.String,java.lang.String,java.lang.String) at parameter at index 3
SEVERE: Method, public javax.ws.rs.core.Response com.package.ImportService.specifyLocalFile(java.lang.String,java.lang.String,java.lang.String,java.lang.String), annotated with POST of resource, class com.package.ImportService, is not recognized as valid resource method.

我有一个以前工作的POST方法,它接受Multipart数据(文件上传),然后是其他一些来自提交表单的字符串数据字段,这里是代码:

I have a previously working POST method that takes a Multipart data (a file upload) and then some other String data fields from the submitted form, here's the code:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
    @FormDataParam("file") InputStream uploadedInputStream,
    @FormDataParam("file") FormDataContentDisposition fileDetail,
    @FormDataParam("param1") String param1,
    @FormDataParam("param2") String param2,
    @FormDataParam("param3") String param3) {
    ....
    ....
    return Response.status(200).entity(getEntity()).build();
}

错误似乎与表单参数的解释方式有关新泽西州。这里的代码失败了:

The error seems to be related to the way the form parameters are being interpreted by Jersey. here's the code that fails:

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/local")
public Response specifyLocalFile(
    @FormDataParam("file") String fullFilePath,
    @FormDataParam("param1") String param1,
    @FormDataParam("param2") String param2,
    @FormDataParam("param3") String param3) {
    ....
    ....
    return Response.status(200).entity(getEntity()).build();
}


谷歌搜索后我结束了回顾一些有趣的案例,例如 @FormParam的解组问题失败,或者缺少mulipart JAR依赖问题我问题的最近似的帖子是:缺少方法的依赖,我回答了这个POST的链接,因为我看不到那个特定的解决方案。

After googling a little I end up reviewing some interesting cases, such as Failed unmarshalling issue with @FormParam, or Missing mulipart JAR dependency issue the most aproximate post for my problem was this: "Missing dependecy for method", which I answer with a link to this POST, as I see no currenty solution for that particular one.

这个问题似乎与有关@FormDataParam 注释,当与方法级 @Consumes 注释一起使用时,值 MediaType.APPLICATION_FORM_URLENCODED

The issue appeared to be related to the @FormDataParam annotation, when used with the method-level @Consumes annotation with the value MediaType.APPLICATION_FORM_URLENCODED.

当我更改了Method签名,用 @FormParam 注释每个纯文本字段,异常消失了。检查下面的固定代码:

When I changed the Method signature to annotate each plain-text field with @FormParam, the exception was gone. Check the fixed code below:

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/local")
public Response specifyLocalFile()
    @FormParam("file") String fullFilePath,
    @FormParam("param1") String param1,
    @FormParam("param2") String param2,
    @FormParam("param3") String param3) {
    ....

如果收到的数据类型不必处理MIME编码, @FormParam 注释将尝试处理内容通过序列化;相反, @FormDataParam 注释需要在 @Consumes 注释具有 MediaType.MULTIPART_FORM_DATA 。希望这会有所帮助。

If the type of the data being received does not have to deal with MIME-encodings, the @FormParam annotation will attempt to deal with the contents via serialization; in contrast, the @FormDataParam annotation requires some specific handling that is configured when the @Consumes annotation has the MediaType.MULTIPART_FORM_DATA. Hope this helps.