【发布时间】:2017-02-09 00:01:22
【问题描述】:
我正在做一些 Rest 和一些 SOAP 服务,对于 REST 我正在使用的 FOSRestBundle 和 SOAP 类型我正在使用BeSimple Soap Bundle(这实际上并不相关,因为问题在于FOSRest 侦听器IMO,但我这样说是为了以防万一)。
问题是FOSRest 提供了一个监听器来处理响应并像这样序列化/呈现它们:
<?php
/**
* @Rest\View
*/
public function getAction()
{
return $item;
}
它处理将其序列化为JSON/XML 或其他任何内容的响应。
当我尝试调用我的 SOAP 服务并且听众对我大喊它不支持 SOAP 作为格式(它支持 JSON、XML 等)时,冲突就出现了。
当我将view_response_listener: 'force' 放入我的 FOSRest yml 配置文件时会发生这种情况。如果我将其更改为view_response_listener: 'enabled',SOAP 服务确实可以工作,但我似乎失去了自动处理我的响应的方便的 FOSRest 能力。
我怎样才能让FOSRest view_response_listener 只处理正确的响应,让SOAP Bundle 处理肥皂类型的响应。
编辑:
fos_rest:
param_fetcher_listener: true
body_listener: true
format_listener: true
view:
view_response_listener: 'force' #This is the parameter that is driving me crazy. Value to 'force' Rest controllers work just fine SOAP don't, value to 'enabled' the other way around
formats:
xml: true
json : true
templating_formats:
html: true
force_redirects:
html: true
failed_validation: HTTP_BAD_REQUEST
default_engine: twig
routing_loader:
default_format: json
即FOSRestBundle 配置。
这是我的routing.yml,用于 Rest 端点和 SOAP 端点:
#config/routing.yml
rest_api:
resource: "@RESTBundle/Resources/config/api_routes.yml"
host: "%host_front%" #api.myproject.local
type: rest
#api_routes.yml detail
accounts:
resource: 'RESTBundle\Controller\AccountsController'
type: rest
name_prefix: api_
#config/routing.yml
soap_api:
resource: "@SOAPBundle/Resources/config/soap_routing.yml"
host: "%host_ws%"
prefix: "%contexto_ws%"
#soap_routing.yml detail
_besimple_soap:
resource: "@BeSimpleSoapBundle/Resources/config/routing/webservicecontroller.xml"
soap_ws:
resource: "@SOAPBundle/Controller/"
type: annotation
RestController 示例:
<?php
namespace RESTBundle\Controller\API;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Nelmio\ApiDocBundle\Annotation\ApiDoc as ApiDoc;
use Symfony\Component\HttpFoundation\Request;
/**
* @Rest\RouteResource("accounts", pluralize=false)
*/
class AccountsController extends FOSRestController
{
/**
* @Rest\View
*/
public function getAction($userId)
{
return [
['name' => 'Example', 'pass'=> 'example'],
['name' => 'Example2', 'pass'=> 'example2']
];
}
}
SOAP 控制器:
/**
* @Soap\Header("user", phpType="string")
* @Soap\Header("token", phpType="string")
*/
class AccountsController implements ContainerAwareInterface
{
use ContainerAwareTrait;
/**
* @Soap\Method("getAccounts")
* @Soap\Param("account", phpType="SOAPBundle\DataType\AccountFilter" )
*
* @Soap\Result(phpType="SOAPBundle\DataType\Account[]"")
*/
public function getAccountsAction(Request $request, AccountFilter $filter)
{
return [(new Account())->setName('Example')->setPass('example')] //This is just an example
}
}
编辑错误:
request.ERROR: 未捕获的 PHP 异常 Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException: “不支持格式 'soap',必须实施处理程序”在 /home/teampro/Sites/corinto/vendor/friendsofsymfony/rest-bundle/View/ViewHandler.php 第 292 行 {“异常”:“[对象] (Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException(代码: 0): 不支持格式“soap”,处理程序必须在 /.../vendor/friendsofsymfony/rest-bundle/View/ViewHandler.php:292)"} []
【问题讨论】:
-
请用
config更新您的问题,这很难读懂:) 另外,您能分享一下您的代码吗? -
@Samundra Done,这不是我的代码(不能在这里发布),但它是一个基于它的虚拟示例,所以它大喊相同的东西。
标签: php symfony soap fosrestbundle