我想考虑网址中的格式扩展名,以便_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)
答案 0 :(得分:0)
对于请求,Content-Type
标头定义请求正文的媒体类型。您POST到的URL无效,请求正文的默认媒体类型为text/plain
,因此这种行为是正确的。
PUT /foo.json
Content-Type: application/x-www-form-urlencoded
this=that
200 OK
然后:
GET /foo.json
200 OK
Content-Type: text/json; charset=utf-8
{"this":"that"}
答案 1 :(得分:0)
对我来说问题是我发送了表格数据序列化,我用数组解决了:
var dataj = {
name: (tjq('input[name="appbundle_contact[name]"]').val()),
description: (tjq('input[name="appbundle_contact[description]"]').val()),
email: (tjq('input[name="appbundle_contact[email]"]').val()),
country: (tjq('input[name="appbundle_contact[country]"]').val()),
phone: (tjq('input[name="appbundle_contact[phone]"]').val()),
};
tjq.ajax({
type : tjq('#contactForm').attr( 'method' ),
url : tjq('#contactForm').attr( 'action' ),
jsonp: "response",
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(dataj),
success : function(data, status, object) {
tjq('.alert-success').show();
tjq('#contactForm').hide();
},
error: function(data, status, object){
console.log(data.message);
tjq('.alert-error').show();
tjq('#contactForm').hide();
}
});
希望这适合你;)