【发布时间】:2019-05-29 13:02:41
【问题描述】:
我正在使用 PostgreSQL 编写带有 Spring-Boot 的服务器 我正在尝试获取有关链接到特定实体的图像的信息。 我正在尝试将用户信息从服务器获取到我的前端 Angular 应用程序。 在我的系统中,用户有图像链接到他的帐户,所以我做了 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;
}
}
然后我将图像列表链接到用户帐户类
@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);
}
}
然后我创建端点以通过 id 获取用户
@GetMapping("users/{id}")
public Optional<User> getUserById(@PathVariable Long id) {
return service.getUserById(id);
}
服务方法很简单
@Transactional
public Optional<User> getUserById(Long id) {
return repository.findById(id);
}
我通过另一个端点添加了一些图像工作正常,因为我能够在我的前端获取图像。
问题是当我想从服务器获取用户信息作为 JSON 时(我在 @Lob 字段上写了@JsonIgnore,因为我只想获得图像信息而不是实际图像)我收到此错误
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"])
我阅读了一些类似的文章,并尝试在 Image @Lob 图像的 getter 上提供 @JsonIgnore 我将 @Transactional 添加到服务方法检索元素但它不起作用。
我只是想从服务器实现那种消息:
{
id: "1"
firstName: "test",
lstName: "test_ln",
images: {
{
"id": 10,
"name": "IMG12.jpg",
"type": "image/jpeg"
},
{
"id": 20,
"name": "IMG456.jpg",
"type": "image/jpeg"
}
}
}
【问题讨论】:
标签: java json postgresql blob