Arrays 使用get将列表或数组传递给RESTeasy

Arrays 使用get将列表或数组传递给RESTeasy,arrays,rest,get,resteasy,Arrays,Rest,Get,Resteasy,我在各种示例中看到过这种情况,这些示例演示了如何创建一个REST服务,该服务将数组或对象列表作为URL的一部分 我的问题是,如何使用RESTeasy实现这一点? 下面是我如何假设这是可行的 @GET @Path("/stuff/") @Produces("application/json") public StuffResponse getStuffByThings( @QueryParam("things") List<Thing>

我在各种示例中看到过这种情况,这些示例演示了如何创建一个REST服务,该服务将数组或对象列表作为URL的一部分

我的问题是,如何使用RESTeasy实现这一点? 下面是我如何假设这是可行的

   @GET
   @Path("/stuff/")
   @Produces("application/json")
    public StuffResponse getStuffByThings(
            @QueryParam("things") List<Thing> things);
@GET
@路径(“/stuff/”)
@生成(“应用程序/json”)
public StuffResponse getStuffByThings(
@QueryParam(“事物”)列出事物;
创建并使用包装器对象。下面是一个简单的例子:

public class QueryParamAsListTest {
public static class Thing {
    String value;
     Thing(String value){ this.value = value; }
}

public static class ManyThings {
    List<Thing> things = new ArrayList<Thing>();

    ManyThings(String values){
        for(String value : values.split(",")){
            things.add(new Thing(value));
        }
    }
}

static class Converter implements StringConverter<ManyThings> {

    public ManyThings fromString(String str) {
        return new ManyThings(str);
    }

    public String toString(ManyThings value) {
        //TODO: implement
        return value.toString();
    }

}

@Path("/")
public static class Service {
    @GET
    @Path("/stuff/")
    public int getStuffByThings(
        @QueryParam("things") ManyThings things){

        return things.things.size();
    }
}



@Test
public void test() throws Exception {
    Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
    dispatcher.getProviderFactory().addStringConverter(new Converter());
    dispatcher.getRegistry().addSingletonResource(new Service());

    MockHttpRequest request = MockHttpRequest.get("/stuff?things=a,b,c");
    MockHttpResponse response = new MockHttpResponse();

    dispatcher.invoke(request, response);

    Assert.assertEquals("3", response.getContentAsString());


}
}
公共类QueryParamAsListTest{
公共静态类事物{
字符串值;
事物(字符串值){this.value=value;}
}
公共静态类ManyThings{
List things=new ArrayList();
许多内容(字符串值){
for(字符串值:values.split(“,”)){
增加(新事物(价值));
}
}
}
静态类转换器实现StringConverter{
公共ManyThings fromString(String str){
归还新的物品(str);
}
公共字符串到字符串(ManyThings值){
//TODO:实现
返回值.toString();
}
}
@路径(“/”)
公共静态类服务{
@得到
@路径(“/stuff/”)
公共int getStuffByThings(
@QueryParam(“事物”)许多事物(事物){
返回things.things.size();
}
}
@试验
public void test()引发异常{
Dispatcher=MockDispatcherFactory.createDispatcher();
dispatcher.getProviderFactory().addStringConverter(新转换器());
dispatcher.getRegistry().addSingletonResource(新服务());
MockHttpRequest request=MockHttpRequest.get(“/stuff?things=a,b,c”);
MockHttpResponse response=新的MockHttpResponse();
调用(请求、响应);
Assert.assertEquals(“3”,response.getContentAsString());
}
}

我想你也可以使用我很幸运,使用了
集合
而不是
列表
。我无法为
List
制作
StringConverter

@Provider
public class CollectionConverter implements StringConverter<Collection<String>> {

  public Collection<String> fromString(String string) {
    if (string == null) {
      return Collections.emptyList();
    }
    return Arrays.asList(string.split(","));
  }

  public String toString(Collection<String> values) {
    final StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (String value : values) {
      if (first) {
        first = false;
      } else {
        sb.append(",");
      }
      sb.append(value);
    }
    return sb.toString();
  }
}
@Provider
公共类CollectionConverter实现StringConverter{
公共集合fromString(字符串){
if(字符串==null){
返回集合。emptyList();
}
返回Arrays.asList(string.split(“,”);
}
公共字符串到字符串(集合值){
最终StringBuilder sb=新StringBuilder();
布尔值优先=真;
for(字符串值:值){
如果(第一){
第一个=假;
}否则{
某人加上(“,”);
}
附加(价值);
}
使某人返回字符串();
}
}

我做了头上的
toString
。一定要为它编写单元测试以进行验证。当然,当你使用番石榴时,一切都会变得更简单、更清晰。可以使用
Joiner
Splitter
。非常方便。

只需单独使用包装器,无需其他任何东西

在您的端点中

@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Path("/find")
@GET
MyResponse find(@QueryParam("ids") Wrapper ids); 
你看起来像这样:

public class Wrapper implements Serializable {
    private List<BigInteger> ids = Collections.emptyList();

    public String toString() {
        return Joiner.on(",")
                     .join(ids);
    }

    public List<BigInteger> get() {
        return ids;
    }

    public Wrapper(String s) {
        if (s == null) {
            ids = Collections.emptyList();
        }
        Iterable<String> splitted = Splitter.on(',')
                                            .split(s);
        Iterable<BigInteger> ids = Iterables.transform(splitted, Functionz.stringToBigInteger);
        this.ids = Lists.newArrayList(ids);
    }

    public Wrapper(List<BigInteger> ids) {
        this.ids = ids;
    }
}
公共类包装器实现可序列化{
私有列表ID=Collections.emptyList();
公共字符串toString(){
返回Joiner.on(“,”)
.加入(ids);
}
公共列表get(){
返回ID;
}
公共包装器(字符串s){
如果(s==null){
ids=Collections.emptyList();
}
Iterable splitted=Splitter.on(',')
.分割;
Iterable ID=Iterables.transform(拆分,Functionz.StringToBiginger);
this.ids=Lists.newArrayList(ids);
}
公共包装器(列表ID){
this.ids=ids;
}
}