Reputation: 2134
I have some problem with sending request to spring mvc controller. I have got entity:
public class Person {
private String name;
private int age;
private String city;
//..getters setters
}
and SpringMvc controller:
@Controller
@RequestMapping("/companies")
public class FirmaController {
@RequestMapping(value = "/addPerson", method = RequestMethod.POST, headers = {"Content-type=application/json"})
public String addPerson(@RequestBody Person person) {
return "person";
}
}
When i would like to send request to server with curl:
curl -i -X POST -HContent-Type:application/json -HAccept:application/json http://localhost:8080/api/companies/addPerson -d "{ 'name': 'Gerry', 'age': 20, 'city': 'Sydney' }"
i have got a HTTP/1.1 400 Bad Request:
HTTP/1.1 400 Bad Request
Content-Type: text/html;charset=ISO-8859-1
Cache-Control: must-revalidate,no-cache,no-store
Content-Length: 1392
Server: Jetty(8.1.10.v20130312)
What I do wrong?
Upvotes: 2
Views: 8504
Reputation: 2134
This one is correct:
curl -i -X POST -HContent-Type:application/json
-HAccept:application/json
http://localhost:8080/api/companies/addPerson
-d '{ "name": "Gerry", "age": 20, "city": "Sydney" }'
Upvotes: 3
Reputation: 26828
The data you send is not valid JSON. Strings have to be wrapped in double quotes "
not single quotes '
like in your example.
If you don't have an old version of Spring you should use consumes = "application/json"
instead of headers=...
.
Upvotes: 5