我正在使用 React.js 和 Springboot 構建一個全堆疊應用程式,該應用程式從用戶那里獲取影像并允許他們為該影像命名并將其作為用戶帖子發布,例如:
用戶影像:myImage.jpeg 和標題:這是 myImage 的標題
我已經成功創建了一個 API,允許將其存盤在我的資料庫中,如下面的 UserPost 表所示:

我正在使用 AWS S3 存盤桶來存盤影像檔案。在同一個應用程式中,每個用戶都有一個組態檔,其中包含一個用戶組態檔影像和一個用于他們的組態檔卡的橫幅影像。我創建了一個允許此功能的 API,但是,目前,我需要通過連接到我的 S3 存盤桶的特定 URL 鏈接加載每個用戶的特定個人資料影像和橫幅。以下是演示用戶個人資料的 GET 請求示例。

如您所見,組態檔影像和橫幅的檔案名都被存盤,然后分別添加到profile_img_complete 和 profile_banner_complete 中每個重建的 URL 鏈接的末尾,并且每個鏈接都包含影像以便可以看到。我能夠完成 GET 請求 API 方法,以便將每個組態檔影像和橫幅檔案名添加到重建的鏈接以供查看,如我UserProfileController班級中的以下方法所示:
@GetMapping("/profile")
public UserProfile profile(Model model, HttpServletRequest request) {
final String authHeader = request.getHeader(AUTH_HEADER);
String username = null;
String jwt = null;
if (authHeader != null && authHeader.startsWith("Bearer")) {
jwt = authHeader.substring(7);
username = jwtUtils.getUserNameFromJwtToken(jwt);
}
//Load logged in Username from JWT Token obtained
UserDetailsImpl customUser = (UserDetailsImpl) userDetailsService.loadUserByUsername(username);
Long userId = customUser.getId();
UserProfile userProfile = userProfRepo.findByUserId(userId);
model.addAttribute("profile_img", userProfile.getProfile_img_complete());
model.addAttribute("profile_banner", userProfile.getProfile_banner_complete());
return userProfile;
}
最后三行代碼在此函式中非常重要,因為它是完成 URL 鏈接重建的地方model.addAttribute,因此當用戶查看自己的個人資料卡時,當影像被拉出時,鏈接可以正確地查看影像。所以現在我已經解釋了我是如何使用重建的鏈接來提取影像的,最后讓我解釋一下我的用戶帖子(而不是用戶個人資料)的問題。
正如我所說,我希望用戶發布影像并讓他們為該影像命名。提交用戶帖子后,我希望將其存盤在我的資料庫中(完成此操作),然后我希望所有用戶的所有帖子都顯示在我的 Web 應用程式的特定頁面上,這就是我迫切需要幫助的地方. 我需要想辦法將每個帖子的“post_image”列中的檔案名添加到將post_img_complete在我的請求正文中的鏈接中。這是我迄今為止在我的 UserPostController 中嘗試過的:
@GetMapping("/list")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public List<UserPost> post(Model model) {
List<UserPost> allUserPosts = userPostRepo.findAllByOrderByIdDesc();
model.addAttribute("post_image", allUserPosts.getPost_img_complete()); // Error here: Cannot resolve method 'getPost_img_complete' in 'List'
return allUserPosts;
}
這是我的 UserPost 物體類(使用 JPA Hibernate),其中包含我的 getPost_img_complete() 方法和我正在談論的 URL 鏈接:
package com.Application.models;
import javax.persistence.*;
@Entity
@Table(name = "user_posts")
public class UserPost {
@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private UserProfile user;
@Column(name = "title")
private String title;
@Column(name = "post_image")
private String postImage;
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public UserProfile getUser() {
return user;
}
public void setUser(UserProfile user) {
this.user = user;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPostImage() {
return postImage;
}
public String getPost_img_complete() {
return " https://bucket.s3.us-east-2.amazonaws.com/" postImage;
}
public void setPostImage(String postImage) {
this.postImage = postImage;
}
}
我知道這可能不是提取存盤在 S3 存盤桶中的影像的最有效方式,但這是我目前正在做的事情。因此,對于每個帖子檔案名,我需要將其添加到 getPost_img_complete 中 URL 鏈接的末尾以供查看,以便在加載頁面時可以查看所有帖子的影像。我認為這可能與我這次使用的串列與之前沒有使用的串列有關(在我上面顯示的用戶組態檔示例中)。如果有人可以幫助我了解我需要做些什么來實作這一點,那將是一個巨大的幫助。抱歉,我花了這么長時間才明白這一點,我只是想在我的問題上包含正確的資訊。在此先感謝,如有必要,我將添加任何有助于我解決此問題的資訊。
uj5u.com熱心網友回復:
你不能這樣做,model.addAttribute("post_image", allUserPosts.getPost_img_complete());因為allUserPosts是一個串列。
嘗試迭代它:
List<UserPost> allUserPosts = userPostRepo.findAllByOrderByIdDesc();
allUserPosts.forEach(post -> model.addAttribute("post_image", post.getPost_img_complete()));
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/459042.html
上一篇:無法建立HibernateSessionFactory;嵌套例外是org.hibernate.MappingException:無法實體化id生成器
