[英]ReST Webservices: How to accept a list of parameter
我有rest方法应该接受对象列表。 我试过这个:
@GET
@Path("/getList")
@Produces(MediaType.APPLICATION_JSON)
public response getList(@BeanParam List<MyObjects> myobjectsList) {
//Iterate over the list and return
return Response.ok(outputList).build();
}
并说MyObjects具有一个属性:
public class MyObjects {
@QueryParam
private String name;
public String getName() {
return name;
}
}
因此,在进行REST调用时:localhost / restservice / getList?name = A&name = BI出现以下异常:
Message:A MultiException has 2 exceptions. They are:
1. java.lang.NoSuchMethodException: Could not find a suitable constructor in java.util.List class.
2. java.lang.IllegalArgumentException: Errors were discovered while reifying SystemDescriptor(
implementation=java.util.List
contracts={java.util.List}
scope=org.glassfish.jersey.process.internal.RequestScoped
qualifiers={}
descriptorType=CLASS
descriptorVisibility=NORMAL
metadata=
rank=0
loader=null
proxiable=null
proxyForSameScope=null
analysisName=null
id=349
locatorId=0
identityHashCode=1585683969
reified=false)
MultiException stack 1 of 2
java.lang.NoSuchMethodException: Could not find a suitable constructor in java.util.List class.
at org.glassfish.jersey.internal.inject.JerseyClassAnalyzer.getConstructor(JerseyClassAnalyzer.java:192)
at org.jvnet.hk2.internal.Utilities.getConstructor(Utilities.java:180)
at org.jvnet.hk2.internal.ClazzCreator.initialize(ClazzCreator.java:129)
at org.jvnet.hk2.internal.ClazzCreator.initialize(ClazzCreator.java:182)
at org.jvnet.hk2.internal.SystemDescriptor.internalReify(SystemDescriptor.java:723)
at org.jvnet.hk2.internal.SystemDescriptor.reify(SystemDescriptor.java:678)
at org.jvnet.hk2.internal.ServiceLocatorImpl.reifyDescriptor(ServiceLocatorImpl.java:416)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2029)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:105)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:87)
//more stack trace
MultiException stack 2 of 2
java.lang.IllegalArgumentException: Errors were discovered while reifying SystemDescriptor(
implementation=java.util.List
contracts={java.util.List}
scope=org.glassfish.jersey.process.internal.RequestScoped
qualifiers={}
descriptorType=CLASS
descriptorVisibility=NORMAL
metadata=
rank=0
loader=null
proxiable=null
proxyForSameScope=null
analysisName=null
id=349
locatorId=0
identityHashCode=1585683969
reified=false)
at org.jvnet.hk2.internal.SystemDescriptor.reify(SystemDescriptor.java:689)
at org.jvnet.hk2.internal.ServiceLocatorImpl.reifyDescriptor(ServiceLocatorImpl.java:416)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2029)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:105)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:87)
如何编写接受MyObjects列表的ReST方法?
我在哪里错呢?
您应该能够在方法上以列表形式指定查询参数:
@GET
@Path("/getList")
@Produces(MediaType.APPLICATION_JSON)
public Response getList(@QueryParam("name") List<String> nameList) {
//Iterate over the list and return
return Response.ok(nameList).build();
}
问题可能是由于在bean中使用了QueryParam
,并且在方法上使用了BeanParam
。 如果要传递一堆对象作为查询参数(这似乎是个坏主意),则必须将它们设置为由查询参数所使用的仅String
表示形式创建。
您可以使用由@MatrixParam
批注表示的矩阵参数列表,如下所示:
@GET
@Path("/foo")
@Produces(MediaType.APPLICATION_JSON)
public Response someMethod(@MatrixParam("name") List<String> name,
@MatrixParam("age") List<Integer> age) {
...
}
使用这种方法,您的URL将类似于:
GET /foo;name=John;age=30;name=Jane;age=20
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.