- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我一直在试用 Symfony 2.2、FOSRest Bundle(使用 JMS Serializer)和使用 MongoDB 的 Doctrine ODM。
在尝试找出如何正确设置 FOSRest Bundle 的许多小时后,我仍然遇到了一些麻烦:我有一个非常简单的路线,可以返回产品和价格列表。 每当我请求 HTML 格式时,我都会得到正确的响应,但如果我请求任何其他格式(JSON、XML),我会收到错误消息:
[{"message": "Resources are not supported in serialized data. Path: Monolog\\Handler\\StreamHandler -> Symfony\\Bridge\\Monolog\\Logger -> Doctrine\\Bundle\\MongoDBBundle\\Logger\\Logger -> Doctrine\\Bundle\\MongoDBBundle\\Logger\\AggregateLogger -> Doctrine\\ODM\\MongoDB\\Configuration -> Doctrine\\MongoDB\\Connection -> Doctrine\\ODM\\MongoDB\\LoggableCursor",
"class": "JMS\\Serializer\\Exception\\RuntimeException",...
您可以看到完整的错误信息here
我当前的设置非常简单:我创建了一个到 Controller 的单一路由,该 Controller 返回产品列表和价格(我按照 this example 创建产品文档)。
这是路线:
rest_product:
type: rest
resource: Onema\RestApiBundle\Controller\ProductController
这是 Controller :
<?php
namespace Onema\RestApiBundle\Controller;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Routing\ClassResourceInterface;
use FOS\Rest\Util\Codes;
use JMS\Serializer\SerializationContext;
use Onema\RestApiBundle\Document\Product;
class ProductController extends FOSRestController implements ClassResourceInterface
{
public function getAction()
{
$dm = $this->get('doctrine_mongodb')->getManager();
$products = $dm->getRepository('RestApiBundle:Product')->findAll();
if(!$products)
{
throw $this->createNotFoundException('No product found.');
}
$data = array('documents' => $products);
$view = $this->view($data, 200);
$view->setTemplate("RestApiBundle:Product:get.html.twig");
return $this->handleView($view);
}
}
这是从 Controller Resources/Product/get.html.twig 调用的 View :
<ul>
{% for document in documents %}
<li>
{{ document.name }}<br />
{{ document.price }}
</li>
{% endfor %}
</ul>
知道为什么这对一种格式有效但对其他格式无效吗?我还应该设置什么吗?
更新:这是我一直在使用的配置值。在 app/config/config.yml 的最后我有这个:
sensio_framework_extra:
view: { annotations: false }
router: { annotations: true }
fos_rest:
param_fetcher_listener: true
body_listener: true
format_listener: true
view:
formats:
json: true
failed_validation: HTTP_BAD_REQUEST
default_engine: twig
view_response_listener: 'force'
解决方法:
做了更多的研究,我遇到了另一个错误,导致我提出这个问题和答案:
https://stackoverflow.com/a/14030646/155248
一旦我通过将每个结果添加到数组中来摆脱 Doctrine\ODM\MongoDB\LoggableCursor
,如下所示:
$productsQ = $dm->getRepository('RestApiBundle:Product')->findAll();
foreach ($productsQ as $product) {
$products[] = $product;
}
return $products;
我开始以正确的格式获取结果。这是一种蹩脚的解决方案,仍然希望找到更好的答案来解决这个问题。
最佳答案
如果你想获得 RestApiBundle:Product 文档的集合,你必须在从存储库调用 find 方法或从查询构建器调用 getQuery 方法后调用方法“toArray”
/**
* @Route("/products.{_format}", defaults={"_format" = "json"})
* @REST\View()
*/
public function getProductsAction($_format){
$products = $this->get('doctrine_mongodb')->getManager()
->getRepository('RestApiBundle:Product')
->findAll()->toArray();
return $products;
}
您也可以调用 array_values($products) 以正确序列化排除策略
关于php - FOSRestBundle 和 JMS Serializer,获取 JSON 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16173429/
我正在读这个 question和 corresponding answer并被答案第一行中的术语 JMS broker 弄糊涂了: MS (ActiveMQ is a JMS broker imple
我正在学习 API 中的 Reactive Streams,我对它与 JMS 之间的相似性感到震惊。在 JMS 中,我们也有异步处理、发布者和订阅者。我在进行这种等效时缺少什么观点? 最佳答案 Rea
假设生产者向 JMS 主题“新闻”发送一条消息。消费者 1 读取了消息,但消费者 2 处于离线状态,因此他还没有读取消息。 是否有任何内置(针对规范或实现)的方式来通知生产者消费者 1 已阅读他的消息
目前我正在开发一个 JMS 应用程序。但我使用普通的 JMS API 和属性文件进行配置。我的应用程序在 Weblogic 中运行并连接到我客户端的 MQ 系列服务器。 最近我知道我可以使用 Webl
我正在尝试使用 Solace 中可用的异步发送功能,但我打算使用 JMS 进行抽象,而不是直接使用 JCSMP 使用它。 JMS 2.0 支持异步发送以及其他新功能:http://www.oracle
我无法获得 javax.jms.ConnectionFactory注入(inject)我的独立 JMS 客户端。 我得到一个 java.lang.NullPointerException在 conne
保持 JMS 连接/ session /消费者始终打开是一种不好的做法吗? 代码草稿示例: // app startup code ConnectionFactory cf = (Connection
我有几个作业,每个作业都有多条消息排队。每个作业的消息随机交错。如果用户决定取消作业,我想从队列中删除属于该作业的所有消息。我已经能够使用 browse() 找到所有要删除的消息,但一直无法弄清楚如何
是否可以将主题配置为仅存储最后一条消息的副本并将其发送到新连接而不知道客户端标识符或其他信息? 更新: 从 Shashi 提供的信息中,我发现这两页使用 retroactive consumer 描述
目前正在使用 WebLogic 和分布式队列。我从文档中了解到,分布式队列允许您使用全局 JNDI 名称检索到集群中任何队列的连接。分布式队列为您提供的主要功能之一似乎是跨多个托管服务器的负载平衡连接
再见,我的基本要求是有一个可以发送消息的路由,并将其放在 JMS 队列中。 camel 上下文在 JavaEE 6 容器中运行,即 JBoss AS 7.1.1,因此它是 HornetQ for JM
我正在阅读 JMS 2.0 规范,其中提到(相关摘录下方)如果客户端尝试修改 Message 对象,则 JMS 提供程序可能会抛出异常。 我的问题是 JMS 提供者如何知道客户端是否试图修改 Mess
我的 spring 上下文文件中有以下设置。 "PowerEventQueue" “${
我正在尝试使用 JSP 连接到 ActiveMQ。但是,当我运行该程序时,它给了我以下类型的异常: NoClassDefFoundError: javax/jms/Destination . 我不确定
我刚看了CORBA和JMS,他们好像都是用来实现的代理架构/模式。 我对他们有几个问题 1.他们之间的区别我还不是很清楚,谁能解释一下? 2.CORBA 是否用于当今的 IT 解决方案?还是正在失去魅
我正在更新现有的 Mule 配置,任务是增强它以根据消息的某些属性将消息路由到不同的端点,因此最好对我手头的两个选项有一些利弊: 在消息上添加属性,使用“message-properties-tran
我有一个订阅 JMS 主题应用程序的 Java 应用程序,该应用程序偶尔会出现以下异常: javax.jms.JMSException: Connection has been terminated
我知道 Camel 的 JMS 组件用于接收消息,使用 Springs DefaultMessageListenerContainer。它可以配置为使用 CLIENT_ACKNOWLEDGE 模式来确
通常不鼓励使用从 JMS 提供者返回的消息 ID 作为相关 ID,将消息发布到队列中。人们如何为请求/响应架构生成相关 ID? 最佳答案 客户端可以使用唯一的 ID 标准,如 UUID生成新的 ID。
我有一个简单的代码可以将 2 条消息放入队列中。 1) 我用两台服务器设置了 connectionNameList。 2) 这两个服务器是独立的,但有相同的队列管理器和定义相同名称的队列,例如“QMg
我是一名优秀的程序员,十分优秀!