如何将通用列表参数发送到 JAX-RS 服务?
这是方法签名:
@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
是一个由原始类型组成的简单 Java 类:
class SearchOption {
int channelId;
int locationId;
int targetStatus;
}
上面的代码引发了一个异常,声明如下:
No injection source found for a parameter of type [put method signature here]
最佳答案
好的,经过漫长而痛苦的研究,我找到了解决方案。
为了能够在服务方法中接受 POJO,您需要将 @XmlRootElement 属性添加到您的 POJO,在类中编写无参数构造函数并为字段设置 getter/setter。
我一直在使用 Maven,所以这里是 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>
您现在可以在您的 api 方法中发送和接收 POJO 对象。
关于java - 将通用列表参数发送到 JAX-RS Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38583229/