我使用FosRestBundle,它使用jms序列化程序(看起来像默认值),并且无法覆盖jms序列化程序包中的默认处理程序。如何停止默认处理程序覆盖我的自定义处理程序?
我尝试使用优先级:-1000和优先级:1000仍未使用。我在getSubscribingMethods
中设置了断点,什么都没有,调试未输入:(
我使用了新版本的捆绑包
"name": "jms/serializer-bundle",
"version": "3.5.0",
"name": "symfony/framework-bundle",
"version": "v5.0.5",
我升级到"name": "jms/serializer-bundle", "version": "dev-master",
的版本仍然遇到相同的情况
App\Serializer\ConstraintViolationListHandler:
tags:
- {name: jms_serializer.subscribing_handler, priority: 1000}
class ConstraintViolationListHandler implements SubscribingHandlerInterface
{
public static function getSubscribingMethods()
{
return [
[
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
'format' => 'json',
'type' => ConstraintViolationList::class,
'method' => 'serializeConstraintViolationList',
]
];
}
public function serializeConstraintViolationList(
JsonSerializationVisitor $visitor,
$constraintViolationList,
array $type,
Context $context = null
)
{
$t = 1;
return 1;
}
}
但是调试程序永远不会输入我的ConstraintViolationListHandler
我来自休息控制器的动作
* @Rest\View(statusCode=Response::HTTP_OK)
*/
public function getCategoriesAction(SentEmail $sentEmail, ConstraintViolationListInterface $validationErrors)
{
$content = 1;
if (count($validationErrors) > 0) {
// Handle validation errors
return $validationErrors;
和我的休息配置
fos_rest:
body_listener:
service: my_body_listener
body_converter:
enabled: true
validate: true
validation_errors_argument: validationErrors
unauthorized_challenge: "Basic realm=\"Restricted Area\""
access_denied_listener:
# all requests using the 'json' format will return a 403 on an access denied violation
json: true
param_fetcher_listener: true
format_listener:
rules:
- { path: ^/api, prefer_extension: true, fallback_format: json, priorities: [ json ] }
- { path: ^/, priorities: [ json, xml, html ], fallback_format: ~, prefer_extension: true }
view:
view_response_listener: 'force'
formats:
json: true
jsonp: false
xml: false
rss: false
mime_types:
json: ['application/json', 'application/x-json']
routing_loader:
default_format: json
exception:
enabled: true
codes:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
'Symfony\Component\HttpKernel\Exception\BadRequestHttpException': 400
messages:
Symfony\Component\HttpKernel\Exception\BadRequestHttpException: true
Symfony\Component\HttpKernel\Exception\HttpException: true
现在ConstraintViolationList
的响应看起来像这样
[
{
"property_path": "email",
"message": "This value is not a valid email address."
}
]
但是我需要自定义它。为此需要做什么?
对于版本
"name": "jms/serializer-bundle",
"version": "3.5.0",
"name": "symfony/framework-bundle",
"version": "v5.0.5",
决定应该是这样,'priority' => -915
应该在getSubscribingMethods
中
/**
* @return array
*/
public static function getSubscribingMethods()
{
return [
[
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
'format' => 'json',
'type' => ConstraintViolationList::class,
'method' => 'serializeCustomListToJson',
'priority' => -915
]
];
}
然后,\JMS\SerializerBundle\DependencyInjection\Compiler\CustomHandlersPass::sortAndFlattenHandlersList
将其移至最后一个位置,并在处理程序中设置自定义服务,而不是在jms默认处理程序中进行设置