- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 Symfony 4 和 FOSRestBundle 构建 API。我有非常基本的关系:
实体:User.php
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @JMS\Groups({"onlyId"})
*/
private $id;
/**
* @ORM\Column(unique=true, type="string", length=255)
*/
private $email;
//...
/**
* @ORM\OneToMany(targetEntity="App\Entity\UserProduct", mappedBy="user", orphanRemoval=true)
*/
private $userProducts;
实体:UserProduct.php
/**
* @ORM\Entity(repositoryClass="App\Repository\UserProductRepository")
*/
class UserProduct
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @JMS\Groups({"onlyId"})
*/
private $id;
//...
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="userProducts")
* @JMS\Groups({"onlyId"})
*/
private $user;
配置:fos_rest.yaml
fos_rest:
body_converter:
enabled: true
view:
view_response_listener: 'force'
formats:
json: true
routing_loader:
default_format: json
# param_fetcher_listener: true
# allowed_methods_listener: true
# routing_loader: true
# view:
# view_response_listener: true
# exception:
# codes:
# App\Exception\MyException: 403
# messages:
# App\Exception\MyException: Forbidden area.
format_listener:
rules:
- { path: ^/api, prefer_extension: true, fallback_format: json, priorities: [ json, html ] }
- { path: ^/login_check, stop: true }
- { path: ^/register, stop: true }
起初,当我发布用户 ID 和 JSON 数据时,我无法POST UserProduct 将 JSON 发送到端点,因为它抛出它需要一个 entity
的实例。 , 不是 integer
或者我发送的任何内容。我通过本教程解决了这个问题 -> https://medium.com/@maartendeboer/using-the-symfony-serializer-with-doctrine-relations-69ecb17e6ebd
它使用 Symfony 序列化程序。我得到的是 circular reference
返回时出错 View
的 FOSRestBundle。在这一点上我知道 circular reference
使用 JMS Serializer 工具可以很好地处理错误,并在我的 postUserProductAction
中使用它 Controller :
POST 产品并处理 circular reference
我使用此 Controller (使用教程中自定义编写的反序列化方法反序列化发布的 JSON,将其插入数据库,然后在 JMS 序列化程序及其组的帮助下返回Response(不是 View )):
class UserProductController extends FOSRestController
{
/**
* @Rest\Route("/api/product", name="userProduct")
*
*
* @Method("POST")
*
* @param Request $request
* @param SerializerInterface $serializer
* @return Response
* @throws \InvalidArgumentException
*/
public function postUserProductAction(Request $request, SerializerInterface $serializer): View
{
$userProduct = $serializer->deserialize($request->getContent(), UserProduct::class, 'json');
dump($userProduct);
$em = $this->getDoctrine()->getManager();
$em->persist($userProduct);
$em->flush();
$userProduct = $this->JMSSerializeWithGroups($userProduct,array('onlyId'));
return new View($userProduct, Response::HTTP_OK);
}
}
/**
* @param $entity
* @param array $groupArray
* @return mixed|string
*/
public function JMSSerializeWithGroups($entity, Array $groupArray)
{
$JMSserializer = SerializerBuilder::create()->build();
$entity = $JMSserializer->serialize( $entity, 'json', SerializationContext::create()->setGroups($groupArray));
return $entity;
}
我得到的响应很好且清晰:
{
"id": 100,
"user": {
"id": 9
}
}
但是好像有很多不必要的步骤。此外,如果我选择这种方法,我需要自己将 JMS 序列化添加到我的每个 Controller (GET、PUT 等)。
所以我的问题是为什么 FOSRestBundle 默认不使用 JMS 进行渲染 View
,正如它在其文档中所说的那样(如果已安装和注册)?我是否应该将它添加到某个地方以便 FOSRestBundle 识别它?也许如果可以的话,我一开始就不需要使用 Symfony Serializer?我想我可能会迷失在所有 Symfony 4 升级中。
谢谢!
最佳答案
由于我没有得到任何建议,所以我坚持使用我发布的相同解决方案。我会留下它以防有人需要它,因为它是一个完整的工作示例。
干杯
关于php - 使用 JMSSerializer 序列化的 Symfony 4 FOSRestBundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49982802/
我正在尝试构建 symfony2 rest api,但我正在为 FOSRestBundle 苦苦挣扎 起初我可以制作“@View()”注释,因为它总是会说我的模板丢失,无论我使用什么配置选项我都会得到
对于使用 FOSRestBundle 的 Web 服务,我创建了一个防火墙来强制登录以访问应用程序。 我的问题是,当我通过 ajax 调用 API 时,我需要在用户未通过身份验证时获取错误代码 401
我可以使用@QueryParam()批注获取GET参数,但是看起来它仅适用于查询字符串数据:/user?id=123。 我宁愿像/user/123一样使用它。为此,我可能会使用@Get("/user/
我正在使用FOSRestBundle。我想返回用户信息,但数据信息较少......如何做到这一点?这是我当前的回复 { "id": 1, "user": { "id":
我正在尝试使用 Symfony2 和 FOSRestBundle 来构建 REST 框架,但失败得很惨。 我已完成以下操作: 在我的 deps 文件中: [FOSRest] git=git:/
我正在尝试在 FOSRestBundle 中添加自定义异常控件,但它似乎忽略了我的自定义消息(响应的状态代码正常)。 我有: throw new HttpException(404, "User {$
我使用 Symfony2.2 和 FOSRestBundle 我设置 #app/config/config/yml myapp_rest: resource: "@MyappRestBundl
我正在寻找一个简单、愚蠢的解决方案来强制所有symfony2 的content-type 到application/json fosrestbundle 中的 http 错误消息(如 MethodNo
上下文 我发现了很多关于使用 FOSRest 进行部分 API 响应的问题,所有答案都基于 JMS 序列化程序选项(排除、包含、组等)。它工作正常,但我尝试实现一些不那么“静态”的东西。 假设我有一个
我正在尝试将 FOSRestBundle 集成到我的应用程序中。 我只想在路由匹配时使用它,如 ^/api。 我真的不知道如何使用这个包,而且我认为文档非常糟糕。 (无意冒犯)。 fos_rest:
我在使用 FOSRestBundle 时遇到错误。 Fatal error: Class 'JMS\Serializer\SerializationContext' not found in /hom
我的 fos_rest 部分的配置: fos_rest: serializer: serialize_null: true view: view_res
有人可以清楚地解释应该如何使用 FOSRest 为 REST 请求配置路由吗?每个教程似乎都有不同的做法。 我的 Controller : all(); // returns arra
我是 Symfony (5.3) 的新手,想扩展 RequestBodyParamConverter (FOSRestBundle 3.0.5) 创建一个 REST api。使用 @ParamConv
我正在使用 Symfony2 和 FosRestBundle 为我们的系统开发一个 REST API。FosRestBundle 是一个非常好的工具,但它似乎有一个限制:只有属性(priviate、p
我正在尝试让我的 Controller 与参数 getter 一起工作。我做了文档中指定的所有说明。所以我有:配置fos_rest.yml: fos_rest: param_fetcher_lis
这是我的配置文件: // app/config/config.yml fos_rest: body_listener: array_normalizer: fos_rest.n
config.yml: twig: debug: %kernel.debug% strict_variables: %kernel.debug% exce
我有一个 symfony 应用程序,它提供 RESTful API(用于移动应用程序)并具有后端管理。 我可以通过facebook成功登录后端,但是我应该如何允许通过RESTful API登录? 最佳
我正在使用 symphony a fosrestbundle 开发一个 api。我还想在我的 Controller 中使用构造函数注入(inject),就像我总是注入(inject)一些 Entity
我是一名优秀的程序员,十分优秀!