我有休息方法,它应该接受对象列表。 我试过这个:
@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;
}
}
因此,当进行休息调用时: localhost/restservice/getList?name=A&name=B 我收到以下异常:
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
造成的。如果您想传递一堆对象作为查询参数(这似乎是一个坏主意),您必须将它们设置为从查询参数的仅字符串表示形式创建。
关于java - 休息网络服务 : How to accept a list of parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35749792/