简体   繁体   English

Java EE-找不到GET方法

[英]Java EE - Can't find GET method

I have the following resource: 我有以下资源:

@Path("")
public class ReviewsResource {

  @Context
  private UriInfo context;

  @EJB
  private ReviewsReadBeanRemote reviewReadServices;

  @EJB
  private ReviewsBeanRemote reviewServices;

  public ReviewsResource() {
  }

  @GET
  @AuthenticateUser
  @Produces({MediaType.APPLICATION_JSON})
  @Path("cadets/{id}/reviews")
  public Response getApprovedReviewsByCadet(@PathParam("id") Long cadetId)
          throws EnviosYaException {
    return Response.ok(reviewReadServices.getReviewsByCadet(cadetId, State.APPROVED)).build();
  }

  @GET
  @AuthenticateAdministrator
  @Produces({MediaType.APPLICATION_JSON})
  @Path("reviews")
  public Response getReviews(@QueryParam("state") String state,
          @QueryParam("start") Date start, @QueryParam("end") Date end)
          throws EnviosYaException {
    State stateValue = null;
    if (state != null && !state.isEmpty()) {
      stateValue = State.valueOf(state);
    }
    return Response.ok(reviewReadServices.getReviews(stateValue, start, end)).build();
  }

  @GET
  @AuthenticateUser
  @Produces({MediaType.APPLICATION_JSON})
  @Path("clients/{id}/shipments")
  public Response getShipmentsPendingReviewByClient(@PathParam("id") Long clientId)
          throws EnviosYaException {
    return Response.ok(reviewReadServices.getShipmentsPendingReviewsByClient(clientId)).build();
  }
}

I can use the getReviews just fine like this: 我可以像这样很好地使用getReviews

https://localhost:8181/Gateway-war/reviews

But when I try to hit the others like this: 但是,当我尝试像这样打其他人时:

https://localhost:8181/Gateway-war/cadets/1/reviews

I get 404 Not found. 我得到404未找到。

Is there something wrong with the path? 路径有问题吗? Could it be because of the name of my resource? 可能是因为我的资源名称吗?

I do have another resource that starts like this: 我确实有另一个像这样的资源:

@Path("cadets")
public class CadetsResource {

Could the problem be it tries to look there? 问题可能出在那里吗?

Yes - the other path is interfering. 是的-其他路径正在干扰。 In general I put all of my services in a "namespace" per class. 通常,我将每个类的所有服务都放在“名称空间”中。 So the ReviewsResource might instead look like: 因此,ReviewsResource可能看起来像:

@Path("/reviews")
public class ReviewsResource {

Then everything within that class has a unique part of the URL. 然后,该类中的所有内容都具有URL的唯一部分。 You're already following that pattern in the CadetsResource class - I would recommend using it everywhere. 您已经在CadetsResource类中遵循了该模式-我建议在任何地方都使用它。

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

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