【问题标题】:Multiple GET under a single resource: REST API单个资源下的多个 GET:REST API
【发布时间】:2017-01-22 22:16:19
【问题描述】:

我正在尝试创建两个相互链接的 GET 请求,也就是说,一个是另一个的孩子。请看下面的代码:

@GET
@QueryParam("{customerId}")
  public List<customerModel> getCustomers(@QueryParam("customerId") Long customerId) {
if (customerId== null || customerId== 0)
  throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("400: customerId cannot be null!").build());
try {
   ......
   ......
  return findCustomer(customerId); 
} catch (Exception e) {
  log.error("Exception occurred when fetching the master detail customerId:" + customerId+", error :"+e.getMessage(), e);
  throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity("......").build());
}
  }

@GET
@Path("{customerId}/moreInfo")
  public List<customerInfoModel> getCustomerInfo(@PathParam("customerId") Long customerId) {
    if (customerId== null || customerId== 0)
      throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("customerId cannot be null").build());
try {
  .....
  .....
  return getCustomerInfo(customerId);
} catch (Exception e) {
  log.error("Exception occurred when fetching the customer detail customerId:" + e.getMessage(), e);
  throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity("....").build());
}
  }

如果我们注意到,两个 GET 调用都在执行相同的初始和日志记录工作。唯一的区别是方法调用。有没有办法编写相同的代码,只根据路径调用不同的函数?

【问题讨论】:

  • 你为什么不创建另一个 pathparam /{moreinfo} 并检查这个参数是否存在?
  • 您使用的框架是什么?
  • @CamilleGerin-Roze,我们可以这样做吗?你能提供一个简单的例子来说明我们如何做到这一点吗?
  • 据我所知,除了findCustomer(customerId);getCustomerInfo(customerId); 之外,您的方法完全相同,但两种方法都返回List&lt;customerModel&gt;,所以这很混乱。
  • @cricket_007 抱歉打错了,他们定义返回不同的模型

标签: java rest jax-ws


【解决方案1】:

你可以提取一些方法。一个用于身份检查。一种用于记录错误。

public interface IdChecker {
    public void checkId(Long id);
}

public class CustomerWebService extends ... implements IdChecker {

    @Override
    public void checkId(Long id) {
        if (id == null || id == 0)
            throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("400: customerId cannot be null!").build());
    }

    private void logMessage(Exception e, String reason) {
        log.error(String.format("Exception occurred when %s", reason), e);
        throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity("......").build());
    }

    @GET
    @QueryParam("{customerId}")
    public List<customerModel> getCustomers(@QueryParam("customerId") Long customerId) {
        checkId(customerId);
        try {
           ......
           ......
          return findCustomer(customerId); 
        } catch (Exception e) {
            String action = "fetching the master detail customerId:" + customerId;
            logMessage(e, action);
        }
    }

    @GET
    @Path("{customerId}/moreInfo")
    public List<customerInfoModel> getCustomerInfo(@PathParam("customerId") Long customerId) {
        checkId(customerId);
        try {
          .....
          .....
          return getCustomerInfo(customerId);
        } catch (Exception e) {
            String action = "fetching the customer detail customerId:" + customerId;
            logMessage(e, action);
        }
    }

【讨论】:

    猜你喜欢
    • 2017-06-08
    • 2019-08-14
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    相关资源
    最近更新 更多