我正在创建Sping Boot 应用程序。这里有MediaContent实体:
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
@Table(name = "media_content")
public class MediaContent extends AbstractEntity{
@Column(unique = true)
private String name;
@Column
@Lob
private byte[] video;
@Column
@Lob
private byte[] previewImage;
@Column
private String description;
@Column
@Enumerated(value = EnumType.STRING)
private Status status;
@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
@JoinColumn
private User user;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "content")
private List<Like> likes = new ArrayList<>();
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "content")
private List<MediaComments> comments = new ArrayList<>();
public enum Status{
ACCEPTED, REJECTED, UNDER_REVIEW
}
}
我还有MediaConentRepository、Service和Service Implementation
public interface MediaContentRepository extends JpaRepository<MediaContent, UUID> {
MediaContent findMediaContentByName(String name);
}
public interface MediaContentService {
List<MediaContent> findAllContent();
List<MediaComments> findAllComments(MediaContent content);
Integer getAllLikes(MediaContent content);
boolean isLiked(MediaContent content, Principal principal);
User getUserByPrincipal(Principal principal);
MediaContent findContentById(UUID id);
void updateMediaContent(MediaContent content, MultipartFile imageFile, MultipartFile videoFile) throws IOException;
void addMediaContent(Principal principal, MediaContent content, MultipartFile imageFile, MultipartFile videoFile) throws IOException;
boolean deleteMediaContent(User user, UUID id);
}
@Service
@RequiredArgsConstructor
public class MediaContentServiceImpl implements MediaContentService {
private final MediaContentRepository repository;
private final UserRepository userRepository;
private final MediaCommentsRepository commentsRepository;
@Override
public List<MediaContent> findAllContent() {
return repository.findAll().stream().filter(content -> content.getStatus()== MediaContent.Status.ACCEPTED).collect(Collectors.toList());
}
@Override
public List<MediaComments> findAllComments(MediaContent content) {
List<MediaComments> commentList = new ArrayList<>();
commentsRepository.findAll().forEach(comments -> {
if(comments.getMediaID().equals(content.getUuid())){
commentList.add(comments);
}
});
return commentList;
}
@Override
public Integer getAllLikes(MediaContent content) {
return content.getLikes().size();
}
@Override
public boolean isLiked(MediaContent content, Principal principal) {
User user = userRepository.findUserByUsername(principal.getName());
if(content.getLikes().stream().filter(like->like.getUser()==user).count() == 1){
return true;
}
return false;
}
@Override
public MediaContent findContentById(UUID id) {
return repository.findById(id).orElse(null);
}
@Override
public void updateMediaContent(MediaContent content, MultipartFile imageFile, MultipartFile videoFile) throws IOException {
MediaContent content1 = repository.findById(content.getUuid()).orElse(null);
if(content1!=null){
byte[] imageBytes = imageFile.getBytes();
content.setPreviewImage(imageBytes);
byte[] videoBytes = videoFile.getBytes();
content.setVideo(videoBytes);
content.setStatus(MediaContent.Status.UNDER_REVIEW);
repository.delete(content1);
repository.save(content);
} else{
log.error("not such content1 in database");
}
}
@Override
public void addMediaContent(Principal principal, MediaContent content, MultipartFile imageFile, MultipartFile videoFile) throws IOException {
User user = getUserByPrincipal(principal);
if(user.getRole() == User.Role.ADMIN){
content.setStatus(MediaContent.Status.ACCEPTED);
}
content.setUser(user);
byte[] imageBytes = imageFile.getBytes();
byte[] videoBytes = videoFile.getBytes();
content.setVideo(videoBytes);
content.setPreviewImage(imageBytes);
repository.save(content);
}
@Override
public User getUserByPrincipal(Principal principal) {
if (principal == null) return new User();
return userRepository.findUserByUsername(principal.getName());
}
@Override
public boolean deleteMediaContent(User user, UUID id) {
MediaContent content = repository.findById(id).orElse(null);
if(content!=null){
if(content.getUser().getUuid().equals(user.getUuid())){
repository.delete(content);
return true;
}
log.error("Media content with id = {} is not found", id);
} else{
log.error("not such content in database");
}
return false;
}
}
我想得到我所有的媒体内容。我为RestController写了两个不同的方法:
@RestController
@RequiredArgsConstructor
public class MediaContentController {
private final MediaContentServiceImpl service;
@PermitAll
@GetMapping("/media")
@ResponseBody
private List<MediaContentResponse> getAllMediaContents(){
List<MediaContent> contentList = service.findAllContent();
List<MediaContentResponse> list = new ArrayList<>();
contentList.forEach(content -> {
list.add(new MediaContentResponse(
content.getUuid(), content.getName(), content.getDescription(), content.getUser()
));
});
return list;
}
@PermitAll
@GetMapping("/media")
public ResponseEntity<List<MediaContent>> getAll(){
List<MediaContent> mediaContents = service.findAllContent();
return ResponseEntity.ok(mediaContents);
}
}
但在第一种情况下,我遇到了这个异常“无法调用”org.example.service.impl.MediaContentServiceImpl.findAllContent()“,因为“this.service”为null”。当我使用第二个时,我得到数据
请帮帮我,问题出在哪里?我在其他控制器中也有类似的异常。我试图删除一些注解,将RestController更改为Controller,但这一切都没有t help me.
您的文本`
1条答案
按热度按时间8wtpewkr1#
使用前需要注入服务
这同样适用于其他依赖关系,例如资料库