简体   繁体   English

如何指定枚举对象数组的RESTful资源查询参数

[英]How to specify RESTful resource query parameter of an array of enum objects

I have a RESTful resource which should take as a parameter a comma separated list of values. 我有一个RESTful资源,应该以逗号分隔的值列表作为参数。

http://host:port/path/to/resource?enums=abc,def,ghi

How do I make RestEasy and Jackson handle this? 我如何让RestEasy和Jackson进行处理?

@GET
public ResourceModel getResource( @QueryParam("enums") MyEnum[] values ) {
   return new ResourceModel( values );
}

I know I can make it a simple String with an internal parser. 我知道我可以使用内部解析器使它成为一个简单的String But I would much prefer to be able to specify the type so that Jackson does the conversion for me (returning appropriately numbered error codes if the input is bad, etc). 但是我更希望能够指定类型,以便Jackson替我完成转换(如果输入不正确,则返回正确编号的错误代码,等等)。

I've tried making values an array (as shown above), a List<MyEnum> and a custom MyList class extending List<MyEnum> with public static MyList fromString( String commaSeparatedValues ) method. 我已经试图使values的阵列(如上所示),一个List<MyEnum>和一个自定义MyList延伸类List<MyEnum>public static MyList fromString( String commaSeparatedValues )方法。 Nothing seems to work. 似乎没有任何作用。

There must be some way to introduce a custom converter for it? 必须采用某种方法来引入自定义转换器吗?

Thanks. 谢谢。 -Shadow -阴影

I found a work-around using something similar to the MyList variant mentioned above. 我发现使用与上述MyList变体类似的方法来解决。 For some reason RestEasy (or Jackson, I have no idea which) can't handle it if MyList extends ArrayList<MyEnum> . 出于某种原因,如果MyList extends ArrayList<MyEnum> ,那么RestEasy(或Jackson,我不知道哪个)无法处理它。 However it does seem to work if MyList implements Iterable<MyEnum> (renamed to MyEnums as it no longer is a List ). 但是,如果MyList implements Iterable<MyEnum> (因为它不再是List ,就重命名为MyEnums ,它似乎确实起作用。

public class MyEnums
   implements Iterable<MyEnum>
{
   @NotNull private final Collection<MyEnum> m_objects;

   public MyList ( @NotNull String stringValue )
   {
      m_objects = CSVEnums.getCommaSeparatedEnumValuesList( MyEnum.class, stringValue );
   }

   @NotNull
   @Override
   public Iterator<MyEnum> iterator ()
   {
      return m_objects.iterator();
   }
}

/** Used for deserializing lists of {@link Enum}s from a string of comma separated values. */
public abstract class CSVEnums
{
   @NotNull
   public static <T extends Enum<T>> Collection<T> getCommaSeparatedEnumValuesList (
      @NotNull Class<T> type,
      @Nullable String commaSeparatedValues )
   {
      if( commaSeparatedValues == null )
         return Collections.emptyList();

      Collection<T> collection = new ArrayList<T>();
      String[] splitStringValues = commaSeparatedValues.split( "," );
      for( String stringValue : splitStringValues )
         collection.add( getEnumValue( type, stringValue ) );
      return collection;
   }

   @NotNull
   private static <T extends Enum<T>> T getEnumValue (
      @NotNull Class<T> type,
      @NotNull String stringValue )
   {
      for( T possibleValue : type.getEnumConstants() )
      {
         if( possibleValue.name().equals( stringValue ) )
            return possibleValue;
      }
      throw new IllegalArgumentException( "Unexpected value '" + stringValue + "' found for type '" + type.getName() + "'" );
   }
}

This could probably be cleaned up a bit using Guava (Joiner/Splitter) libs, but this seems to work for me now. 可以使用Guava(Joiner / Splitter)库将其清除一些,但是现在看来对我来说有效。

@GET
@NotNull
public ResourceModel getResource( @QueryParam("enums") MyEnums values ) {
   ResourceModel model = new ResourceModel( values );
   for( MyEnum value : values )
      model.add( value );
   return model;
}

Again, the above could be cleaned up using Guava (Lists/Iterables) libs, but this works. 同样,可以使用Guava(Lists / Iterables)库清除上面的内容,但是可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM