2016-06-29 6 views
1

BadRequestExceptionにメッセージを含めることは可能ですか?ユーザーが応答コード400を確認したときに、その理由を把握することもできますか?BadRequestExceptionにメッセージを含めるにはどうすればいいですか?

シナリオが簡略化され、このような何か、次のようになります。私は、これは代わりにResponseオブジェクトを返す行うことができますが、私はそれを望んでいないだろう知っている

public Entity getEntityWithOptions(@PathParam("id") String id, @QueryParam("option") String optValue) { 
    if (optValue != null) { 
     // Option is an enum 
     try { 
      Option option = Option.valueOf(optValue); 
     } catch (IllegalArgumentException e) { 
      throw new BadRequestException(e.getMessage()); 
     } 
     return new Entity(option); 
    } 
    return new Entity(); 
} 

これは可能ですか?たぶんExceptionMapper<BadRequestException>と? BadRequestExceptionはすでにジャージー特有の例外であるため、これを行うことはできませんか?

+0

私は右、メッセージがコンストラクタに入れることができると思いますか?また、exceptionMapperが使用されている場合、実際にはResponseを返します。 – Cuero

+0

メッセージをコンストラクタに渡していますが、レスポンスには含まれていません。 – dabadaba

+0

exceptionMapperを使用すると、コンストラクターに渡されたメッセージが表示されます。このようにマッパーを作成するだけです:public Response toResponse(BadRequestException e){Response.status(Response.Status.BAD_REQUEST).type(MediaType.TEXT_PLAIN).entity(ExceptionUtils.getStackTrace(e))。build();} – Cuero

答えて

0

あなたはこの

public class CustomBadReq extends WebApplicationException { 
    public CustomBadReq(String message) { 
     super(Response.status(Response.Status.BAD_REQUEST) 
      .entity(message).type(MediaType.TEXT_PLAIN).build()); 
    } 
} 

のようなカスタム例外を作成する必要がありますあなたがCustomExceptionを投げると、カスタマイズされた応答を提供するために、CustomExceptionMapperにマッピングすることができ、またthis

1

参照してください。

public class CustomException extends RuntimeException {  
    public CustomException(Throwable throwable) { 
     super(throwable); 
    } 

    public CustomException(String string, Throwable throwable) { 
     super(string, throwable); 
    } 

    public CustomException(String string) { 
     super(string); 
    } 

    public CustomException() { 
     super(); 
    } 
} 

@Provider 
public class CustomExceptionMapper implements ExceptionMapper<CustomException> { 
    private static Logger logger = Logger.getLogger(CustomExceptionMapper.class.getName()); 
    /** 
    * This constructor is invoked when exception is thrown, after 
    * resource-method has been invoked. Using @provider. 
    */ 
    public CustomExceptionMapper() { 
     super(); 
    } 

    /** 
    * When exception is thrown by the jersey container.This method is invoked 
    */ 
    public Response toResponse(CustomException ex) { 
     logger.log(Level.SEVERE, ex.getMessage(), ex); 
     Response.ResponseBuilder resp = Response.status(Response.Status.BAD_REQUEST) 
         .entity(ex.getMessage()); 

     return resp.build(); 
    } 
} 

このようなコードでは、CustomExceptionを使用してください。

public Entity getEntityWithOptions(@PathParam("id") String id, 
            @QueryParam("option") String optValue) 
            throws CustomException { 
    if (optValue != null) { 
     // Option is an enum 
     try { 
      Option option = Option.valueOf(optValue); 
     } catch (IllegalArgumentException e) { 
      throw new CustomException(e.getMessage(),e); 
     } 
     return new Entity(option); 
    } 
    return new Entity(); 
} 

メッセージの代わりに、オブジェクトを作成して、CustomExceptionを介してマッパーに渡すこともできます。

1

以下に示すように、これには本当に簡単な方法があります。

Response.ResponseBuilder resp_builder=Response.status(Response.Status.BAD_REQUEST); 
resp_builder.entity(e.getMessage());//message you need as the body 
throw new WebApplicationException(resp_builder.build()); 

あなたは、ヘッダー、応答のメディアタイプや他のいくつかの機能を追加する必要がある場合

ResponseBuilderはそれらすべてを提供します。

関連する問題