在封送Java对象时如何解决编码问题

时间:2019-04-10 09:39:43

标签: java encoding character-encoding jaxb marshalling

我正在使用模拟器,涉及两个请求:

1)发送请求A,并提取响应A并将其编组到xml    字符串。

2)请求B的部分包含响应A,我是通过以下操作从响应A中提取的:

String dataString = StringUtils.substringBetween(getDataServiceResponse, 
DATA_STRING_START, DATA_STRING_END);

我正在尝试封送请求B,但是包含响应A的部分未按我期望的方式编组(编码问题)。我粘贴了下面的xml来解释问题所在。

我尝试设置marshaller的属性:

private String marshall(Object data, Marshaller marshaller) throws 
JAXBException
{
  marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
  StringWriter writer = new StringWriter();
  marshaller.marshal(data, writer);

  return writer.toString();
}

预期结果:

  <Data type="XML">
    <OfferLookups>
      <Offer cache="1" content="1" expiry="1430434799" id="9078" present="1" promoId="4255">
        <Action>
          <Prn id="254" targetPrinter="EPSON::TM_T88IV">
            <Bep count="1" id="144" />
            <Overlay id="200" printDirection="topToBottom">
              <Group hAlign="center" id="201">
                <Img filename="file:///retailer_logo1.png" hAlign="center" id="210" />
              </Group>
            </Overlay>
            <Group hAlign="center" id="19" printDirection="topToBottom">
              <Text bold="true" charAlign="center" id="4">GREAT OFFER 1</Text>
              <Line id="6" />
            </Group>
            <Cut id="500" lineFeed="100" type="full" />
         </Prn>
       </Action>
     </Offer>
   </OfferLookups>
 </Data>  

实际结果:

 <Data>
    &lt;OfferLookups&gt;
      &lt;Offer cache="1" content="1" expiry="1430434799" id="9078" present="1" promoId="4255"&gt;
        &lt;Action&gt;
          &lt;Prn id="254" targetPrinter="EPSON::TM_T88IV"&gt;
            &lt;Bep count="1" id="144" /&gt;
            &lt;Overlay id="200" printDirection="topToBottom"&gt;
              &lt;Group hAlign="center" id="201"&gt;
                &lt;Img filename="file:///retailer_logo1.png" hAlign="center" id="210" /&gt;
              &lt;/Group&gt;
            &lt;/Overlay&gt;
            &lt;Group hAlign="center" id="19" printDirection="topToBottom"&gt;
              &lt;Text bold="true" charAlign="center" id="4"&gt;GREAT OFFER 1&lt;/Text&gt;
              &lt;Line id="6" /&gt;
            &lt;/Group&gt;
            &lt;Cut id="500" lineFeed="100" type="full" /&gt;
         &lt;/Prn&gt;
       &lt;/Action&gt;
     &lt;/Offer&gt;
   &lt;/OfferLookups&gt;
 </Data>

1 个答案:

答案 0 :(得分:0)

我从JAXB Marshalling获得了帮助,从而使这项工作得以实现。

有两个重要的位:

1)我必须更改元帅的逻辑:

  StringWriter stringWriter = new StringWriter();
                    PrintWriter printWriter = new PrintWriter(stringWriter);
                    DataWriter dataWriter = new DataWriter(printWriter, "UTF-8", new JaxbCharacterEscapeHandler());

2)实现JaxbCharacterEscapeHandler:

import com.sun.xml.bind.marshaller.CharacterEscapeHandler;

public class JaxbCharacterEscapeHandler implements CharacterEscapeHandler {

    public void escape(char[] buf, int start, int len, boolean isAttValue,
                    Writer out) throws IOException {

            for (int i = start; i < start + len; i++) {
                    char ch = buf[i];
                    out.write(ch);
            }
    }

}