Spring Boot演示项目的问题

时间:2018-09-11 16:37:04

标签: java spring spring-boot

我最近开始学习Java的Spring Boot,并在这里尝试实现的一个演示项目https://www.devglan.com/spring-boot/file-upload-angularjs-spring-boot-rest中找到了一个示例。 “演示”并没有真正深入,所以我只是尝试使用他拥有的代码在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(); intellij告诉我找不到方法getFile()。我不确定自己做错了什么,因为我所做的唯一更改是不是在我使用findById()的同一行上使用了findOne()。

这是documentDao.java代码:

package com.formupload.demo.dao;

import com.formupload.demo.service.Document;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DocumentDao extends CrudRepository<Document, Long> {

}

如果有人可以帮助您,将不胜感激。

这也是Document.java代码:

package com.formupload.demo.service;

import javax.persistence.*;

@Entity
public class Document {

    private long id;

    @Column
    private String docName;

    @Column
    @Lob
    private byte[] file;

    public long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDocName() {
        return docName;
    }

    public void setDocName(String docName) {
        this.docName = docName;
    }

    public void setFile(byte[] file) {
        this.file = file;
    }

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

}

3 个答案:

答案 0 :(得分:2)

findById返回Optional<Document>,而不是Document

您的代码应为:

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

答案 1 :(得分:0)

如果您在网上遇到错误:

return documentDao.findbyId(id).getFile();

假设找不到getFile()方法,则您的Document类很可能没有签名为getFile()的方法。在您关注的教程中,Document类中有一条注释:

    //getters and setters goes here

因此,您应确保根据需要添加了getter和setter,如下所示:

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

答案 2 :(得分:0)

FindById在所使用的Spring数据版本的CrudRepository中未定义。

https://docs.spring.io/autorepo/docs/spring-data-commons/1.13.0.M1/api/org/springframework/data/repository/CrudRepository.html

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

@Repository
public interface DocumentDao extends CrudRepository<Document, Long>{
    Document findById(Long aLong);
}

然后您可以像这样使用它:

documentDao.findById(id).getFile();

如果您升级到2.0.10.RELEASE,则可以默认使用findById,因为它是CrudRepository定义的一部分。

请参阅:https://jira.spring.io/browse/DATACMNS-944,作为此提交的一部分,findOne也已在版本2中删除。