Ankur Verma
Ankur Verma

Reputation: 5933

Post in Spring Rest not working

This is the first time I am using REST with Spring, before this I always used JAX RS with Guice implementation.

Anyways here is my Controller

@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);
   }
}

Here is the Pojo of Operation class:

public class Operation{
   public int operationId;
   //get & set
}

Here is the rest call I am calling from Advance Rest Client Google chrome extension:

PATH : 
      <server:port>/datastructures/v1/process/
BODY : 
     {
       "operationId":10
     }
METHOD :
     POST
CONTENT TYPE : 
     application/json 

And on calling this is the exception I am getting,

Required request body content is missing....with stack trace

Here is the stacktrace click here

its really frustrating that this much simple call is killing the app, :) . Please help, thanks in advance

Upvotes: 0

Views: 1724

Answers (1)

Raniz
Raniz

Reputation: 11113

I stripped away your @Api and @EnableSwagger annotations since I don't know which library they're from.

This code is working for me:

@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);
    }

}

Using 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

You do have both @Controller and @RestController on your controller which is unnecessary, but shouldn't be the cause of your issues. Check that you have imported the correct annotations.

Upvotes: 1

Related Questions