Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning JSON as Response Spring Boot

I am trying to get rest response as json instead I am getting as string.

Controller

@RestController
@RequestMapping("/api/")
public class someController{

  @Autowired
  private SomeService someService;

  @GetMapping("/getsome")
  public Iterable<SomeModel> getData(){
    return someService.getData();
  }
}

Service

@Autowired
private SomeRepo someRepo;

public Iterable<someModel> getData(){
  return someRepo.findAll();
}

Repository

public interface SomeRepo extends CrudRepository<SomeModel,Integer>{

}

Models

@Entity
@Table(name="some_table")
public class SomeModel{

  @Id
  @Column(name="p_col", nullable=false)
  private Integer id;
  @Column(name="s_col")
  private String name
  @Column(name="t_col")
  private String json;   // this column contains json data

  //constructors, getters and setters
}

when I run localhost:8080/api/getsome I am getting:

[
 {
    "p_col":1,
    "s_col":"someName",
    "t_col":" 
{\r\n\t"school_name\":\"someSchool\",\t\r\n\t"grade\":"A\",\r\n\t\"class\": 
 [{\"course\":"abc",\t"course_name\":\"def" }]}"
  }
]

Field t_col is returning string instead of json. How do I get json objects in response?

As for the database, the three columns are int, varchar and varchar.

Any help would be appreciated. Thanks !!

like image 571
404or505 Avatar asked Mar 06 '19 02:03

404or505


1 Answers

You need to define your json attribute as JsonNode so jackson can read it back and forrward, but mark is as @Transient so JPA does not try to store it on database.

Then you can code getter/setter for JPA, where you translate from JsonNode to String back and forward. You define a getter getJsonString that translate JsonNode json to String. That one can be mapped to a table column, like 'json_string', then you define a setter where you receive the String from JPA and parse it to JsonNode that will be avaialable for jackson, jackson then will translate it to a json object not a string as you mention.

@Entity
@Table(name = "model")
public class SomeModel {

  private Long id;
  private String col1;

  //  Attribute for Jackson 
  @Transient
  private JsonNode json;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  public Long getId() {
    return id;
  }

  @Column(name ="col1")
  public String getCol1() {
    return col1;
  }

  // Getter and setter for name

  @Transient
  public JsonNode getJson() {
    return json;
  }

  public void setJson(JsonNode json) {
    this.json = json;
  }

  // Getter and Setter for JPA use
  @Column(name ="jsonString")
  public String getJsonString() {
    return this.json.toString();
  }

  public void setJsonString(String jsonString) {
    // parse from String to JsonNode object
    ObjectMapper mapper = new ObjectMapper();
    try {
      this.json = mapper.readTree(jsonString);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Notice, @Column are defined at gettters because we need to indicate JPA to use getJsonString and JPA requires consistency so all column's getters must be mark with @Columns.

like image 169
Cristian Colorado Avatar answered Sep 28 '22 02:09

Cristian Colorado