Java 单个资源下的多个GET:restapi

Java 单个资源下的多个GET:restapi,java,rest,jax-ws,Java,Rest,Jax Ws,我试图创建两个相互链接的GET请求,也就是说,一个是另一个的子请求。请查看以下代码: @GET @QueryParam("{customerId}") public List<customerModel> getCustomers(@QueryParam("customerId") Long customerId) { if (customerId== null || customerId== 0) throw new WebApplicationException(Resp

我试图创建两个相互链接的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
@QueryParam(“{customerId}”)
公共列表getCustomers(@QueryParam(“customerId”)长customerId){
if(customerId==null | | customerId==0)
抛出新的WebApplicationException(Response.status(status.BAD_REQUEST).entity(“400:customerId不能为null!”).build());
试一试{
......
......
返回findCustomer(customerId);
}捕获(例外e){
log.error(“获取主详细信息customerId时发生异常:“+customerId+”,错误:“+e.getMessage(),e”);
抛出新的WebApplicationException(Response.status(status.INTERNAL_SERVER_ERROR).entity(“…”).build());
}
}
@得到
@路径(“{customerId}/moreInfo”)
公共列表getCustomerInfo(@PathParam(“customerId”)长customerId){
if(customerId==null | | customerId==0)
抛出新的WebApplicationException(Response.status(status.BAD_REQUEST).entity(“customerId不能为null”).build();
试一试{
.....
.....
返回getCustomerInfo(customerId);
}捕获(例外e){
log.error(“获取客户详细信息customerId时发生异常:”+e.getMessage(),e);
抛出新的WebApplicationException(Response.status(status.INTERNAL_SERVER_ERROR).entity(“..”).build());
}
}

如果我们注意到,两个GET调用都在做相同的初始和日志工作。唯一的区别是方法调用。是否有一种方法可以编写相同的代码,根据路径调用不同的函数?

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

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

公共类CustomerWebService扩展。。。实现IdChecker{
@凌驾
公共无效检查id(长id){
如果(id==null | | id==0)
抛出新的WebApplicationException(Response.status(status.BAD_REQUEST).entity(“400:customerId不能为null!”).build());
}
私有无效日志消息(异常e,字符串原因){
log.error(String.format(“在%s时发生异常”,原因),e);
抛出新的WebApplicationException(Response.status(status.INTERNAL_SERVER_ERROR).entity(“…”).build());
}
@得到
@QueryParam(“{customerId}”)
公共列表getCustomers(@QueryParam(“customerId”)长customerId){
checkId(customerId);
试一试{
......
......
返回findCustomer(customerId);
}捕获(例外e){
String action=“获取主明细customerId:+customerId;
日志信息(e,行动);
}
}
@得到
@路径(“{customerId}/moreInfo”)
公共列表getCustomerInfo(@PathParam(“customerId”)长customerId){
checkId(customerId);
试一试{
.....
.....
返回getCustomerInfo(customerId);
}捕获(例外e){
String action=“获取客户详细信息customerId:+customerId;
日志信息(e,行动);
}
}

为什么不创建另一个pathparam/{moreinfo}并检查此参数是否存在?您使用的框架是什么?@CamilleGerin Roze,我们可以这样做吗?您可以提供一行示例说明如何做到这一点吗?据我所知,您的方法与
findCustomer(customerId)完全相同
getCustomerInfo(customerId);
,但这两个方法都返回一个
列表
,所以这很混乱。@cricket\u 007抱歉,这是一个输入错误,它们返回的是不同的型号
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);
        }
    }