我有休息方法,它應該接受一個對象列表。 我嘗試這樣做:ReST Webservices:如何接受參數列表
@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調用:本地主機/ restservice /的GetList名稱= A &名= 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 Params' AFAIK這隻能被'@ QueryParam'讀取,2-我認爲如果你想使用'@ BeanParam'或'@ FormParam' ...你的方法'getList()'應該用'@ POST'而不是'@ GET'註釋,然後你可以在請求的主體中發送數據。 ** GET **沒有正文,它只通過Query Params發送數據。 – Yazan