Élodie Petit
Élodie Petit

Reputation: 5914

Sending a generic list parameter to a JAX-RS web service

How can I send a generic list parameter to a JAX-RS service?

Here is the method signature:

@POST
@Path("findcustomers")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void FindCustomers(@PathParam("branchId") long branchId, 
                          @PathParam("searchOptions") List<SearchOption> searchOptions) 
                          throws Exception {
    ...
}

SearchOption is a simple Java class composed of primitive types:

class SearchOption {
    int channelId;
    int locationId;
    int targetStatus;
}

The code above raises an exception stating something like:

No injection source found for a parameter of type [put method signature here]

Upvotes: 2

Views: 1220

Answers (1)

&#201;lodie Petit
&#201;lodie Petit

Reputation: 5914

Ok, after a long and painful research, I've found the solution.

In order to be able to accept a POJO in a service method, you need to add @XmlRootElement attribute to your POJO, write a no-argument constructor in the class and have getters/setters for the fields.

I've been using Maven so here's the minimum required dependencies in pom.xml:

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.22.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.23.1</version>
</dependency>

You can now send and receive POJO objects in your api methods.

Upvotes: 1

Related Questions