How to use @produces in JSON object spring boot

Is it possible to use @produces for JSON objects in spring boot? Or is there another way to 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);

Upvotes: 4

Views: 47129

Answers (2)

Learner
Learner

Reputation: 21435

Here is a 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 to json response.

Below is the 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 this link for a complete detailed example with CRUD operations.

The above code is from this link itself, I just modified the controller part to make it simple.

Upvotes: 6

Andres Navarro
Andres Navarro

Reputation: 137

there are a few errors , dont use JSONObject , only if is necesary (try to use Gson library), for convention the variable J_Session the first letter must be in lower case like jSession , if your class have the annotation @RestController you dont need to use @ResponseBody in the method only if the class is marked like @Controller; there is my example for a class with @Controller annotation instead @RestController annotation, with this your JSONObject is not longer necesary , another thing , try lo read about of differences between pojos and beans , and always try to nulify yours variables after use them (sorry about my english)

@RequestMapping(value = "/postMethod", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<String> insertSomething(@RequestBody String body){
    ArrayList<String> listOfStrings=null;
    if (body != null){
        listOfStrings= new ArrayList<String>();
        listOfStrings.add (body);
        listOfStrings.add("somethin else");//Json Object because the "produces"
    }else{
        listOfStrings = new ArrayList<String>();// empty Json object to avoid null exception in client side (in my case Angular 6)
    }

    return listOfStrings;
}

Upvotes: 3

Related Questions