0

I need help creating a POST request to my Java Spring Restcontroller.

This is my controller -

@RestController
@RequestMapping("hedgesimulator/")
public class HedgeSimulatorController {

    @RequestMapping(value = "hedgesim/", method = RequestMethod.POST)
    @ResponseBody
    public HedgeSimulatorLog putHedgeSimulation(
        @RequestBody HedgeSimulatorLog hedgeSimulatorLog) {

        System.out.println(hedgeSimulatorLog.toJsonString());
        return hedgeSimulatorLog;
    }
}

I am using Chrome's "Advanced Rest Client" Plugin to POST my request to my URL (I am sure my localhost is running properly, etc.)

What do I need to add to my header?

I receive an error for "HTTP 400 - Status report: The request sent by the client was syntactically incorrect"

Please help!

mikepetretta
  • 1
  • 1
  • 1
  • You might want to set the spring MVC logging levels down to DEBUG and also check your server access logs for more information about the request that is being made. Sounds like the request is wrong - maybe also try doing a request using curl or some other command line tool. Lastly, `@ResponseBody` is redundant in a `@RestController`, I think. – cjstehno May 18 '15 at 18:01
  • I think tha request payload can't be converted into HedgeSimulatorLog bean. Can you post the HedgeSimulatorLog bean and sample JSON payload? – K. Siva Prasad Reddy May 19 '15 at 05:53

4 Answers4

0

To pass an object to controller you must configure HttpMessageConverter which helds serialization and deserealization of this object. For example, if you want to pass an object to controller as JSON, set a MappingJackson2HttpMessageConverter as parameter in your mvc declaration in spring config file.

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
    </mvc:message-converters>
</mvc:annotation-driven>

If http message converter configured properly, maybe request was improperly formed.

Lea32
  • 96
  • 2
0

@RequestMapping(value = "/hedgesim/", method = RequestMethod.POST)

Pankaj
  • 114
  • 3
0

Try with following, hope you might resolve the issue:

  1. Since you are using @RestController annotation, so no need to use @ResponseBody annotation again, which is redundant.

  2. If you are using spring boot, then make sure you have added the below dependency .

     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
  3. If the project is not spring boot, then add dependency for jackson : com.fasterxml.jackson.databind.ObjectMapper

  4. Just to make sure the request body is correct, what you can do is, execute request method first, get the JSON response, pass the same JSON for POST, so this might avoid some typo/human error while creating JSON data.

Hope, it helps.

Sampada
  • 2,931
  • 7
  • 27
  • 39
Rakesh
  • 1,374
  • 1
  • 16
  • 24
0

You can do the following checks.

  1. Validate the request body you are sending through some online tool like JSonLint.
  2. Check whether MappingJackson2HttpMessageConverter is registered or not. By default, Spring registers this converter if you have the jar in the classpath.
  3. No need to use @ResponseBody if you are using @RestController. So remove it.

For a complete example on creating and consuming REST Service using Spring 4.0, you can visit Techno Spots.

Sampada
  • 2,931
  • 7
  • 27
  • 39
RLD
  • 1,867
  • 3
  • 15
  • 20