Jersey REST WS Error: "Missing dependency for method... at parameter at index X"
Asked Answered
C

3

9

I am getting the following error:

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.

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();
}
Carson answered 9/4, 2013 at 18:41 Comment(0)
C
13

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.

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.

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) {
    ....

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.

Carson answered 9/4, 2013 at 18:41 Comment(0)
A
4

I had the same error on my project.

1) you need to put all jersey dependencies to the same version.

2) I had also problem because of swagger anotations @ApiParam :

@ApiParam(value = "import file", required = true) @FormDataParam("file") InputStream inputStreamCsv

Removing them did the trick :

@FormDataParam("file") InputStream inputStreamCsv

here is the link mentionning the problem : https://github.com/swagger-api/swagger-core/issues/1530

Finally, everything worked with this :

@POST
@Path("/import")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response import(
        @FormDataParam("file") InputStream inputStreamCsv,
        @FormDataParam("file") FormDataContentDisposition detailsFichier) {...}
Ajar answered 16/6, 2017 at 12:47 Comment(0)
R
1

For reference: In my case it was the depency jersey-multipart, used in a different version than the other Jersey libs, causing this error in connection with multipart form data. --> make sure to use the same version number for all Jersey libs! (mvn dependency:tree is your friend)

Reareace answered 23/8, 2019 at 12:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.