基于x-www-form-urlencoded与Spring @Controller的问题的答案
我写了下面的@Controller方法
@RequestMapping(value = "/{email}/authenticate", method = RequestMethod.POST
, produces = {"application/json", "application/xml"}
, consumes = {"application/x-www-form-urlencoded"}
)
public
@ResponseBody
Representation authenticate(@PathVariable("email") String anEmailAddress,
@RequestBody MultiValueMap paramMap)
throws Exception {
if(paramMap == null || paramMap.get("password") == null) {
throw new IllegalArgumentException("Password not provided");
}
}
Run Code Online (Sandbox Code Playgroud)
请求失败并出现以下错误
{
"timestamp": 1447911866786,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported",
"path": "/users/usermail%40gmail.com/authenticate"
}
Run Code Online (Sandbox Code Playgroud)
[PS:泽西岛更加友好,但现在考虑到实际限制,现在无法使用它]
这是我第一次使用Alamofire它,它让我非常沮丧.
我正在使用以下代码在后端API上调用注册API
Alamofire.request(.POST, "\(self.authBaseURL)/signup", parameters: params, headers: headers, encoding: .JSON)
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON { response in
switch response.result {
case .Success(let JSON):
print("Success with JSON: \(JSON)")
success(updatedUser)
case .Failure(let error):
print("Request failed with error: \(error)")
failure(error)
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我在.Failure函数中获得的错误对象不包含服务器端消息.我试图访问其余的对象(请求,响应,数据,结果)我无法在任何地方找到我的错误消息
无论服务器消息说什么,我总是会收到以下错误.请求失败并显示错误:
FAILURE:Error Domain = com.alamofire.error Code = -6003"响应状态代码不可接受:400"UserInfo = {NSLocalizedFailureReason =响应状态代码不可接受:400}
我在做什么有什么不对吗?
Swift 2.2,AlamoFire 3.3.0,Xcode 7.3
有没有办法304 Not Modified用 Alamofire 4检测响应?我发现response.statusCode即使服务器以 304 响应,Alamofire也始终为 200。
网络通话设置:
Alamofire
.request("http://domain.com/api/path", method: .get)
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON { response in
print(response.response?.statusCode)
}
Run Code Online (Sandbox Code Playgroud)
Alamofire 响应头
<NSHTTPURLResponse: 0x61800003e1c0> { URL: http://domain.com/api/path } { status code: 200, headers {
"Access-Control-Allow-Headers" = "content-type, authorization";
"Access-Control-Allow-Methods" = "GET, PUT, POST, DELETE, HEAD, OPTIONS";
"Access-Control-Allow-Origin" = "*";
"Cache-Control" = "private, must-revalidate";
Connection = "keep-alive";
"Content-Type" = "application/json";
Date = "Mon, 23 Jan 2017 23:35:00 GMT";
Etag = "\"f641...cbb6\"";
"Proxy-Connection" = "Keep-alive"; …Run Code Online (Sandbox Code Playgroud)