我想考虑我的 URL 中的格式扩展名,以便它为_format
参数提供最高优先级。我的配置如下:
fos_rest:
param_fetcher_listener: true
view:
view_response_listener: 'force'
formats:
json: true
xml: true
routing_loader:
default_format: json
format_listener:
enabled: true
rules:
- { path: '^/api', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: true }
serializer:
serialize_null: true
body_converter:
enabled: true
我的 HTTP 请求如下:
POST /app_dev_local.php/api/users/admin/globaltoken.json HTTP/1.1
Host: localhost:8000
Cache-Control: no-cache
{
"password": "<a password>"
}
这会产生如下异常:
{"error":{"code":415,"message":"Unsupported Media Type","exception":[{"message":"The format \"txt\" is not supported for deserialization.","class":"Symfony\\Component\\HttpKernel\\Exception\\UnsupportedMediaTypeHttpException","trace":[{"namespace":"","short_class":"","class":"","type":"","function":"","file":"\/Users\/Matteo\/Documents\/belka\/auth\/vendor\/friendsofsymfony\/rest-bundle\/FOS\/RestBundle\/Request\/AbstractRequestBodyParamConverter.php","line":121,"args":[]},{"namespace":"FOS\\RestBundle\\Request","short_class":"AbstractRequestBodyParamConverter","class":"FOS\\RestBundle\\Request\\AbstractRequestBodyParamConverter","type":"->","function":"execute","file":"\/Users\/Matteo\/Documents\/belka\/auth\/vendor\/friendsofsymfony\/rest-bundle\/FOS\/RestBundle\/Request\/RequestBodyParamConverter.php
有趣的部分是:
"Unsupported Media Type","exception":[{"message":"The format \"txt\"
所以我尝试像这样更改我的 HTTP 请求:
POST /app_dev_local.php/api/users/admin/globaltoken.json HTTP/1.1
Host: localhost:8000
Content-Type: application/json
Cache-Control: no-cache
{
"password": "<a password>"
}
它有效!我猜我的扩展完全被忽略了。我的配置有问题吗?是不是因为JMSSerializer
配置错误?这是我的注释:
/**
* @View()
*
* @Route("/users/{username}/globaltoken", requirements={"user"="\w+"})
* @ParamConverter(
* "userBody",
* class="Belka\AuthBundle\Entity\User",
* converter="fos_rest.request_body",
* options={"deserializationContext"={"groups"={"personal"}}}
* )
*/
public function postAction($username, User $userBody)