spring mvc处理xml与dtd的相对路径

时间:2016-03-23 13:16:05

标签: spring spring-mvc xml-parsing saxparser dtd-parsing

我的webservice从第三方来源收到xml,其中包含!DOCTYPE声明。因此我必须在我的控制器中使用第二种方法来解析xml文档,第一种方法给了我这个例外: Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not unmarshal to [class com.example.MeterBusXml]: null; nested exception is javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 48; DOCTYPE is disallowed when the feature "http://apache.org/xml/features/disallow-doctype-decl" set to true.]

我无法控制发布xml的应用程序,因此我必须调整我的web服务以使用dtd解析它。

我的问题是,Spring框架将EntityResolver注入每个XMLReader实例的方式是什么?

@RestController
public class MeterBusDataController {

  @RequestMapping (
    consumes = APPLICATION_XML_VALUE,
    method = POST,
    path = "/meterbus1"
  )
  public void method1(@RequestBody MeterBusXml xml) {
    System.out.println(xml);
  }

  @RequestMapping(
    method = POST,
    path = "/meterbus2"
  )
  public void method2(HttpServletRequest rq) throws IOException, ParserConfigurationException, SAXException, JAXBException {
    JAXBContext jc = newInstance(MeterBusXml.class);
    Unmarshaller um = jc.createUnmarshaller();
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);
    spf.setValidating(true);
    SAXParser sp = spf.newSAXParser();
    XMLReader xr = sp.getXMLReader();
    xr.setEntityResolver(new EntityResolver() {
      @Override
      public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
        return new InputSource(new StringReader(""));
      }
    });
    BufferedReader reader = rq.getReader();
    InputSource inputSource = new InputSource(reader);
    SAXSource saxSource = new SAXSource(xr, inputSource);
    MeterBusXml xml = (MeterBusXml)um.unmarshal(saxSource);
    System.out.println(xml);
  }
}

请参阅以下文档,了解我正在尝试解组的mbus.xml示例。 http://prevodniky.sk/products/product_EthMBus_common/download/Ethernet_converters_exports_v1_02_EN.pdf

1 个答案:

答案 0 :(得分:0)

我找到了问题的根源。首先,我尝试创建和配置Jaxb2Marshaller bean,但这没有用。然后我意识到,我需要一个HttpMessageConverter,所以我必须覆盖extendMessageConverters类中的WebMvcConfigurerAdapter方法,并在Jaxb2RootElementHttpMessageConverter上设置所需的属性。此邮件转换器不使用Jaxb2Marshaller,但它的内部工作方式非常相似。

setSupportDtd(true)是必需的,强制解析器接受!DOCTYPE声明。

setProcessExternalEntities(false)是必需的,因为如果此属性为false,则转换器使用空白EntityResolver,就像我在方法2中所做的那样。

@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {

  @Override
  public void extendMessageConverters(List<HttpMessageConverter<Jaxb2RootElementHttpMessageConverter?>> converters) {
    for (final Iterator<HttpMessageConverter<?>> iterator = converters.iterator(); iterator.hasNext();) {
      HttpMessageConverter<?> next = iterator.next();
      if (next instanceof Jaxb2RootElementHttpMessageConverter) {
        Jaxb2RootElementHttpMessageConverter jaxbConverter = (Jaxb2RootElementHttpMessageConverter) next;
        jaxbConverter.setProcessExternalEntities(false);
        jaxbConverter.setSupportDtd(true);
      }
    }
  }
}