2010-03-17 94 views
4

我有一個類,我們稱之爲用戶@XmlRootElement註釋,具有一些屬性(名稱,姓氏等)。jaxb實體打印爲xml

我將這個類用於REST操作,如application/xml

客戶端將POST用戶類,所以我想保留在日誌中的值。

jaxb中是否有打印此對象的方法as xml

例如:

log.info("Customers sent: "+user.whichMethod()); 

應該產生這樣的輸出:

 
Customer sent: 
<user> <name>cristi</name> <surname>kevin</surname> </user> 

感謝。

+1

這已無關,與JAX-WS,您只需要JAXB(標籤是正確的,但你提到的JAX-WS文本)。 – 2010-03-17 12:01:00

回答

8

找到:)

public void toXml() { 
    try { 
     JAXBContext ctx = JAXBContext.newInstance(User.class); 
     Marshaller marshaller = ctx.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 
     marshaller.marshal(this, System.out); 
    } 
    catch (Exception 
      e) { 

       //catch exception 
    } 
} 

這樣稱呼它:

log.info("Customers sent: "+user.toXml()); 
+13

**從來沒有**寫一個空的catch-block,即使在一個例子中!例子有一些惡意的習慣,在某些時候變成生產代碼,那麼我們就需要保持這種混亂。 – 2010-03-17 12:01:44

17

可以使這個由您的端點訪問的常用方法。

public String toXml(JAXBElement element) { 
    try { 
     JAXBContext jc = JAXBContext.newInstance(element.getValue().getClass()); 
     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     marshaller.marshal(element, baos); 
     return baos.toString(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    }  
    return ""; 
} 
2

設置Marshaller.JAXB_FORMATTED_OUTPUT可能不利於記錄。

反而抑制XML Prolog (or Declaration)Marshaller.JAXB_FRAGMENT

public static <J> String printXml(final J instance) throws JAXBException { 
    return printXml(instance, instance.getClass()); 
} 


public static <J> String printXml(final J instance, 
            final Class<?>... classesToBeBound) 
    throws JAXBException { 

    final JAXBContext context = JAXBContext.newInstance(classesToBeBound); 

    final ByteArrayOutputStream output = new ByteArrayOutputStream(); 

    final Marshaller marshaller = context.createMarshaller(); 
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); 
    marshaller.marshal(instance, output); 
    // output.flush(); // Nasty IOException 
    final String jaxbEncoding = (String) marshaller.getProperty(
     Marshaller.JAXB_ENCODING); 

    try { 
     return new String(output.toByteArray(), jaxbEncoding); 
    } catch (UnsupportedEncodingException uee) { 
     throw new RuntimeException(uee); 
    } 
} 

會打印出這樣的單行。

<user><name>cristi</name><surname>kevin</surname></user> 
+2

由於這種方法可能用於日誌輸出,我不會拋出,而是捕獲所有異常,將其記錄(記錄器或控制檯)並返回「」+實例。 – Heri 2015-02-25 17:10:08

0
public String toXml(Event event) { 

    ByteArrayOutputStream baos = null; 

    try { 

     JAXBContext jc = JAXBContext.newInstance(event.getClass()); 
     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 

     baos = new ByteArrayOutputStream(); 
     marshaller.marshal(event, baos); 
     return baos.toString(); 
    } catch (JAXBException e) { 
     LOGGER.log(Level.SEVERE, " problem in Logging raw XML :"+e.getMessage()); 
    }  
    return baos.toString(); 

這行之有效