0
votes

I'm implementing spring boot rest api application, in which I invoke stored procedure (which returns back refcursor). I need to show that response as JSON object with key, value pair without creating any DTO's or entity for that response. Is it possible to achieve the above requirement?

I get response(Cursor) from stored procedure.

@Override

public List getOrder(String orderNo) {
StoredProcedureQuery query = entityManager.createStoredProcedureQuery("xx.xxx.get_order_details"); query.registerStoredProcedureParameter(1, String.class, ParameterMode.IN); query.registerStoredProcedureParameter(2, void.class, ParameterMode.REF_CURSOR); query.setParameter(1, "OR-1001");
List results = query.getResultList();.

1
Could anyone knows on how to convert procedure response(may receive multiple cursor's as a response) to Map or to JSONNode ????Raj

1 Answers

1
votes

Yes, you can either return Map<String, Object> from your controller class or use the generic JsonNode class from Jackson.

An example of the Map might be the following (with the help of Java 11):

@RestController
@RequestMapping("/public")
public class PublicController {

  @GetMapping("/data")
  public Map<String, Object> returnFoo() {
    Map<String, Object> resultFromDatabase = Map.of("name", "duke", "data",
      Map.of("id", 1337, "enabled", true));
    return resultFromDatabase;
  }
}

The result is the following

curl -v localhost:8080/public/data

{"data":{"id":1337,"enabled":true},"name":"duke"}

It is also possible to just return Object, but then you have to make sure the result of your database is always parsable to JSON, as you otherwise might get exceptions:

@GetMapping("/data")
public Object returnFoo() {
  Map<String, Object> resultFromDatabase = Map.of("name", "duke", "data",
    Map.of("id", 1337, "enabled", true));
  return resultFromDatabase;
}