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!
Try with following, hope you might resolve the issue:
Since you are using
@RestController
annotation, so no need to use@ResponseBody
annotation again, which is redundant.If you are using spring boot, then make sure you have added the below dependency .
If the project is not spring boot, then add dependency for jackson :
com.fasterxml.jackson.databind.ObjectMapper
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.
@RequestMapping(value = "/hedgesim/", method = RequestMethod.POST)
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.
If http message converter configured properly, maybe request was improperly formed.
You can do the following checks.
MappingJackson2HttpMessageConverter
is registered or not. By default, Spring registers this converter if you have the jar in the classpath.@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.