Home > Blockchain >  Handling SQL Custom Type as JSON Response using Hibernate JPA Spring Web
Handling SQL Custom Type as JSON Response using Hibernate JPA Spring Web

Time:11-30

I have an entity class with a custom type field among other fields

@Entity
@Table(name = "something") 
public class Test {
    @Id
    private Integer id;

    private <SomethingHere> customTypeObject;

    // Other fields, getters, and setters not shown
}

Using that class I am generating a json representation of the data using repository.findAll()

@Controller 
public class TestController {
    @AutoWired
    private TestRepository repo

    @GetMapping("/test")
    private List<Test> test() {
        return repo.findAll();
    }
}

The JSON response to the user is intended to display the fields within the custom type in JSON format. If I label the customTypeObject as a String, the reponse is something like below [{id: 1, customTypeObject: "(1,2)"}] Whereas I would prefer a response like [{id: 1, customTypeObject: { A: 1, B: 2 }}]

I realize I could do this by creating another entity class for the custom type where I manually type out the fields but I plan on increasing the number of fields in the custom type frequently during the development process so I would prefer if the program would keep track of the fields for me.

Is there any way this could be accomplished?

CodePudding user response:

If I label the customTypeObject as a String, the reponse is something like below [{id: 1, customTypeObject: "(1,2)"}]

based on your example, I assume you are storing a JSON string. You can create a DTO class and use mapstruct to define a mapper from your entity to DTO. You can you fasterxml to parse the string to object

// entity class
@Entity
@Table(name = "something") 
public class Test {
    @Id
    private Integer id;

    private String customTypeObject;

    // Other fields, getters, and setters not shown
}

// DTO class
public class TestDTO {

   // same fields as Entity 

   private Map<String,Object> customTypeObject;
   // or private Object customTypeObject;

}

// mapper class
@Mapper
public interface TestMapper {

   TestMapper INSTANCE =  Mappers.getMapper(TestMapper.class);
   ObjectMapper objectMapper = new ObjectMapper();

   @Mappings({
       @Mapping(source = "test.customTypeObject", target = "customTypeObject", qualifiedByName = "parseCustomObject")
    })
    TestDTO entityToDto(Test test)
    
    List<TestDTO> entitiesToDto(List<Test> tests) 

    // the return type of this method must be the same as the datatype for the field in the dto class
    @Named("parseCustomObject")
    static Map<String,Object> getCustomObject(String objStr) throws JsonProcessingException {
      Map<String, Object> objectMap = new HashMap<>();
      if (Objects.nonNull(objStr)) {
        objectMap = objectMapper.readValue(objStr, Map.class);
      }
      return objectMap;
    }

}

// controller or service class
@Controller 
public class TestController {
    @AutoWired
    private TestRepository repo

    @GetMapping("/test")
    private List<TestDTO> test() {
        List<Test> t = repo.findAll();
        return TestMapper.INSTANCE.entitiesToDto(t);
    }
}



CodePudding user response:

You can also use either GSON (com.google.code.gson) or JSONObject (org.json.JSONObject) to create the JSON body manually this way you can manipulate it as needed here is a link to an article that should be helpful

  • Related