![](/img/trans.png)
[英]How to show null value in JSON in FOS Rest Bundle with JMS Serializer?
[英]Symfony 3 FOS Rest + JMS Serializer groups
我的composer.json
(其中的一部分):
{
"require": {
"symfony/symfony": "3.1.*",
"jms/serializer-bundle": "^1.1",
"friendsofsymfony/rest-bundle": "^2.1"
}
}
我有一些實體,我想返回列表操作的部分數據,並完成查找操作。 為此,我有這些文件:
Product.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
/**
* @ORM\Entity
* @ORM\Table(name="represented")
* @JMS\ExclusionPolicy("ALL")
*/
class Product
{
/**
* @var integer
* @ORM\Column(type="integer", nullable=false, options={"unsigned"=true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @ORM\Column(type="string", nullable=false, length=48)
*/
protected $name;
/**
* @var Group
* @ORM\ManyToOne(targetEntity="Group", inversedBy="products")
* @ORM\JoinColumn(name="group_id", referencedColumnName="id")
*/
protected $group;
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function setGroup(Group $group)
{
$this->group = $group;
}
public function getGroup()
{
return $this->group;
}
}
ProductController.php
<?php
namespace AppBundle\Controller;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\FOSRestController;
use AppBundle\Entity\Product;
class ProductController extends FOSRestController
{
/**
* @Get("/product", name="list_products")
*/
public function listAction()
{
$products = $this->getDoctrine()
->getRepository('AppBundle:Product')
->findBy([], [ 'name' => 'ASC' ]);
$view = $this->view($products);
return $this->handleView($view);
}
/**
* @Get("/product/{id}", requirements={"id" = "\d+"}, name="get_product")
*/
public function getAction($id)
{
$em = $this->getDoctrine()->getManager();
$product = $em->getRepository('AppBundle:Product')
->find($id);
if ( ! $product) {
$error = [
'error' => 'Product not found'
];
$view = $this->view($error, 404);
} else {
$view = $this->view($product);
}
return $this->handleView($view);
}
}
我希望能夠不在列表結果上顯示group
屬性。 為此我嘗試了一些方法,主要是組。
Groups({"List"})
為我要在列表中顯示的屬性配置組名稱,並在控制器上使用@View(serializerGroups={"List"})
引用該組。 但這並沒有影響因為所有屬性都是可見的。 @ExclusionPolicy("all")
也不起作用。 @Expose
到我想要在一些或所有組中顯示的所有屬性,但這使得所有屬性都被標記為顯示。 我也嘗試了一些這些變種,但沒有任何改變結果。
控制器操作的結束不應該只是:
return $this->handleView($view);
但是為了讓一個或多個小組工作,你需要激活它們。 在類的頂部,您需要添加FOSRest Context,而不是JMS上下文:
use FOS\RestBundle\Context\Context;
並在返回視圖之前的控制器操作中:
$view = $this->view($products, 200);
$context = new Context();
$context->setGroups(['list']);
$view->setContext($context);
return $this->handleView($view);
這適用於Symfony 3.2和FOSRest 2.1和2.0。
用於升級FOSRest文檔的鏈接: https : //github.com/FriendsOfSymfony/FOSRestBundle/blob/master/UPGRADING-2.0.md
如果您使用@Exclude
那么這應該有效。 我使用的另一個選項是對config.yml
做一個小的補充:
fos_rest:
serializer:
groups: ['Default']
這要求實體屬性在Default
組中以便序列化。 如果屬性上沒有@Groups
注釋,那么它將處於Default
,但是只要添加@Groups
注釋,它就不再位於Default
組中(除非您專門添加它。)
這允許默認序列化過程包括除具有@Groups
注釋的那些實體字段之外的所有實體字段,然后在其他地方我可以序列化Default
和我選擇的其他組如果我想要包含所有內容。
// Function in the Controller class
public function getAction(MyEntity $me) {
// Use automatic serializer, only things in 'Default' group are included
return $me;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.