Home > database >  How to stringfy projection properties in Java?
How to stringfy projection properties in Java?

Time:12-16

I fill the following projection by joining two tables and it returns only a single record:

public interface EmployeeProjection {

    UUID getEmployeeUuid();

    String getEmployeeName();

    UUID getCompanyUuid();

    String getCompanyName();
}

I want to return this single record in EmployeeProjection type to my QR Code and for this reason I want to convert this data to an array or JSON. So, how can I manage this?

CodePudding user response:

You can use jackson API which I recommend to implement it.

EmployeeProjection obj = new EmployeeProjection();
StringWriter jsonString = new StringWriter();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValue(jsonString, obj);
System.out.println("Employee JSON is: "   jsonString);

This is the output.

{"EmployeeUuid":"1","EmployeeName":"John" ... }

This is the library.

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
  • Related