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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -