2011-01-04 28 views

risposta

17

Aggiungere il seguente agli endpoint e clienti:

<jaxws:features> 
    <bean class="org.apache.cxf.feature.LoggingFeature" /> 
</jaxws:features> 

Questo registrerà tutto al registro del server.

Se si desidera eseguire il log altrove, consultare il codice sorgente del CXF integrato LoggingInInterceptor e LoggingOutInterceptor. Puoi seguire lo schema che usano per afferrare i messaggi mentre entrano e uscire e fare con loro ciò che ti piace.

aggiungere la tua intercettori per la catena con qualcosa di simile:

<jaxws:inInterceptors> 
    <ref bean="myLoggingInInterceptor" /> 
</jaxws:inInterceptors> 
+0

Non si tratta solo di scaricare il contenuto non formattato, vale a dire non come XML? – irishguy

+0

Mostrerà XML nel registro del server. – BPS

+1

Per una funzionalità di registrazione più ricca di funzionalità, vedere https://github.com/greenbird/xml-formatter-components/tree/master/cxf – ThomasRS

26

Così, ho provato un po 'di più con questo. Per ottenere la richiesta XML e Risposte registrati, e se si utilizza Log4J, è necessario impostare il Log-livello di CXF nel file di log4j.xml come questo (> = INFO):

<logger name="org.apache.cxf" > 
    <level value="INFO" /> 
</logger> 

E il file dicxf.xml dovrebbe contiene questo:

<cxf:bus> 
    <cxf:features> 
     <cxf:logging/> 
    </cxf:features> 
</cxf:bus> 

Entrambi i file devono essere nel classpath.

Per visualizzare il messaggio soap aggiungere questo al codice:

Client client = ClientProxy.getClient(service); 
client.getInInterceptors().add(new LoggingInInterceptor()); 
client.getOutInterceptors().add(new LoggingOutInterceptor()); 
+0

Esistono due modi maggiormente utilizzati per eseguire la registrazione CXF. Questo è il modo di configurazione di log4j ... quello sopra è il modo del file di configurazione jaxws –

18

richiesta SOAP XML può essere registrato facilmente da un personalizzato In intercettore. Dire, abbiamo un intercettore denominato "wsLoggingInInterceptor", così nel file contesto sarà simile al seguente:

<bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/> 
<bean id="logOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> 
<bean id="wsLoggingInInterceptor" class="org.jinouts.webservice.logging.WSLoggingInInterceptor"/> 


    <cxf:bus> 
     <cxf:inInterceptors> 
      <ref bean="loggingInInterceptor"/> 
      <ref bean="wsLoggingInInterceptor"/> 
     </cxf:inInterceptors> 
     <cxf:outInterceptors> 
      <ref bean="logOutInterceptor"/>    
     </cxf:outInterceptors> 
    </cxf:bus> 

Nella classe siamo in grado di ottenere la richiesta XML come segue:

public class WSLoggingInInterceptor extends AbstractSoapInterceptor 
{ 

    public WSLoggingInInterceptor() 
    { 
     super(Phase.RECEIVE); 
    } 

    @Override 
    public void handleMessage (SoapMessage message) throws Fault 
    { 
     //get the remote address 
     HttpServletRequest httpRequest = (HttpServletRequest) message.get (AbstractHTTPDestination.HTTP_REQUEST); 
     System.out.println ("Request From the address : " + httpRequest.getRemoteAddr ()); 

     try 
     { 
      // now get the request xml 
      InputStream is = message.getContent (InputStream.class); 
      CachedOutputStream os = new CachedOutputStream (); 
      IOUtils.copy (is, os); 
      os.flush (); 
      message.setContent ( InputStream.class, os.getInputStream ()); 
      is.close (); 

      System.out.println ("The request is: " + IOUtils.toString (os.getInputStream ())); 
      os.close (); 
     } 

     catch (Exception ex) 
     { 
      ex.printStackTrace (); 
     } 

    } 

} 

sguardo , qui ho anche il log l'indirizzo da cui proviene la richiesta. È inoltre possibile ottenere ulteriori informazioni dall'oggetto "HttpServletRequest". si può avere di più da: http://cxf.apache.org/docs/interceptors.html

Per registro XML di risposta si può dare un'occhiata a this thread

2

E 'molto più facile aggiungere la tua logger alle proprietà endpoint. In questo caso l'intercettore di registrazione predefinito cercherà il tuo logger nelle proprietà dell'endpoint e se ne trova uno lo userà altrimenti creerà il default.Ecco il mio esempio di utilizzo:

<jaxws:endpoint 
     xmlns:client="http://service.info.client.diasoft.services.stream.integration.cib.sberbank.ru" 
     address="/diasoft/clientInfoWS" 
     serviceName="client:ClientWS" 
     implementor="#clientServiceImpl"> 
    <jaxws:properties> 
     <entry key="MessageLogger" value-ref="logger"/> 
    </jaxws:properties> 
    <jaxws:features> 
     <bean class="org.apache.cxf.feature.LoggingFeature"/> 
    </jaxws:features> 
</jaxws:endpoint> 


<bean id="logger" class="org.apache.cxf.common.logging.LogUtils" factory-method="getLogger"> 
    <constructor-arg value="ru.sberbank.cib.integration.stream.services.diasoft.client.info.service.ClientWSImpl"/> 
</bean> 
2

Se si utilizza Primavera dell'edificio con le sue Java-configurazione, ci sono 2 semplici modi per attivare la registrazione di sapone-messaggi con Apache CXF:

  1. Direttamente sul SpringBus - che è utile, se si desidera registrare i messaggi di tutti i tuoi CXF-endpoint:

    @Bean(name=Bus.DEFAULT_BUS_ID) public SpringBus springBus() { SpringBus springBus = new SpringBus(); LoggingFeature logFeature = new LoggingFeature(); logFeature.setPrettyLogging(true); logFeature.initialize(springBus); springBus.getFeatures().add(logFeature); return springBus; }

  2. attivare la registrazione separatamente la vigilia ry esposto CXF-endpoint

    @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), weatherService()); endpoint.publish(SERVICE_NAME_URL_PATH); endpoint.setWsdlLocation("Weather1.0.wsdl"); LoggingFeature logFeature = new LoggingFeature(); logFeature.setPrettyLogging(true); logFeature.initialize(springBus()); endpoint.getFeatures().add(logFeature); return endpoint; }

Ricordare al LoggingFeature.setPrettyLogging (true); Metodo per vedere i messaggi SOAP-messages e LoggingFeature.initialize (springBus()); - Senza quest'ultimo, la magia non accade. Per il codice pulitore, è anche possibile separare la caratteristica di registrazione come bean separato e iniettarlo nel proprio SpringBus o Endpoint-Bean.

Problemi correlati