Reputation: 89
I recently started learning spring boot for java, and found a demo project here: https://www.devglan.com/spring-boot/file-upload-angularjs-spring-boot-rest which I tried to implement. The "demo" doesn't really go in depth so I just tried remaking the project in intellij using the code he has. I'm currently running into an issue in the DocumentServiceImpl.java file:
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();
}
}
the part that is giving me trouble is the line: return documentDao.findbyId(id).getFile(); intellij is telling me it can't find the method getFile(). I'm not sure what I did wrong as the only thing I changed was instead of using findOne() on that same line I used findById().
Here is the documentDao.java code:
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> {
}
If anyone could help that would be greatly appreciated.
Here is the Document.java code as well:
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;
}
}
Upvotes: 0
Views: 189
Reputation: 2230
FindById Not defined in CrudRepository Of the Spring-data version you are using.
if you need it, you should add findById inside your DocumentDao :
@Repository
public interface DocumentDao extends CrudRepository<Document, Long>{
Document findById(Long aLong);
}
and then you can use it like:
documentDao.findById(id).getFile();
if you upgrade to 2.0.10.RELEASE you could use findById by default as it's part of CrudRepository definition.
see: https://jira.spring.io/browse/DATACMNS-944 , findOne also removed in version 2 as part of this commit.
Upvotes: 0
Reputation: 159086
findById
returns an Optional<Document>
, not a Document
.
Your code should be:
@Override
public byte[] getDocumentFile(Long id) {
return documentDao.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Document not found: " + id))
.getFile();
}
Upvotes: 2
Reputation: 963
If you are getting an error on the line:
return documentDao.findbyId(id).getFile();
saying that the getFile()
method cannot be found, then it is likely that your Document
class does not have a method with the signature getFile()
. In the tutorial you are following, there is a comment in the Document class saying:
//getters and setters goes here
So you should make sure that you added the getters and setters as needed, like so:
public byte[] getFile() {
return file;
}
Upvotes: 0