Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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
如何使用(Java)Akka HTTP基于Accept头返回XML?_Java_Xml_Marshalling_Akka Http - Fatal编程技术网

如何使用(Java)Akka HTTP基于Accept头返回XML?

如何使用(Java)Akka HTTP基于Accept头返回XML?,java,xml,marshalling,akka-http,Java,Xml,Marshalling,Akka Http,我正在使用AKKAHTTP(Java的版本)来创建RESTAPI。我有一个返回application/json的概念证明。不幸的是,我找不到任何清晰的文档和/或(工作)示例,说明如何使它同时返回其他内容(在我的示例中为text/xml) 这是我目前拥有的相关部分(只要我发送一个不带Accept标头或带有Accept标头application/JSON的请求,它就会返回Airport对象的JSON表示): 不幸的是,我无法让Marshaller.One工作,编译器一直在抱怨: incompatib

我正在使用AKKAHTTP(Java的版本)来创建RESTAPI。我有一个返回application/json的概念证明。不幸的是,我找不到任何清晰的文档和/或(工作)示例,说明如何使它同时返回其他内容(在我的示例中为text/xml)

这是我目前拥有的相关部分(只要我发送一个不带Accept标头或带有Accept标头application/JSON的请求,它就会返回Airport对象的JSON表示):

不幸的是,我无法让Marshaller.One工作,编译器一直在抱怨:

incompatible types: inference variable A has incompatible equality constraints Object,Airport where A,B are type-variables:
A extends Object declared in method <A,B>oneOf(Seq<Marshaller<A,B>>)
B extends Object declared in method <A,B>oneOf(Seq<Marshaller<A,B>>)
不兼容类型:推理变量A具有不兼容的等式约束对象,其中A、B是类型变量:
方法oneOf(Seq)中声明的扩展对象
B扩展方法oneOf(Seq)中声明的对象
我不清楚我的A和B(Airport和RequestEntity)显然扩展了Object(Java中几乎所有的东西都是这样),这到底是什么问题。因此,任何来自一双新鲜眼睛的帮助都将不胜感激


另外:我在这里假设Akka HTTP的内置内容协商将根据Accept头中的值确定使用哪个封送器。我的这个假设是正确的,还是我试图用完全错误的方式解决这个问题?

好的,所以我自己想出来了(新的一天,新的一双眼睛;-)

事实上,我的问题中已经提到:

 List<Marshaller<List<Airport>, RequestEntity>> marshallers = new ArrayList();
 marshallers.add(Jackson.marshaller());
 marshallers.add(xmlMarshaller());

 Marshaller<List<Airport>, RequestEntity> airportMarshaller = Marshaller.oneOf(JavaConversions.asScalaBuffer(marshallers).toSeq());
xmlMarshaller()和toXML(Object)方法保持不变

因此,结果证明我的思路是正确的,但我只是忘记了我返回的是一个列表而不是一个机场,这导致了编译器错误

List<Marshaller<Airport, RequestEntity>> marshallers = new ArrayList();
marshallers.add(Jackson.marshaller());
marshallers.add(xmlMarshaller());

Marshaller<Airport, RequestEntity> airportMarshaller = Marshaller.oneOf(JavaConversions.asScalaBuffer(marshallers).toSeq());
completeOK(airport, airportMarshaller)
incompatible types: inference variable A has incompatible equality constraints Object,Airport where A,B are type-variables:
A extends Object declared in method <A,B>oneOf(Seq<Marshaller<A,B>>)
B extends Object declared in method <A,B>oneOf(Seq<Marshaller<A,B>>)
 List<Marshaller<List<Airport>, RequestEntity>> marshallers = new ArrayList();
 marshallers.add(Jackson.marshaller());
 marshallers.add(xmlMarshaller());

 Marshaller<List<Airport>, RequestEntity> airportMarshaller = Marshaller.oneOf(JavaConversions.asScalaBuffer(marshallers).toSeq());
completeOK(airport, airportMarshaller);