[英]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.