i new spring boot
.
i trying write code uses post request input.
i getting error when try send post request through post rest client chrome plugin.
i using maven build tool.
{ "timestamp": 1431079188726, "status": 415, "error": "unsupported media type", "exception": "org.springframework.web.httpmediatypenotsupportedexception", "message": "content type 'text/plain;charset=utf-8' not supported", "path": "/api/greetings" }
these classes.
application.java
@springbootapplication @componentscan(basepackages="webapi") public class application { public static void main(string[] args) throws exception{ springapplication.run(application.class, args); } }
greeting.java
public class greeting { private biginteger id; private string text; public greeting() { } public biginteger getid() { return id; } public void setid(biginteger id) { this.id = id; } public string gettext() { return text; } public void settext(string text) { this.text = text; } }
greetingcontroller.java
@restcontroller public class greetingcontroller { private static biginteger nextid; private static map<biginteger, greeting> greetingmap; private static greeting save(greeting greeting) { if (greetingmap == null) { greetingmap = new hashmap<biginteger, greeting>(); nextid = biginteger.one; } greeting.setid(nextid); nextid = nextid.add(biginteger.one); greetingmap.put(greeting.getid(), greeting); return greeting; } static { greeting g1 = new greeting(); g1.settext("hello world"); save(g1); greeting g2 = new greeting(); g2.settext("hola mundo!"); save(g2); } @requestmapping(value = "/api/greetings", method = requestmethod.get, produces = mediatype.application_json_value) public responseentity<collection<greeting>> getgreetings() { collection<greeting> greetings = greetingmap.values(); return new responseentity<collection<greeting>>(greetings, httpstatus.ok); } @requestmapping(value = "/api/greetings/{id}", method = requestmethod.get, produces = mediatype.application_json_value) public responseentity<greeting> getgreeting( @pathvariable("id") biginteger id) { greeting greeting = greetingmap.get(id); if (greeting == null) { return new responseentity<greeting>(httpstatus.not_found); } return new responseentity<greeting>(greeting, httpstatus.ok); } @requestmapping(value = "/api/greetings", method = requestmethod.post, consumes = mediatype.application_json_value, produces = mediatype.application_json_value) @responsebody public responseentity<greeting> creategreeting( @requestbody greeting greeting) { greeting savedgreeting = save(greeting); return new responseentity<greeting>(savedgreeting, httpstatus.created); } }
the service expecting application/json
content-type
receiving text/plain
input.
what using make the post? if testing postman sure add content-type
application/json
value in headers section.
Comments
Post a Comment