After following a lot of online articles about starting up a CRUD app with Java/Mongo, I am seeing this error. I did a lot of iterations, but cannot figured it out…
Blog Model
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "Blog")
public class Blog {
@Id
private int id;
private String title;
}
Blog Repo
import java.util.Optional;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface BlogRepository extends MongoRepository<Blog, String> {
}
Blog Controller (Where I get the error)
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BlogController {
private BlogRepository blogRepo;
@Autowired
public BlogController(BlogRepository blogRepo) {
this.blogRepo = blogRepo;
}
@PostMapping(value = "/add")
public String addBlog(@RequestBody Blog blog){
blogRepo.save(blog);
return "Blog created successfully";
}
@GetMapping(value = "/get")
public String getBlog(){
blogRepo.findAll();
return "Blog created successfully";
}
}
I get it on the addBlog method:
The method save(Iterable) in the type MongoRepository<Blog,String> is not applicable for the arguments (Blog)
My getBlog is fine. I don’t know why the save method is giving me trouble. Thank you
I am trying to save this to my local mongodb repository
flat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.