2016-08-21 4 views
-2

필수 매개 변수를 가져 오는 메소드를 만들려고합니다. 나는 이런 식으로 일을 해요 :저지 예외가 확실하지 않습니다.

@Path("/insertPerson") 
@GET 
@UnitOfWork 
public short insertPerson(@NotNull @QueryParam("person") Person per) 
{ ... } 

내가 매개 변수없이 GET 요청을 보내려고, 내가 다시 수신하고 다음 HTML 페이지 :

<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
    <title>Error 404 Not Found</title> 
</head> 
<body> 
    <h2>HTTP ERROR 404</h2> 
    <p>Problem accessing /person/insertPerson/. Reason: 

     <pre> Not Found</pre> 
    </p> 
    <hr> 
    <i> 
     <small>Powered by Jetty://</small> 
    </i> 
    <hr/> 
</body> 

거기인가 방법은 null 개체의 결과로 다시 보내고있어 제어하는 ​​방법? 오류 404는 null 매개 변수로 인해이 문제가 발생했음을 알기에 충분하지 않습니다. HTML 페이지 대신 간단한 문자열을 보내는 방법이 있습니까?

답변

0

내가 결국 한 것은 자동으로 오류가 발생했을 때 예외를 만드는 방법을 찾는 것입니다. 그래서 ExceptionMapper를 사용하여 예외 메시지와 함께 응답을 생성합니다 :

@Provider 
public class CustomExceptionMapper implements ExceptionMapper<Exception> 
{ 
    @Override 
    public Response toResponse(Exception exception) 
    { 
     String error = exception.getMessage(); 
     return Response.status(Status.BAD_REQUEST).entity(error).type("text/plain").build(); 
    } 
} 
관련 문제