简体   繁体   中英

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:

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.

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:

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

Then everything within that class has a unique part of the URL. You're already following that pattern in the CadetsResource class - I would recommend using it everywhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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