Issue
First thanks to help me.
I am stuck in this error when I call my get method I get 404 error. I see all the files and I think all is ok. Can you help me? I share you the structure of my project and files.
Controller:
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alejandrocode.demo.interfacesService.IpersonaService;
import com.alejandrocode.demo.model.Persona;
@RestController
public class Controlador {
@Autowired
private IpersonaService service;
@GetMapping(value="/obtenerPersonas")
public String listar(Model model) {
List<Persona> personas = service.listar();
model.addAttribute("personas",personas);
return"index";
}
}
Main class:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import com.alejandrocode.demo.controller.Controlador;
@SpringBootApplication
public class ProjectAlejandroSeguridadItApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectAlejandroSeguridadItApplication.class, args);
}
}
Interface
package com.alejandrocode.demo.interfaces;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.alejandrocode.demo.model.Persona;
@Repository
public interface IPersona extends CrudRepository<Persona,Integer>{
}
Interface Service:
package com.alejandrocode.demo.interfacesService;
import java.util.List;
import java.util.Optional;
import com.alejandrocode.demo.model.Persona;
public interface IpersonaService {
public List<Persona> listar();
public Optional<Persona>litarId(Integer id);
public int save(Persona p);
public void delete(Integer id);
}
Model
package com.alejandrocode.demo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="persona")
public class Persona {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String telefono;
private String mail;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Persona(Integer id, String name, String telefono, String mail) {
super();
this.id = id;
this.name = name;
this.telefono = telefono;
this.mail = mail;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTelefono() {
return telefono;
}
public void setTelefono(String telefono) {
this.telefono = telefono;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
}
Service
package com.alejandrocode.demo.service;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alejandrocode.demo.interfaces.IPersona;
import com.alejandrocode.demo.interfacesService.IpersonaService;
import com.alejandrocode.demo.model.Persona;
@Service
public class PersonaService implements IpersonaService {
@Autowired
private IPersona data;
@Override
public List<Persona> listar() {
return (List<Persona>) data.findAll();
}
@Override
public Optional<Persona> litarId(Integer id) {
// TODO Auto-generated method stub
return null;
}
@Override
public int save(Persona p) {
// TODO Auto-generated method stub
return 0;
}
@Override
public void delete(Integer id) {
// TODO Auto-generated method stub
}
}
And my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.alejandrocode</groupId>
<artifactId>ProjectAlejandroSeguridadIT</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ProjectAlejandroSeguridadIT</name>
<description>Alejandro Martin Spring Boot Project</description>
<properties>
<java.version>16</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>
</project>
My error:
If you need more info about my project tell me on responses and I give you all that you need.
Thanks a lot
Solution
The first problem is with your package hierarchy. Spring won't be able to scan the other packages in which controller and other components are present.
As per your shared package hierarchy your main app class (ProjectAlejandroSeguridadItApplication.java) is in the following package
package com.alejandrocode.core;
and other components are in
package com.alejandrocode.demo.*;
So, if you move your main app class to the following package then spring will be able to scan and configure the beans implicitly.
package com.alejandrocode.demo;
Now your sub packages will be covered by the spring implicitly Component Scan. If you don't want to move the package then other way to achieve the same by explicitly mentioned the component scan as folllwing in your main app class.
@ComponentScan(basePackages = "com.alejandrocode.*")
@SpringBootApplication
Second problem is that you are using @RestController
in the controller (Controlador.java) but your main objective is to render the view with the model data with the help of the thymeleaf
dependency so change @RestController
to @Controller
.
Answered By - harry