How to display class as a JSON response in Java? -
currently i'm working on spring project , want display class json response. following class template , other related details.
public class country {
@column(name = "name") private string name; @id @column(name = "code") private string code; //getters & setters ... }
current response :
[{"name":"andorra","code":"ad"},{"name":"united arab emirates","code":"ae"}]
expected response :
[ { "countries" : [{"name":"andorra","code":"ad"},{"name":"united arab emirates","code":"ae"}], "status" : "ok", "message":"success", etc..etc...}]
instead of status , message, array list too.
you need create class contain list , use responseentity.
public class foo { private list<country> countries; // get/set... } @controller public class mycontroller { @responsebody @requestmapping(value = "/foo", produces = mediatype.application_json_value, method = requestmethod.get) public responseentity<foo> foo() { foo foo = new foo(); country country = new country(); foo.getcountries().add(country); return responseentity.ok(foo); } }
Comments
Post a Comment