这是我第一次使用REST和Spring,在此之前我总是使用JAX RS和Guice实现。
无论如何,这是我的控制器
@Controller
@Api(value = "Redis Data Structures Service", description = "Spring REST Controller that handles all the HTTP requests for the Redis Data Structures Service.", position = 1)
@ManagedResource(description = "Redis Data Structures Service REST Controller")
@RestController
@EnableAutoConfiguration
@EnableSwagger
@RequestMapping("/datastructures/v1")
public class MyResource{
@RequestMapping(value = "/process", method = RequestMethod.POST, produces = "application/json", consumes="application/json")
public ResponseEntity<Operation> batch(@RequestBody Operation operation) throws JSONException{
//this is just for testing
operation.operationId = 123;
return new ResponseEntity<Operation>(operation, HttpStatus.OK);
}
}
这是Pojo of Operation类:
public class Operation{
public int operationId;
//get & set
}
以下是我从Advance Rest Client Google Chrome扩展程序调用的其他电话:
PATH :
<server:port>/datastructures/v1/process/
BODY :
{
"operationId":10
}
METHOD :
POST
CONTENT TYPE :
application/json
在调用时,这是我得到的例外,
Required request body content is missing....with stack trace
这是stacktrace click here
这真是令人沮丧,这个简单的电话是杀死应用程序,:)。 请帮助,提前谢谢
答案 0 :(得分:1)
我删除了您的@Api
和@EnableSwagger
注释,因为我不知道他们来自哪个库。
此代码对我有用:
@Controller
@ManagedResource(description = "Redis Data Structures Service REST Controller")
@RestController
@EnableAutoConfiguration
@RequestMapping("/datastructures/v1")
public class Main {
@RequestMapping(value = "/process", method = RequestMethod.POST, produces = "application/json", consumes="application/json")
public ResponseEntity<Operation> batch(@RequestBody Operation operation) {
//this is just for testing
operation.operationId = 123;
return new ResponseEntity<Operation>(operation, HttpStatus.OK);
}
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
使用HTTPie:
$ http localhost:8080/datastructures/v1/process << EOF
heredoc : {
heredoc : "operationId": 5
heredoc : }
heredoc : EOF
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Date: Mon, 27 Apr 2015 05:55:37 GMT
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
{
"operationId": 123
}
Spring boot 1.2.3.RELEASE
您的控制器上确实有@Controller
和@RestController
,这是不必要的,但不应该是您的问题的原因。检查您是否导入了正确的注释。