Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何使用名称空间前缀将POJO序列化为xml_Spring_Spring Mvc_Jackson_Xstream_Jaxb2 - Fatal编程技术网

Spring 如何使用名称空间前缀将POJO序列化为xml

Spring 如何使用名称空间前缀将POJO序列化为xml,spring,spring-mvc,jackson,xstream,jaxb2,Spring,Spring Mvc,Jackson,Xstream,Jaxb2,Spring提供了几种通过HttpMessageConverter将POJO转换为XML的方法。然而,我很难找到一个支持带有前缀的自定义名称空间的名称空间 例如来自 public class Student { String name; String address; Integer score; } 到 某个名字 地址 95 我很高兴地将MappingJackson2HttpMessageConverter与jackson dataformat xml一起使用,直到我意

Spring提供了几种通过HttpMessageConverter将POJO转换为XML的方法。然而,我很难找到一个支持带有前缀的自定义名称空间的名称空间

例如来自

public class Student {
   String name;
   String address;
   Integer score;
}


某个名字
地址
95
我很高兴地将MappingJackson2HttpMessageConverter与jackson dataformat xml一起使用,直到我意识到它不支持自定义前缀

然后我研究了将MarshallingHttpMessageConverter与XStreamMarshaller一起使用,结果发现XStream也不支持自定义前缀


有人能给我举一个例子,说明如何使用自定义名称空间前缀将POJO序列化为xml吗?谢谢。

我已经设法为杰克逊解决了类似的问题。首先,您必须使用woodstox XML处理器

<dependency>
  <groupId>org.codehaus.woodstox</groupId>
  <artifactId>woodstox-core-asl</artifactId>
  <version>4.4.0</version>
</dependency>
当然,这不是优雅的解决方案,但我不确定是否还有其他方法。我希望Jackson在将来的版本中会增加对前缀的api支持


但是,我想在您的情况下,需要使用带有前缀的默认名称空间,这似乎更困难,因为Jackson不支持默认名称空间(),甚至不支持将类继承与
@JacksonXmlRootElement(名称空间=”http://xmlns.uri.com“”
您仍然需要使用
@JacksonXmlProperty注释每个属性(名称空间=”http://xmlns.uri.com“

如果我只想在根元素上使用前缀怎么办?这对我不起作用,没有输出名称空间。设置输入属性的行对我来说似乎可疑,但用
mConfig.enableAutomaticNamespaces(true);
效果非常好。
<dependency>
  <groupId>org.codehaus.woodstox</groupId>
  <artifactId>woodstox-core-asl</artifactId>
  <version>4.4.0</version>
</dependency>
  XmlMapper mapper = new XmlMapper();
  // override default instance of WstxOutputFactory
  mapper.getFactory().setXMLOutputFactory(new WstxOutputFactory() {
    @Override
    public XMLStreamWriter createXMLStreamWriter(Writer w) throws XMLStreamException {
      mConfig.setProperty(WstxInputProperties.P_RETURN_NULL_FOR_DEFAULT_NAMESPACE,  true);
      XMLStreamWriter result = super.createXMLStreamWriter(w);
      result.setPrefix("xlink", "http://www.w3.org/1999/xlink");
      return result;
    }
  });