【发布时间】:2016-07-26 16:52:40
【问题描述】:
我有 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;
}
}
所以当进行休息调用时: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 方法?
我哪里错了?
【问题讨论】:
-
好吧,1-您将参数发送为
Query ParamsAFAIK,这只能由@QueryParam阅读,2-我想如果您想使用@BeanParam或@FormParam...你的方法getList()应该用@POST而不是@GET注释,然后你可以在请求的正文中发送数据。 GET 没有正文,它只通过查询参数发送数据。
标签: java web-services rest