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

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -