2016-07-14 25 views
3

Spring Marshallerを使用するときに、特殊文字を強制的にエスケープしたい。XMLの特殊文字を強制的に春にマーシャリングする

<description> 
 

 
<![CDATA[<p>With hundreds of practice questions and hands-on exercises, <b>SCJP Sun Certified Programmer for Java 6 Study Guide</b> covers what you need to know--and shows you how to prepare--for this challenging exam. </p>]]> 
 
</description>
:私は

package com.odr.core.action; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlRegistry; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement(name = "book") 
public class Book { 

    private String name; 
    private String author; 
    private String publisher; 
    private String isbn; 

    @XmlJavaTypeAdapter(value=CDATAAdapter.class) 
    private String description; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getAuthor() { 
     return author; 
    } 

    public void setAuthor(String author) { 
     this.author = author; 
    } 

    public String getPublisher() { 
     return publisher; 
    } 

    public void setPublisher(String publisher) { 
     this.publisher = publisher; 
    } 

    public String getIsbn() { 
     return isbn; 
    } 

    public void setIsbn(String isbn) { 
     this.isbn = isbn; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    @Override 
    public String toString() { 
     return "Book [name=" + name + ", author=" + author + ", publisher=" 
      + publisher + ", isbn=" + isbn + ", description=" + description 
      + "]"; 
    } 
} 

オブジェクト出力

 writer = new BufferedWriter(new FileWriter(selectedFile)); 
     context = JAXBContext.newInstance(Book.class); 
     Marshaller m = context.createMarshaller(); 
     m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 

     m.setProperty("com.sun.xml.bind.marshaller.CharacterEscapeHandler", 
       new CharacterEscapeHandler() { 
        @Override 
        public void escape(char[] ch, int start, int length, 
          boolean isAttVal, Writer writer) 
          throws IOException { 
         writer.write(ch, start, length); 
        } 
       }); 
     m.marshal(book, writer); 

XMLにjavax.xml.bind.Marshaller

ブッククラスを使用する場合、コードの下に完全に取り組んでいます

しかし、私はorg.springframework.oxm.jaxb.Jaxb2Marshallerを使用するときに同じ種類のコードが動作していない、以下のコード

Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller(); 
    Map<String, Object> map = new HashMap<String, Object>(); 
    map.put("jaxb.formatted.output", true); 
    jaxb2Marshaller.setPackagesToScan("com.odr.core.action"); 
    // com.sun.xml.bind.characterEscapeHandler 
    // com.sun.xml.bind.marshaller.CharacterEscapeHandler 
    map.put("com.sun.xml.bind.marshaller.CharacterEscapeHandler", 
      new CharacterEscapeHandler() { 
       @Override 
       public void escape(char[] ac, int i, int j, boolean flag, 
         Writer writer) throws IOException { 
        writer.write(ac, i, j); 
       } 
      }); 
    jaxb2Marshaller.setMarshallerProperties(map); 

    org.springframework.oxm.Marshaller marshaller = jaxb2Marshaller; 
    FileOutputStream fos = null; 
    // String fileNamePath = directory.getAbsolutePath() + "\\" + fileName; 

    try { 
     // fos = new FileOutputStream(fileNamePath); 
     fos = new FileOutputStream(selectedFile); 
     marshaller.marshal(book, new StreamResult(fos)); 

     // File f = new File(directory,fileName); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (fos != null) { 
      fos.close(); 
     } 
    } 

出力

<description>&lt;![CDATA[&lt;p&gt;With hundreds of practice questions and hands-on exercises, &lt;b&gt;SCJP Sun Certified Programmer for Java 6 Study Guide&lt;/b&gt; covers what you need to know--and shows you how to prepare--for this challenging exam. &lt;/p&gt;]]&gt;</description>
にあります

最初のスニペットは特殊文字をエンコードしませんでした。しかし、私は財産を設定していますが、春を使っている2番目のスニペットはエンコードしました。既存のコードに影響を与えないために、私のプロジェクトでSpringを使用する必要があります。

+0

どのバージョンのSpringを使用していますか?私はSpring 4.xでJaxb2Marshallerの同じコードを試してみました。 –

+0

私はspring-oxm-4.1.5.RELEASE.jarを使用しています –

+0

Bookクラスを共有できますか? –

答えて

0

私は同じ問題を抱えていましたが、私はこのように解決しました。

