I was creating a CRUD using spring boot. I had no problem with listing and adding records. The problem happened when I added the method remove. The bean is not found…
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'controllerContact': Unsatisfied dependency expressed through field 'contactservice'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'contactserviceimpl': Unsatisfied dependency expressed through field 'contactrepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactrepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract paqueteninja5.entity.Contact paqueteninja5.repository.ContactRepository.findbyid(int)! No property findbyid found for type Contact!
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'contactserviceimpl': Unsatisfied dependency expressed through field 'contactrepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactrepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract paqueteninja5.entity.Contact paqueteninja5.repository.ContactRepository.findbyid(int)! No property findbyid found for type Contact!
the controller is:
package paqueteninja5.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import paqueteninja5.constant.ViewConstant;
import paqueteninja5.model.ContactClass;
import paqueteninja5.service.ContactService;
@Controller
@RequestMapping("/contacts")
public class ControllerContact {
@Autowired
@Qualifier("contactserviceimpl")
private ContactService contactservice;
@GetMapping("/contactform")
public String redirectcontactform(Model modelo) {
modelo.addAttribute("contactModel", new ContactClass());
return ViewConstant.CONTACTFORM;
}
@GetMapping("/cancel")
public String cancel() {
return "redirect:/contacts/showcontacts";
}
@PostMapping("/addcontact")
public String addcontact(@ModelAttribute (name="contactModel") ContactClass contactModel, Model model ) {
if (contactservice.addcontact(contactModel)!=null) {
model.addAttribute("resultado", 1);
}
else {model.addAttribute("resultado", 0);}
return "redirect:/contacts/showcontacts";
}
@GetMapping("/showcontacts")
public ModelAndView showcontacts() {
ModelAndView mav=new ModelAndView(ViewConstant.CONTACTS);
mav.addObject("contacts", contactservice.listallcontacts());
return mav;
}
@GetMapping("/removecontacts")
public ModelAndView removecontacts(@RequestParam(name="id", required=true) int id) {
contactservice.removecontact(id);
return showcontacts();
}
}
El repository is:
package paqueteninja5.repository;
import java.io.Serializable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import paqueteninja5.entity.Contact;
@Repository(“contactrepository”)
public interface ContactRepository extends JpaRepository<Contact, Serializable>{
public abstract Contact findbyid(int id);
}
The service is:
package paqueteninja5.service;
import java.util.List;
import paqueteninja5.entity.Contact;
import paqueteninja5.model.ContactClass;
public interface ContactService {
public abstract ContactClass addcontact(ContactClass contactmodel);
public abstract List<ContactClass> listallcontacts();
public abstract Contact findcontactbyid(int id);
public abstract void removecontact(int id);
}
the class that implements the service is:
package paqueteninja5.service.serviceimpl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import paqueteninja5.component.ComponentConverter;
import paqueteninja5.entity.Contact;
import paqueteninja5.model.ContactClass;
import paqueteninja5.repository.ContactRepository;
import paqueteninja5.service.ContactService;
@Service(“contactserviceimpl”)
public class ContactServiceImpl implements ContactService{
@Autowired
@Qualifier("contactrepository")
private ContactRepository contactrepository;
@Autowired
@Qualifier("contactconverter")
private ComponentConverter contactconverter;
@Override
public ContactClass addcontact(ContactClass contactmodel) {
Contact contact = contactrepository.save(contactconverter.modeltoentity(contactmodel));
return contactconverter.entitytomodel(contact);
}
@Override
public List<ContactClass> listallcontacts() {
List<Contact> contacts=contactrepository.findAll();
List<ContactClass> contactsmodel=new ArrayList<ContactClass>();
contactrepository.findAll();
for(Contact contact: contacts) {
contactsmodel.add(contactconverter.entitytomodel(contact));
}
return contactsmodel;
}
@Override
public Contact findcontactbyid(int id) {
return contactrepository.findbyid(id);
}
@Override
public void removecontact(int id) {
Contact contact = findcontactbyid(id);
if(contact!=null) {
contactrepository.delete(contact);
}
}
}
the pom.xml is:
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
nin5
ninja5
0.0.1-SNAPSHOT
ninja5
proyecto rninja5
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
```
the whole project is here, if you can help me: