【问题标题】:@ExclusionPolicy("all") and @Expose do nothing@ExclusionPolicy("all") 和 @Expose 什么都不做
【发布时间】:2014-07-16 23:44:21
【问题描述】:

我不明白为什么注释对我的 GET REST API 没有任何作用。 我在所有类的供应商中都有 JMS 序列化程序.. 但是当我调用我的 web 服务时,会出现我的所有属性.. 而我做了一个 @ExlusionPolicy("all") 并且只是 ID 属性上的strong>@Expose..

这是我的实体产品:

<?php

namespace GroupeGC\Bundle\ProductBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Jms\Serializer\Annotation as JMS;


/**
 * Product
 *
 * @ORM\Table(name="gc_product")
 * @ORM\Entity
 * @JMS\ExclusionPolicy("all")
 *
 */
class Product
{
 /**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 * @JMS\Type("integer")
 * @JMS\Expose
 */
private $id;


/**
 * @var string
 *
 * @ORM\Column(name="code", type="string", length=255, nullable=false)
 */
private $code;

/**
 * @var string
 *
 * @ORM\Column(name="label", type="string", length=255, nullable=true)
 *
 */
private $label;


/**
 * @var  float
 * @ORM\Column(name="volume", type="float")
 *
 */
private $volume;

 /**
 * @var float
 * @ORM\Column(name="weight", type="float")
 */
private $weight;
 /**
 * Get id
 *
 * @return integer
 */
public function getId()
{

    return $this->id;
}


 /**
* Set code
*
* @param string $code
* @return Product
*/
public function setCode($code)
{
    $this->code = $code;

    return $this;
}

/**
 * Get code
 *
 * @return string
 */
public function getCode()
{
    return $this->code;
}

/**
 * Set label
 *
 * @param string $label
 * @return Product
 */
public function setLabel($label)
{
    $this->label = $label;

    return $this;
}

/**
 * Get label
 *
 * @return string
 */
public function getLabel()
{
    return $this->label;
}


public function getVolume() {
    return $this->volume;
}

public function setVolume($volume) {
    $this->volume = $volume;
    return $this;
}


public function getWeight() {
    return $this->weight;
}

public function setWeight($weight) {
    $this->weight = $weight;
    return $this;
}

但是我们可以看到,通常情况下,我应该有应该出现在我的 JSON 中的 id 属性,而我有所有属性.. 我不明白。

编辑 1:这是 app/config 中的 fos_rest 配置:

fos_rest:
    view:
        failed_validation:    HTTP_BAD_REQUEST
        default_engine:       php
        formats:
            json:             true
            xml:              true
    format_listener:
        prefer_extension:     true
    body_listener:
        decoders:
            json:             fos_rest.decoder.json
            xml:              fos_rest.decoder.xml
    routing_loader:
        default_format:       json

fos_js_routing:
    routes_to_expose:         [oro_*]

我认为这里没有问题..

【问题讨论】:

    标签: php symfony fosrestbundle jmsserializerbundle


    【解决方案1】:

    By default, the serializer will retrieve, or set the value via reflection.here。因此,您的实体中的每个属性都将被检索/设置。

    /**
     * @JMS\ExclusionPolicy("all")
     * @JMS\AccessType("public_method")
     */
    

    使用 AccessType 注解时,您是在告诉序列化程序使用公共方法(例如 getX、setX、hasX)来检索/设置值。

    【讨论】:

    • 我有 @JMS\AccessType("public_method") 就像你说的,但总是不屑一顾。我总是拥有所有属性......这很奇怪
    • 是的,我总是在测试前清除缓存。我知道缓存可能是一个真正的问题,因为我们到处搜索,但在这里......甚至不是这个
    • 奇怪的是,即使使用 AccessType,你也有这个。您可以尝试查看是否调用了getId? (通过在其中写入类似 var_dump() 或 exit() 之类的内容)。
    • 我放了一个 var_dump("test");在我的 id getter 中,它在屏幕上写得很好。所以它也被称为 ..
    • 我真的不知道,:x。一种解决方案是使用 @JMS\Groups 将您的属性分组。
    猜你喜欢
    • 1970-01-01
    • 2021-07-31
    • 2019-02-21
    • 2020-10-15
    • 2019-03-08
    • 2016-12-24
    • 2017-04-18
    • 2011-08-03
    • 2014-05-11
    相关资源
    最近更新 更多