我的 spring 引导控制器方法出现 404 错误
404 error in my spring boot controler method
首先感谢帮助我。
我在调用 get 方法时遇到了这个错误,我收到 404 错误。我看到了所有文件,我认为一切正常。你能帮助我吗?我与您分享我的项目和文件的结构。
Package structure:
控制器:
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";
}
}
主要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);
}
}
界面
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>{
}
接口服务:
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);
}
型号
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;
}
}
服务
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
}
}
还有我的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>
我的错误:
如果您需要有关我的项目的更多信息,请告诉我回复,我会为您提供所需的一切。
非常感谢
第一个问题是您的包层次结构。 Spring 将无法扫描存在控制器和其他组件的其他包。
根据您的共享包层次结构,您的主要应用程序 class (ProjectAlejandroSeguridadItApplication.java) 如下包裹
package com.alejandrocode.core;
其他组件在
package com.alejandrocode.demo.*;
因此,如果您将主应用 class 移至以下包,则 spring 将能够隐式扫描和配置 bean。
package com.alejandrocode.demo;
现在您的子包将被 spring 隐式组件扫描覆盖。如果您不想移动包,那么通过在您的主应用程序中明确提到组件扫描作为 folllwing class.
来实现相同的其他方法
@ComponentScan(basePackages = "com.alejandrocode.*")
@SpringBootApplication
第二个问题是您在控制器 (Controlador.java) 中使用 @RestController
但您的主要objective 是在 thymeleaf
依赖项的帮助下使用模型数据渲染视图,因此将 @RestController
更改为 @Controller
.
首先感谢帮助我。
我在调用 get 方法时遇到了这个错误,我收到 404 错误。我看到了所有文件,我认为一切正常。你能帮助我吗?我与您分享我的项目和文件的结构。
Package structure:
控制器:
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";
}
}
主要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);
}
}
界面
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>{
}
接口服务:
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);
}
型号
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;
}
}
服务
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
}
}
还有我的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>
我的错误:
如果您需要有关我的项目的更多信息,请告诉我回复,我会为您提供所需的一切。
非常感谢
第一个问题是您的包层次结构。 Spring 将无法扫描存在控制器和其他组件的其他包。
根据您的共享包层次结构,您的主要应用程序 class (ProjectAlejandroSeguridadItApplication.java) 如下包裹
package com.alejandrocode.core;
其他组件在
package com.alejandrocode.demo.*;
因此,如果您将主应用 class 移至以下包,则 spring 将能够隐式扫描和配置 bean。
package com.alejandrocode.demo;
现在您的子包将被 spring 隐式组件扫描覆盖。如果您不想移动包,那么通过在您的主应用程序中明确提到组件扫描作为 folllwing class.
来实现相同的其他方法@ComponentScan(basePackages = "com.alejandrocode.*")
@SpringBootApplication
第二个问题是您在控制器 (Controlador.java) 中使用 @RestController
但您的主要objective 是在 thymeleaf
依赖项的帮助下使用模型数据渲染视图,因此将 @RestController
更改为 @Controller
.