0

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?

5 Answers5

0

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);
    }
}



0

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

Brian Akumah
  • 144
  • 3
  • 12
0

Since you are using Spring, and Spring uses Jackson ObjectMapper, you could create another class to your customTypeObject with the respective getter methods to the fields you want to show (probably best to use Lombok's @Getter so you don't need to write getters), if you have a getter method Jackson will automatically create the json for you.

zn43
  • 3
  • 1
  • 4
0

If you use JPA with Hibernate, I recommend to use Hibernate Types for mapping JSON column into POJO, Map, or even JsonNode.

Here is an example explaining how to use it

Artique
  • 86
  • 2
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 06 '22 at 09:33
0

If you want to save a Custom object in the database, then you can create your own Hibernate Convertors

You can also try updating the toString() method of your custom object's class.

From your question, it is unclear where you want the desired representation, is it in a database or in a Field in UI?