我有一个非常简单的场景,我正在努力.我正在使用Alamofire在rest API上注册用户.第一次注册调用成功,用户可以登录.第二次调用,当尝试使用相同的电子邮件地址注册时,应该从服务器生成HTTP状态代码409.但是,Alamofire会返回一个带有空请求和响应的.Success.我用postman测试了这个API,它正确返回409.
为什么Alamofire没有返回.Failure(错误),错误有状态代码信息等?
这是我每次使用相同输入运行的调用.
Alamofire.request(.POST, "http://localhost:8883/api/0.1/parent", parameters: registrationModel.getParentCandidateDictionary(), encoding: .JSON).response(completionHandler: { (req, res, d, e) -> Void in
print(req, res, d, e)
})
Run Code Online (Sandbox Code Playgroud) 是新来春目前正在试图做的HTTP POST请求的应用程序/ x-WWW的形式,URL编码,但是当我守这在我的头,然后弹簧不能识别它,并说415 Unsupported Media Type
了x-www-form-urlencoded
org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型'application/x-www-form-urlencoded'
任何人都知道如何解决它吗?请评论我.
我的控制器的一个例子是:
@RequestMapping(
value = "/patientdetails",
method = RequestMethod.POST,
headers="Accept=application/x-www-form-urlencoded")
public @ResponseBody List<PatientProfileDto> getPatientDetails(
@RequestBody PatientProfileDto name
) {
List<PatientProfileDto> list = new ArrayList<PatientProfileDto>();
list = service.getPatient(name);
return list;
}
Run Code Online (Sandbox Code Playgroud) 我检查了几种不同的方式,还下载了一个新项目,看看有什么检查bug,但我仍然不知道答案.
那是我的RestController
@RestController
@RequestMapping(value = "/message")
public class MessageController {
@RequestMapping(value = "/", method = RequestMethod.POST)
public void createMessage(@RequestBody Message message){
System.out.println(message);
}
}
Run Code Online (Sandbox Code Playgroud)
那是我的模特
@Data
@Entity
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String sender;
private String telephone;
private String message;
}
Run Code Online (Sandbox Code Playgroud)
必要时Gradle依赖项
dependencies {
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.0.pr3'
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('com.h2database:h2')
runtime('org.postgresql:postgresql')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Run Code Online (Sandbox Code Playgroud)
在邮递员我得到了那个错误
{"timestamp":1495992553884,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException",
"message":"Content type'application/x-www -form-urlencoded; charset = UTF-8'不支持",
"path":"/ message /"}
这是最简单的休息方式,但我犯了错误?
我有一个Spring休息端点做一个简单的hello应用程序.它应该接受{"name":"something"}并返回"Hello,something".
我的控制器是:
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
@RequestMapping(value="/greeting", method=RequestMethod.POST)
public String greeting(Person person) {
return String.format(template, person.getName());
}
}
Run Code Online (Sandbox Code Playgroud)
人:
public class Person {
private String name;
public Person() {
this.name = "World";
}
public Person(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
当我向服务提出请求时
curl -X POST -d '{"name": "something"}' http://localhost:8081/testapp/greeting
Run Code Online (Sandbox Code Playgroud)
我明白了
Hello, World!
Run Code Online (Sandbox Code Playgroud)
看起来它没有正确地将json反序列化为Person对象.它使用默认构造函数,然后不设置名称.我发现了这个:如何在REST中创建POST请求以接受JSON输入? …
我正在尝试编写一个接收 application/x-www-form-urlencoded 的休息端点。但端点不接受@RequestBody或@RequestParam的请求参数
我尝试使用 MultiValueMap 来获取请求参数。但我总是得到 0 个参数。有没有办法获取 MultiValueMap 或其他 POJO 类的请求值。
AD=&value=sometestvalue- 这是 application/x-www-form-urlencoded 请求主体。我正在尝试使用邮递员来完成请求
@RequestMapping(value = "/test/verification/pay/{id}", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
@ResponseBody
public Response testVerificationPay(@PathVariable("id") long id, @RequestParam MultiValueMap formData,
HttpServletRequest servletRequest, ServiceContext serviceContext){
log.info("!--REQUEST START--!"+formData.toString());
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试处理带有纯文本(utf-8)正文的 POST 请求,但 spring 似乎不喜欢调用的纯文本性质。可能是它不受支持 - 或者是我编码错误?
@RestController
@RequestMapping(path = "/abc", method = RequestMethod.POST)
public class NlpController {
@PostMapping(path= "/def", consumes = "text/plain; charset: utf-8", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> doSomething(@RequestBody String bodyText)
{
...
return ResponseEntity.ok().body(responseObject);
}
}
Run Code Online (Sandbox Code Playgroud)
回应是:
已解决 [org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型“application/x-www-form-urlencoded”]
我用curl命令测试:
curl -s -X POST -H 'Content-Type: text/plain; charset: utf-8' --data-binary @text.txt localhost:8080/abc/def
Run Code Online (Sandbox Code Playgroud)
text.txt 包含纯文本(希伯来语中的 UTF-8)。
以下Spring MVC代码抛出MissingServletRequestParameterException,
@Controller
@RequestMapping("/users")
public class XXXXResource extends AbstractResource {
.....
@RequestMapping(method = RequestMethod.PUT
, produces = {"application/json", "application/xml"}
, consumes = {"application/x-www-form-urlencoded"}
)
public
@ResponseBody
Representation createXXXX(@NotNull @RequestParam("paramA") String paramA,
@NotNull @RequestParam("paramB") String paramB,
@NotNull @RequestParam("paramC") String paramC,
@NotNull @RequestParam("paramD") String paramD ) throws Exception {
...
}
}
Run Code Online (Sandbox Code Playgroud)
日志中没有堆栈跟踪,只有来自Postman的请求返回HTTP 400错误.
java ×5
spring ×5
rest ×3
spring-mvc ×3
spring-boot ×2
alamofire ×1
ios ×1
jquery ×1
json ×1
swift ×1