1

I am implementing a Spring Boot Data JPA based RESTful service using normalized data structures stored in a database. I need to find out the best/recommended implementation of generating the external DTOs following a nested JSON structures which will be shared to the javascript front end.

Therefore, I am seeking your advise in order to generate the desired output using Spring Boot Data JPA.

Database Table Structure

lookup_name: varchar,
lookup_code: varchar,
lookup_value:varchar

lookup_name     | lookup_code   | lookup_value
---------------------------------------------------
parent_type_1   | child_code_1  | child_value_1
parent_type_1   | child_code_2  | child_value_2
parent_type_1   | child_code_3  | child_value_3
parent_type_2   | child_code_4  | child_value_4
parent_type_2   | child_code_5  | child_value_5
parent_type_3   | child_code_6  | child_value_6
parent_type_3   | child_code_7  | child_value_7

Required External DTO Structure

    [
  {
    "lookup_name": "parent_type_1",
    "child": [
      {
        "lookup_code": "child_code_1",
        "lookup_value": "child_value_1"
      },
      {
        "lookup_code": "child_code_2",
        "lookup_value": "child_value_2"
      },
      {
        "lookup_code": "child_code_3",
        "lookup_value": "child_value_3"
      }
    ]
  },
  {
    "lookup_name": "parent_type_2",
    "child": [
      {
        "lookup_code": "child_code_4",
        "lookup_value": "child_value_4"
      },
      {
        "lookup_code": "child_code_5",
        "lookup_value": "child_value_5"
      }
    ]
  },
  {
    "lookup_name": "parent_type_3",
    "child": [
      {
        "lookup_code": "child_code_6",
        "lookup_value": "child_value_6"
      },
      {
        "lookup_code": "child_code_7",
        "lookup_value": "child_value_7"
      }
    ]
  }
]

Thanks.

Aviro
  • 2,025
  • 19
  • 28
  • What about a query which will return a List of DTOs – YCF_L Sep 03 '19 at 05:31
  • The query will return the flat structure, I rather wanted the nested JSON structure to communicate with the front end. – Aviro Sep 03 '19 at 05:35
  • 1
    Yes if you are using Spring boot then you can just annotate your controller with the data you want to you will get the type you want – YCF_L Sep 03 '19 at 05:37
  • 1
    Check this https://stackoverflow.com/questions/44839753/returning-json-object-as-response-in-spring-boot – YCF_L Sep 03 '19 at 05:37
  • 1
    @Aviro a query cannot return a data in this format. Probably you will need to transform the result from your query to your desired format in your Java code. – Mushif Ali Nawaz Sep 03 '19 at 05:40
  • 1
    @Aviro JSON format doesn't look like a valid one. – Mushif Ali Nawaz Sep 03 '19 at 05:46
  • 1
    @Aviro probably you want something like that: [Corrected JSON response](https://jsoneditoronline.org/?id=6af28b9ed8844c3f9dd6c6d11e7778d3) – Mushif Ali Nawaz Sep 03 '19 at 05:52
  • Thanks @YCF_L and @Mushif; I think I should be able to tacke this now with the given hints. I will keep posted if I need more details. – Aviro Sep 03 '19 at 05:56

0 Answers0