【问题标题】:Multiple get methods for single restlet resource单个restlet资源的多个get方法
【发布时间】:2017-04-05 08:43:59
【问题描述】:

我有以下两个查询:

/api/resource/{id}
/api/resource?id=x&id=y&id=z

它们需要使用两种不同的资源方法:

@Get
public DTO getSingleDTO();
@Get
public List<DTO> getMultipleDTO();

是否可以将这两种方法放入单个restlet资源中? 我尝试使用@Get("?id"),但这个注解不是很强大,在我的情况下也无法使用。

实现这一点的最佳方法是什么?我不想为我拥有的每个资源实现两个控制器。

【问题讨论】:

    标签: java rest restlet


    【解决方案1】:

    我不知道你的模型,但据我所见,我会选择单一路线和单一资源方法:

    在应用程序类中:

    router.attach("/api/resource/{id_1}?id_2=y&id_3=z", MyResource.class)
    

    MyResource 类中:

       public Representation doMyThing() {
    
          String id1 = getRequestAttributes("id_1").toString()getRequestAttributes();
          String id2 = getQueryValue("id_2");
          String id3 = getQueryValue("id_3");
    
          if (StringUtils.isEmpty(id2) && StringUtils.isEmpty(id3)) {
            DTO result = service.handleSingleId(id1);
            return JsonRepresentation(result);
          } else {
            List<DTO> result = service.handleMultipleIds(id1, id2, id3);
            return JsonRepresentation(result);
          }     
       }
    

    你总是可以返回一些其他的Representation 而不是我建议的JsonRepresentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-27
      • 1970-01-01
      • 2011-11-29
      • 2020-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多