I'm writing a server with Spring-Boot using PostgreSQL I'm trying to get information about images that are linked to a specific entity. I'm trying to get User information from the server to my front-end Angular app. In my system user have images linked to his account so i did class ImageEntity
@Entity @Table(name = "image") @Data
public class ImageEntity {
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String name;
private String type;
@Lob
private byte[] image;
@JsonIgnore
public byte[] getImage() {
return image;
}
}
Then i linked the list of images to user account class
@Entity @Data
public class UserAccount{
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String firstName;
private String lastName
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "user_images",
joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "image_id", referencedColumnName = "id")}
)
private List<ImageEntity> images;
public void addImage(ImageEntity image) {
images.add(image);
}
}
Then i create endpoint to get user by id
@GetMapping("users/{id}")
public Optional<User> getUserById(@PathVariable Long id) {
return service.getUserById(id);
}
service method is very simple
@Transactional
public Optional<User> getUserById(Long id) {
return repository.findById(id);
}
I added some images through another endpoint works fine because i'm able to get image in my front-end.
Problem is when i want to get User info as a JSON from server( and i write @JsonIgnore on @Lob field because i only want to have info of image not the actual image) i get this error
Resolved exception caused by handler execution: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Unable to access lob stream; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unable to access lob stream (through reference chain: com.app.model.user.User["images"])
I read some similar articles and i try to give @JsonIgnore on getter of the Image @Lob image i added @Transactional to service method retrieving elements but it's not working.
I simply want to achieve that kind of message from server:
{
id: "1"
firstName: "test",
lstName: "test_ln",
images: {
{
"id": 10,
"name": "IMG12.jpg",
"type": "image/jpeg"
},
{
"id": 20,
"name": "IMG456.jpg",
"type": "image/jpeg"
}
}
}