简体   繁体   English

FOSRestBundle输出错误的xml

[英]FOSRestBundle output wrong xml

My XML output looks wierd when I request for xml.. 当我请求xml时,我的XML输出看起来很奇怪。

Controller: 控制器:

use FOS\RestBundle\Controller\Annotations as REST;
class RestController {
    /**
     * @REST\View
     */
    public function getAgenciesAction() {
      return array("bb"=>array('zz'=>'vv'),'zz');
    }
}

Request header: Aceept: application/xml 请求标头: Aceept: application/xml

Response : 回应

<?xml version="1.0" encoding="UTF-8"?>
<result>
  <entry>
    <entry><![CDATA[vv]]></entry>
  </entry>
  <entry><![CDATA[zz]]></entry>
</result>

why is only the end node returned and not all the keys and values? 为什么只返回结束节点而不是所有键和值?

The xml serializer in the FOSRestBundle doesn't serialize arrays with the key. FOSRestBundle中的xml序列化程序不使用密钥序列化数组。 Each array entry will result as <entry> in the response, regardless of the key. 无论密钥如何,每个数组条目都将在响应中作为<entry> The output in your example is correct. 示例中的输出是正确的。 Keys are only in the json output relevant. 键仅在json输出中相关。

Serialized entities have the correct output, as the field will result in <field>value</field> 序列化实体具有正确的输出,因为该字段将导致<field>value</field>

Example controller 示例控制器

/**
 * @ApiDoc(
 *     description="Returns the own user details",
 *     statusCodes={
 *         200="Returned when successful",
 *         403="Returned when missing permissions",
 *     }
 * )
 *
 * @Rest\Get("/users/me")
 * @Rest\View(serializerGroups={"details"})
 */
public function getMeAction()
{
    $user = $this->getUser();

    return array('user' => $user);
}

and the response. 和响应。 You see, the key user is outputted as <entry> . 你看,关键user输出为<entry>

<?xml version="1.0" encoding="UTF-8"?>
<result>
  <entry>
    <id><![CDATA[517781e2e707a00217000000]]></id>
    <username><![CDATA[admin]]></username>
    <email><![CDATA[admin@example.com]]></email>
    <company><![CDATA[acme]]></company>
  </entry>
</result>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM