2014-10-09 2 views
1

나는 dropwizard.io 응용 프로그램을 가지고, 문제는 내 자원과 같은, GET 요청에 있습니다 :보내기 대신에 405 느릅 나무 빈 WebApplicationException의 404

@Path("/foobar") 
public class FooBarResource 
    (...) 
    @GET 
    @Path("/{id}") 
    @UnitOfWork 
    public Response getFooBar(@PathParam("id") Long id){ 
    return Response.status(200).entity(FooBarService.get(id)).build(); 
    } 

    @DELETE 
    @Path("/{id}") 
    @UnitOfWork 
    public Response getFooBar(@PathParam("id") Long id){ 
    return Response.status(200).entity(FooBarService.delete(id)).build(); 
    } 

    @PUT 
    @Path("/{id}") 
    @UnitOfWork 
    public Response getFooBar(@PathParam("id") Long id, FooBar fooBar){ 
    return Response.status(204).entity(FooBarService.update(id, fooBar)).build(); 
    } 
} 

을 그리고 나는 GET localhost:port/appPath/foobar/를 보낼 때 내가 대신 405을 404를 얻으려면 어떻게해야합니까? 내 응용 프로그램을 디버깅 할 때 내가 가지고있는 것은 javax.ws.rs.WebApplicationException이지만 비어 있습니다. 포트/appPath /는 foobar/당신이 (방법은 허용되지 않습니다) (405)를 얻을 :

+1

localhost : port/appPath/foobar /'에 대해 'GET'과 일치하는 다른 메소드가 있습니까? –

+0

전체 앱에 없음 – gniadson

+0

이것이 도움이 될 수 있는지 확인하십시오. http://stackoverflow.com/questions/25040222/restful-services-sending-http-405-instead-of-http-500-error – thepace

답변

0

당신은 당신은 로컬 호스트 해보 설명 here

@GET 
@Path("/{id:.*}") 
@UnitOfWork 
public Response getFooBar(@PathParam("id") String id){ 
    try { 
     return Response.status(200).entity(FooBarService.get(Long.parseLong(id))).build(); 
    } catch (NumberFormatException ignore) { 
     return Response.status(404).build(); 
    } 
} 
+0

여전히 405, 앱도 getFooBar 메소드에 들어 가지 않습니다. – gniadson

+0

이상하게 보입니다. 후행 슬래시로 요청을 제공하면이 방법으로 처리해야합니다. \ @GET 및 \ @UnitOfWork 주석이있는 다른 메서드 하나를 추가하지만 \ @Path는 없으면 직접 GET 요청을 처리해야합니다. [Path docs] (http://docs.oracle.com/javaee/6/api/javax/ws/rs/Path.html)를 참조하십시오. – ursa

1

같은 옵션 경로 매개 변수를 사용해야합니다. 그 이유는이 자원 (= foobar)이 존재하므로 404 (자원을 찾을 수 없음)가 잘못 되었기 때문입니다. 패스 주석 없이는 getFooBar() -> getFooBar2()의 복사본을 만들면 200이됩니다.

경로를 정확히 찾을 수없는 경우 예외가되는 컨테이너 필터를 만들 수 있습니다. 그런 다음이 예외를 404에 매핑 할 수 있습니다.

관련 문제