-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* =doc #1290 Merge common/index and common/xml-support pages Akka HTTP does not provide a Java API for handling XML so point to Jackson for a custom integration. * Add example on how to unmarshal XML using Jackson * Make the Java XML support example more complete * Fix typo: unmarshallar -> unmarshaller
- Loading branch information
Showing
9 changed files
with
235 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../scala/http/common/index.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../scala/http/common/xml-support.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
docs/src/test/java/docs/http/javadsl/JacksonXmlExampleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
|
||
package docs.http.javadsl; | ||
|
||
import org.junit.Test; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.TimeUnit; | ||
import com.fasterxml.jackson.annotation.JsonRootName; | ||
|
||
import akka.http.javadsl.testkit.JUnitRouteTest; | ||
import akka.http.javadsl.model.*; | ||
import akka.http.javadsl.server.Route; | ||
import akka.http.javadsl.unmarshalling.Unmarshaller; | ||
|
||
public class JacksonXmlExampleTest extends JUnitRouteTest { | ||
|
||
final String xml = "<point><x>3</x><y>4</y></point>"; | ||
final Point point = new Point() { | ||
{ | ||
setX(3); | ||
setY(4); | ||
} | ||
}; | ||
|
||
@Test | ||
public void marshalXml() throws Exception { | ||
final Route route = route( | ||
completeOK(point, JacksonXmlSupport.<Point>marshaller()) | ||
); | ||
|
||
testRoute(route) | ||
.run(HttpRequest.GET("/")) | ||
.assertStatusCode(StatusCodes.OK) | ||
.assertEntity(xml); | ||
} | ||
|
||
@Test | ||
public void unmarshalXml() throws Exception { | ||
final Unmarshaller<HttpEntity, Point> unmarshaller = JacksonXmlSupport.unmarshaller(Point.class); | ||
|
||
final Route route = route( | ||
entity(unmarshaller, p -> { | ||
assertEquals(p, point); | ||
return complete(p.toString()); | ||
}) | ||
); | ||
|
||
final RequestEntity entity = HttpEntities.create(ContentTypes.TEXT_XML_UTF8, xml); | ||
|
||
testRoute(route) | ||
.run(HttpRequest.POST("/").withEntity(entity)) | ||
.assertStatusCode(StatusCodes.OK) | ||
.assertEntity("Point(x=3, y=4)"); | ||
} | ||
|
||
@Test | ||
public void unmarshalXmlDirect() throws Exception { | ||
{ | ||
CompletionStage<Point> resultStage = | ||
JacksonXmlSupport.unmarshaller(Point.class).unmarshal( | ||
HttpEntities.create(ContentTypes.TEXT_XML_UTF8, xml), | ||
system().dispatcher(), | ||
materializer()); | ||
|
||
assertEquals(point, resultStage.toCompletableFuture().get(3, TimeUnit.SECONDS)); | ||
} | ||
|
||
{ | ||
CompletionStage<Point> resultStage = | ||
JacksonXmlSupport.unmarshaller(Point.class).unmarshal( | ||
HttpEntities.create(ContentTypes.create(MediaTypes.APPLICATION_XML, HttpCharsets.UTF_8), xml), | ||
system().dispatcher(), | ||
materializer()); | ||
|
||
assertEquals(point, resultStage.toCompletableFuture().get(3, TimeUnit.SECONDS)); | ||
} | ||
} | ||
|
||
@JsonRootName("point") | ||
public static class Point { | ||
private int x, y; | ||
public void setX(int x) { this.x = x; } | ||
public int getX() { return this.x; } | ||
public void setY(int y) { this.y = y; } | ||
public int getY() { return this.y; } | ||
|
||
public String toString() { | ||
return "Point(x=" + x + ", y=" + y + ")"; | ||
} | ||
|
||
public boolean equals(Object other) { | ||
Point that = other instanceof Point ? (Point) other : null; | ||
return that != null | ||
&& that.x == this.x | ||
&& that.y == this.y; | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
docs/src/test/java/docs/http/javadsl/JacksonXmlSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
|
||
package docs.http.javadsl; | ||
|
||
//#jackson-xml-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); | ||
} | ||
} | ||
} | ||
//#jackson-xml-support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters