XML 支持

XML Support

Akka HTTP’s marshalling and unmarshalling infrastructure makes it rather easy to seamlessly support specific wire representations of your data objects, like JSON, XML or even binary encodings.

Akka HTTP 的 编组解组 基础设施使得无缝支持对象的线上表示变得相当容易,例如:JSON、XML或二进制编码。

Akka HTTP does not currently provide a Java API for XML support. If you need to produce and consume XML, you can write a custom marshaller using Jackson, which is also the library used for providing JSON support.

import java.io.IOException;
import java.util.List;
import java.util.Arrays;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import akka.http.javadsl.model.*;
import akka.http.javadsl.marshalling.Marshaller;
import akka.http.javadsl.unmarshalling.Unmarshaller;

public class JacksonXmlSupport {
  private static final ObjectMapper DEFAULT_XML_MAPPER =
    new XmlMapper().enable(SerializationFeature.WRAP_ROOT_VALUE);
  private static final List<MediaType> XML_MEDIA_TYPES = Arrays.asList(MediaTypes.APPLICATION_XML, MediaTypes.TEXT_XML);

  public static <T> Marshaller<T, RequestEntity> marshaller() {
    return Marshaller.wrapEntity(
      u -> toXML(DEFAULT_XML_MAPPER, u),
      Marshaller.stringToEntity(),
      MediaTypes.APPLICATION_XML
    );
  }

  public static <T> Unmarshaller<HttpEntity, T> unmarshaller(Class<T> expectedType) {
    return Unmarshaller.forMediaTypes(XML_MEDIA_TYPES, Unmarshaller.entityToString())
                       .thenApply(xml -> fromXML(DEFAULT_XML_MAPPER, xml, expectedType));
  }

  private static <T> String toXML(ObjectMapper mapper, T object) {
    try {
      return mapper.writeValueAsString(object);
    } catch (IOException e) {
      throw new IllegalArgumentException("Cannot marshal to XML: " + object, e);
    }
  }

  private static <T> T fromXML(ObjectMapper mapper, String xml, Class<T> expectedType) {
    try {
      return mapper.readerFor(expectedType).readValue(xml);
    } catch (IOException e) {
      throw new IllegalArgumentException("Cannot unmarshal XML as " + expectedType.getSimpleName(), e);
    }
  }
}

The custom XML (un)marshalling code shown above requires that you depend on the jackson-dataformat-xml library.

sbt
libraryDependencies += "com.fasterxml.jackson.dataformat" % "jackson-dataformat-xml" % "2.10.1"
Gradle
dependencies {
  compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.10.1'
}
Maven
<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-xml</artifactId>
  <version>2.10.1</version>
</dependency>

For XML Akka HTTP currently provides support for Scala XML right out of the box through it’s akka-http-xml module.

对于 XML,Akka HTTP 当前通过 akka-http-xml 模块对 Scala XML 提供了开箱即用的支持 。

Scala XML Support

Scala XML 支持

The ScalaXmlSupport trait provides a FromEntityUnmarshaller[NodeSeq] and ToEntityMarshaller[NodeSeq] that you can use directly or build upon.

ScalaXmlSupport trait 提供了 FromEntityUnmarshaller[NodeSeq]ToEntityMarshaller[NodeSeq],你可以直接使用或者在它基础之上构建自己的。

In order to enable support for (un)marshalling from and to XML with Scala XML NodeSeq you must add the following dependency:

为了启用 XML 和 Scala XML NodeSeq 的(解)编组支持,你必须添加下面的依赖:

sbt
libraryDependencies += "com.typesafe.akka" %% "akka-http-xml" % "10.1.11"
Gradle
dependencies {
  compile group: 'com.typesafe.akka', name: 'akka-http-xml_2.12', version: '10.1.11'
}
Maven
<dependency>
  <groupId>com.typesafe.akka</groupId>
  <artifactId>akka-http-xml_2.12</artifactId>
  <version>10.1.11</version>
</dependency>

Once you have done this (un)marshalling between XML and NodeSeq instances should work nicely and transparently, by either using import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport._ or mixing in the akka.http.scaladsl.marshallers.xml.ScalaXmlSupport trait.

一旦你完成 XML 和 NodeSeq 实例之间的(解)编组功能,它就可以很好的、透明的工作。 使用 import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport._ (导入隐式转换)或者混入 akka.http.scaladsl.marshallers.xml.ScalaXmlSupport trait。

在此文档中发现错误?该页面的源代码可以在 这里 找到。欢迎随时编辑并提交 Pull Request。