6

問題

デフォルトでは、JAX-WS は、キャッチされない拡張例外がRuntimeExceptionサーバーで発生すると、次の SOAP 障害メッセージを作成します。

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <S:Fault xmlns:ns3="http://www.w3.org/2003/05/soap-envelope">
         <faultcode>S:Server</faultcode>
         <faultstring>[runtime exception message here]</faultstring>
         <detail>
            <ns2:exception class="java.lang.RuntimeException" note="To disable this feature, set com.sun.xml.ws.fault.SOAPFaultBuilder.disableCaptureStackTrace system property to false" xmlns:ns2="http://jax-ws.dev.java.net/">
               <message>[runtime exception message here too]</message>
               <ns2:stackTrace>
                  [stack trace details]
               </ns2:stackTrace>
            </ns2:exception>
         </detail>
      </S:Fault>
   </S:Body>
</S:Envelope>

代わりにそれを送信するためにその動作を変更したいことを除いて、どのような意味がありますか:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <S:Fault xmlns:ns3="http://www.w3.org/2003/05/soap-envelope">
         <faultcode>S:Server</faultcode>
         <faultstring>Something wrong happened and it's totally our fault</faultstring>
      </S:Fault>
   </S:Body>
</S:Envelope>

このメッセージは、 RuntimeExceptionのメッセージ コンテンツではなく、サーバー側で発生する可能性のある拡張例外のカスタム静的メッセージであることに注意してください。RuntimeException

WSDL を変更できず、カスタム例外を設定したくありません。

私は春のプラグインを使用しています:com.sun.xml.ws.transport.http.servlet.WSSpringServlet

どうやってやるの?

4

2 に答える 2

0

SoapFaultMappingExceptionResolver http://docs.spring.io/spring-ws/site/reference/html/server.htmlを使用して問題にアプローチできると思います

SoapFaultMappingExceptionResolver は、より洗練された実装です。このリゾルバーを使用すると、スローされる可能性のある例外のクラス名を取得して、次のように SOAP Fault にマップできます。

<beans>
    <bean id="exceptionResolver"
        class="org.springframework.ws.soap.server.endpoint.SoapFaultMappingExceptionResolver">
        <property name="defaultFault" value="SERVER"/>
        <property name="exceptionMappings">
            <value>
                org.springframework.oxm.ValidationFailureException=CLIENT,Invalid request
            </value>
        </property>
    </bean> </beans>

キー値とデフォルトのエンドポイントは、faultCode,faultString,locale の形式を使用します。ここでは、障害コードのみが必要です。エラー文字列が設定されていない場合、デフォルトで例外メッセージが表示されます。言語が設定されていない場合、デフォルトで英語になります。上記の構成は、次の応答に見られるように、タイプ ValidationFailureException の例外を、障害文字列「Invalid request」を含むクライアント側の SOAP Fault にマップします。

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
       <SOAP-ENV:Fault>
           <faultcode>SOAP-ENV:Client</faultcode>
           <faultstring>Invalid request</faultstring>
       </SOAP-ENV:Fault>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

他の例外が発生した場合は、デフォルトの障害 (障害文字列として例外メッセージを含むサーバー側の障害) が返されます。

org.springframework.oxm.ValidationFailureException 例外を、関心のある例外、つまり java.lang.Exception または java.lang.RuntimeException に変更する必要があります。

カスタム例外クラスを作成することもできます

public class CustomGenericAllException extends RuntimeException {


    private String errorCode;
    private String errorMsg;

   //getter and setter for errorCode and errorMsg       

    public CustomGenericAllException(String errorCode, String errorMsg) {
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

}

すべてのメソッドで、この例外をスローできます

 throw new CustomGenericAllException("S:Server", "Something wrong happened and it's totally our fault");

xml 構成では、この一般的な例外をマップできます <value>com.testpackage.CustomGenericAllException ...

お役に立てれば

于 2016-06-27T11:57:15.960 に答える