Java SpringBoot演示项目的问题

Java SpringBoot演示项目的问题,java,spring,spring-boot,Java,Spring,Spring Boot,我最近开始学习SpringBootforJava,并在这里找到了一个演示项目:我试图实现它。“演示”并没有真正深入,所以我只是试着用intellij使用他提供的代码重新制作这个项目。我目前在DocumentServiceImpl.java文件中遇到一个问题: package com.formupload.demo.service; import com.formupload.demo.dao.DocumentDao; import org.springframework.beans.facto

我最近开始学习SpringBootforJava,并在这里找到了一个演示项目:我试图实现它。“演示”并没有真正深入,所以我只是试着用intellij使用他提供的代码重新制作这个项目。我目前在DocumentServiceImpl.java文件中遇到一个问题:

package com.formupload.demo.service;

import com.formupload.demo.dao.DocumentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

public class DocumentServiceImpl implements DocumentService {

    @Autowired
    private DocumentDao documentDao;


    @Override
    public ResponseMetadata save(MultipartFile file) throws IOException {

        Document doc = new Document();
        doc.setDocName(file.getOriginalFilename());
        doc.setFile(file.getBytes());
        documentDao.save(doc);
        ResponseMetadata metadata = new ResponseMetadata();
        metadata.setMessage("success");
        metadata.setStatus(200);
        return metadata;
    }

    @Override
    public byte[] getDocumentFile(Long id) {
        return documentDao.findById(id).getFile();
    }

    @Override
    public List<Document> findAll() {
        return (List<Document>) documentDao.findAll();
    }
}

如果您在线路上遇到错误:

return documentDao.findbyId(id).getFile();
如果说找不到
getFile()
方法,那么您的
文档
类可能没有签名为
getFile()
的方法。在下面的教程中,Document类中有一条注释:

    //getters and setters goes here
因此,您应该确保根据需要添加getter和setter,如下所示:

public byte[] getFile() {
    return file;
}

如果您在线路上遇到错误:

return documentDao.findbyId(id).getFile();
如果说找不到
getFile()
方法,那么您的
文档
类可能没有签名为
getFile()
的方法。在下面的教程中,Document类中有一条注释:

    //getters and setters goes here
因此,您应该确保根据需要添加getter和setter,如下所示:

public byte[] getFile() {
    return file;
}
返回一个而不是
文档

您的代码应该是:

@Override
public byte[] getDocumentFile(Long id) {
    return documentDao.findById(id)
                      .orElseThrow(() -> new IllegalArgumentException("Document not found: " + id))
                      .getFile();
}
返回一个而不是
文档

您的代码应该是:

@Override
public byte[] getDocumentFile(Long id) {
    return documentDao.findById(id)
                      .orElseThrow(() -> new IllegalArgumentException("Document not found: " + id))
                      .getFile();
}

您正在使用的Spring数据版本的CRUDepository中未定义FindById

如果需要,应该在DocumentDao中添加findById:

@Repository
public interface DocumentDao extends CrudRepository<Document, Long>{
    Document findById(Long aLong);
}
如果升级到2.0.10.RELEASE,默认情况下可以使用findById,因为它是crudepository定义的一部分


请参阅:,作为此提交的一部分,在版本2中也删除了FindDone。

您正在使用的Spring数据版本的Crudepository中未定义FindById

如果需要,应该在DocumentDao中添加findById:

@Repository
public interface DocumentDao extends CrudRepository<Document, Long>{
    Document findById(Long aLong);
}
如果升级到2.0.10.RELEASE,默认情况下可以使用findById,因为它是crudepository定义的一部分


请参阅:,作为此提交的一部分,findOne在版本2中也被删除。

您错过了
DocumentServiceImpl
@NicholasK中的注释
@Service
。感谢您让我知道,不幸的是,这并没有解决我的其他问题。返回一个而不是
文档
您错过了
documentserviceinpl
@NicholasK中的注释
@Service
,非常感谢您让我知道,不幸的是,这并没有解决我的其他问题。返回一个,而不是一个
文档
我刚刚添加了Document.java代码,我确实在其中有一个getFile()方法。
findById
返回一个
可选的
,在调用`getFile()`之前必须检查该值,然后将其展开。我刚刚添加了Document.java代码,其中确实有一个getFile()方法,
findById
返回一个
Optional
,在调用`getFile()``之前,必须检查该值,然后将其展开。