最初のものが最初です。 2つのBeanを作成する必要があります。 1つはJaxb2Marshaller、もう1つはMarshallingHttpMessageConverterです。あなたの設定を保持したいと思っているので、あなたのコードを使用します。 Jaxb2Marshaller豆の作成

:まあ、私はのJava 8 を使用してい

@Bean 
public Jaxb2Marshaller getJaxb2Marshaller() { 
    Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller(); 
    Map<String, Object> map = new HashMap<String, Object>(); 
    map.put("jaxb.formatted.output", true); 
    jaxb2Marshaller.setPackagesToScan("com.odr.core.action"); 
    // com.sun.xml.bind.characterEscapeHandler 
    // com.sun.xml.bind.marshaller.CharacterEscapeHandler 
    map.put("com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler", 
      new CharacterEscapeHandler() { 
       @Override 
       public void escape(char[] ac, int i, int j, boolean flag, 
            Writer writer) throws IOException { 
        writer.write(ac, i, j); 
       } 
      }); 
    jaxb2Marshaller.setMarshallerProperties(map); 

    org.springframework.oxm.Marshaller marshaller = jaxb2Marshaller; 
    FileOutputStream fos = null; 
    // String fileNamePath = directory.getAbsolutePath() + "\\" + fileName; 

    try { 
     // fos = new FileOutputStream(fileNamePath); 
     fos = new FileOutputStream(selectedFile); 
     marshaller.marshal(book, new StreamResult(fos)); 

     // File f = new File(directory,fileName); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (fos != null) { 
      fos.close(); 
     } 
    } 

    return jaxb2Marshaller; 
} 

ので、私はcom.sun.xmlにcom.sun.xml.bind.marshaller.CharacterEscapeHandlerを変更しました。上記のようにinternal.bind.marshaller.CharacterEscapeHandlerを参照してください。

MarshallingHttpMessageConverter Beanを作成:

@Bean 
public MarshallingHttpMessageConverter getMarshallingHttpMessageConverter() { 
    return new MarshallingHttpMessageConverter(getJaxb2Marshaller()); 
} 

あなたは、私が問題を解決するために私自身のHttpMessageConverterを作成したことに気づく必要があります。それは、エンティティまたはDTOをXMLオブジェクトに変換する必要があるたびに、新しいMarshallerインスタンスを作成する独自のコンバータをSpringが使用するためです。だから、私はあなたの問題を解決する以下のコードを考える。それがあなたを助けることを願ってください。

import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter; 
import org.springframework.oxm.jaxb.Jaxb2Marshaller; 

@Configuration 
public class XmlParseConfig { 

    /** 
    * Constrói o bean de marshall e unmarshall. 
    * 
    * @return A instância do BEAN usando a implementação de {@link Jaxb2Marshaller} 
    */ 
    @Bean 
    public Jaxb2Marshaller getJaxb2Marshaller() { 
     Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller(); 
     Map<String, Object> map = new HashMap<String, Object>(); 
     map.put("jaxb.formatted.output", true); 
     jaxb2Marshaller.setPackagesToScan("com.odr.core.action"); 
     // com.sun.xml.bind.characterEscapeHandler 
     // com.sun.xml.bind.marshaller.CharacterEscapeHandler 
     map.put("com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler", 
       new CharacterEscapeHandler() { 
        @Override 
        public void escape(char[] ac, int i, int j, boolean flag, 
             Writer writer) throws IOException { 
         writer.write(ac, i, j); 
        } 
       }); 
     jaxb2Marshaller.setMarshallerProperties(map); 

     org.springframework.oxm.Marshaller marshaller = jaxb2Marshaller; 
     FileOutputStream fos = null; 
     // String fileNamePath = directory.getAbsolutePath() + "\\" + fileName; 

     try { 
      // fos = new FileOutputStream(fileNamePath); 
      fos = new FileOutputStream(selectedFile); 
      marshaller.marshal(book, new StreamResult(fos)); 

      // File f = new File(directory,fileName); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (fos != null) { 
       fos.close(); 
      } 
     } 

     return jaxb2Marshaller; 
    } 

    @Bean 
    public MarshallingHttpMessageConverter getMarshallingHttpMessageConverter() { 
     return new MarshallingHttpMessageConverter(getJaxb2Marshaller()); 
    } 
}