How to use @produces in JSON object spring boot -


is possible use @produces json objects in spring boot? or there way implement this:

jsonobject j_session = new jsonobject(); j_session.put("session_id_j", session_jid); j_session.put("j_app", "j"); j_session.put("rest_id_j", rest_id); 

here simple example:

restcontroller class:   import java.util.arraylist; import java.util.list;  import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.restcontroller;  import com.websystique.springboot.model.user;  @restcontroller @requestmapping("/api") public class restapicontroller {      @requestmapping(value = "/user/", method = requestmethod.get, produces = { "application/json" })     public list<user> listallusers() {         list<user> users = new arraylist<user>();         users.add(new user(1, "sam", 30, 70000));         users.add(new user(2, "tom", 40, 50000));         users.add(new user(3, "jerome", 45, 30000));         users.add(new user(4, "silvia", 50, 40000));         return users;     } } 

the attribute produces = { "application/json" } automatically converts list collection json response.

below pojo class.

user pojo class   public class user {      private long id;      private string name;      private int age;      private double salary;      public user(){     }      public user(long id, string name, int age, double salary){         this.id = id;         this.name = name;         this.age = age;         this.salary = salary;     } } 

sample json response:

[    {       "id":1,       "name":"sam",       "age":30,       "salary":70000    },    {       "id":2,       "name":"tom",       "age":40,       "salary":50000    },    {       "id":3,       "name":"jerome",       "age":45,       "salary":30000    },    {       "id":4,       "name":"silvia",       "age":50,       "salary":40000    } ] 

follow link complete detailed example crud operations.

the above code link itself, modified controller part make simple.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -