如何自定义 JAX-WS RuntimeException 响应?

问题描述 投票:0回答:1

我正在尝试自定义在到达

RuntimeException
之前抛出的
WebMethod
的错误响应,例如无效的 xml 正文或 JAX-WS 服务的错误方法名称。 我尝试使用具有
@HandlerChain
的处理程序,但响应是在处理程序处理之前发送的。

这是一个例子。可以说我的 ws 中有一个方法 copyTool 方法,但客户端发送了以下请求。我希望能够自定义响应,以便我可以更改错误字符串或返回一些自定义错误代码。

输入

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:cor="http://acme.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <cor:copy>
      </cor:copy>
   </soapenv:Body>
</soapenv:Envelope>

输出

<S:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Header/>
   <S:Body>
      <ns0:Fault xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.w3.org/2003/05/soap-envelope">
         <faultcode>ns0:Client</faultcode>
         <faultstring>Cannot find dispatch method for {http://acme.com/}copy</faultstring>
      </ns0:Fault>
   </S:Body>
</S:Envelope>
java web-services jax-ws jax-ws-customization
1个回答
0
投票

我认为你可以尝试使用 ExceptionMapper,如下所示:

@Provider
public class WebApplicationExceptionMapper implements ExceptionMapper<Exception>{

    @Override
    public Response toResponse(Exception exception) {
        Response.Status responseStatus = THE_STATUS_YOU_WANT_BASED_ON_THE_EXCEPTION;
        return Response.status(responseStatus).entity(THE_ERROR_ENTITY_YOU_WANT_BASED_ON_THE_EXCEPTION).build();
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.