Reputation: 5914
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
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