2012-11-01 54 views
4

我想有以下返回給瀏覽器(查看源代碼)莫西的@XmlCDATA似乎也沒有影響

<content> 
<![CDATA[Please show this inside a unescaped CDATA tag]]> 
</content> 

但我實際上可以得到

<content> 
Please show this inside a unescaped CDATA tag 
</content> 

如果我改變的價值內容爲

& lt;![CDATA [請在未轉義的CDATA標籤中顯示此內容]] & gt; ,標籤的小於和大於轉義。想知道如何實現我想要的嗎???? ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

這裏是我的代碼

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 

@Path("/myRequest") 
public class MyRestClass { 

    @GET 
    @Path("{myPathNumber}") 
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 
    public Object doInquiry(@PathParam("myPathNumber") String myPathNumber) { 
     try { 
      return new MyObject(); 
     } catch (Exception e) { 
      return "exception " + e.getMessage(); 
     } 
    } 
} 
package org.openengine.wink; 

import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import org.eclipse.persistence.oxm.annotations.XmlCDATA; 

@XmlRootElement 
public class MyObject implements Serializable { 

    @XmlElement 
    @XmlCDATA 
    private String content = "Please show this inside a unescaped CDATA tag"; 

} 

在包org.openengine.wink我有一個文件,jaxb.properties,具有以下內容

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 

回答

4

CLASSPATH

我最好的猜測是那EclipseLink JAXB (MOXy)未在您的類路徑中正確配置,並且JAXB RI正被用作您的環境中的JAXB(JSR-222)提供程序。


元數據

您提供的EclipseLink JAXB (MOXy)元似乎是正確的。這可以通過以下獨立演示代碼進行驗證。

爲MyObject

默認情況下JAXB (JSR-222)實現查找元的財產(的getter/setter)。既然您已註釋了該字段,我建議您使用@XmlAccessorType(XmlAccessType.FIELD註釋(請參閱:http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html)。

package org.openengine.wink; 

import java.io.Serializable; 
import javax.xml.bind.annotation.*; 
import org.eclipse.persistence.oxm.annotations.XmlCDATA; 

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class MyObject implements Serializable { 

    @XmlElement 
    @XmlCDATA 
    private String content = "Please show this inside a unescaped CDATA tag"; 

} 

jaxb.properties

要指定莫西爲您的JAXB提供者,你需要對你的類路徑中的EclipseLink二進制文件,並有一個在同一個包稱爲jaxb.properties與以下域模型文件(參見:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html)。

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 

演示

package org.openengine.wink; 

import javax.xml.bind.*; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(MyObject.class); 

     MyObject myObject = new MyObject(); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(myObject, System.out); 
    } 

} 

輸出

<?xml version="1.0" encoding="UTF-8"?> 
<myObject> 
    <content><![CDATA[Please show this inside a unescaped CDATA tag]]></content> 
</myObject> 

更多信息

相關問題