当前位置: 首页 > 知识库问答 >
问题:

springboot api服务:“状态”:404,“错误”:“未找到”,

穆博简
2023-03-14

我试图使一个API工作在springstart但是当我输入请求:http://localhost:8080/employee/all我得到这个结果:

"timestamp": "2021-10-10T20:05:58.353+00:00",
    "status": 404,
    "error": "Not Found",
    "path": "/employee/all"

它是一个经典的服务,包含一个模型、一个服务、一个存储库、一个映射器和一个异常(如果没有员工),使用的数据库是sql中的实体,如下所示

服务:

package com.employee.employeemanager.service;

import com.employee.employeemanager.exception.UserNotFoundException;
import com.employee.employeemanager.model.Employee;
import com.employee.employeemanager.repo.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class EmployeeService {

    private final EmployeeRepository employeeRepository;

    @Autowired
    public EmployeeService(EmployeeRepository employeeRepository) {

        this.employeeRepository = employeeRepository;
    }

    public Employee addEmployee(Employee employee){
        employee.setEmployeeCode(UUID.randomUUID().toString());
        return employeeRepository.save(employee);
    }

    public List<Employee> findAllEmployees() {
        return employeeRepository.findAll();
    }

    public Employee updateEmployee(Employee employee) {
        return employeeRepository.save(employee);
    }

    public Employee findEmployeeById(Long id){
        return (Employee) employeeRepository.findEmployeeById(id).orElseThrow(() -> new UserNotFoundException("User by id " + id + " was not found"));

    }
    public void deleteEmployee(Long id){
        employeeRepository.deleteEmployeeById(id);
    }

}

仓库

package com.employee.employeemanager.repo;

import com.employee.employeemanager.model.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {

    void deleteEmployeeById(Long id);

    Optional<Employee> findEmployeeById(Long id);
}

模型

package com.employee.employeemanager.model;

import javax.persistence.*;
import java.io.Serializable;

@Entity
public class Employee implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false, updatable = false)
    private Long id;
    private String name;
    private String email;
    private String jobTitle;
    private String phone;
    private String imageUrl;
    @Column(nullable = false, updatable = false)
    private String employeeCode;

    public Employee() {
    }

    public Employee(String name, String email, String jobTitle, String phone, String imageUrl, String employeeCode) {
        this.name = name;
        this.email = email;
        this.jobTitle = jobTitle;
        this.phone = phone;
        this.imageUrl = imageUrl;
        this.employeeCode = employeeCode;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getJobTitle() {
        return jobTitle;
    }

    public void setJobTitle(String jobTitle) {
        this.jobTitle = jobTitle;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getEmployeeCode(){
        return employeeCode;
    }

    public void setEmployeeCode(String employeeCode) {
        this.employeeCode = employeeCode;
    }

    @Override
    public String toString() {
        return "Employee{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", email'" + email + '\'' +
            ", jobTitle'" + jobTitle + '\'' +
            ", phone'" + phone + '\'' +
            ", imageUrl'" + imageUrl + '\'' +
        '}';
    }
}

制图员

package com.employee.employeemanager.mapper;

import com.employee.employeemanager.model.Employee;
import com.employee.employeemanager.service.EmployeeService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController("/employee")
public class EmployeeMapper {
    private final EmployeeService employeeService;

    public EmployeeMapper(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @GetMapping("/all")
    public ResponseEntity<List<Employee>> getAllEmployees() {
        List<Employee> employees = employeeService.findAllEmployees();
        return new ResponseEntity<>(employees, HttpStatus.OK);
    }

    @GetMapping("/find/{id}")
    public ResponseEntity<Employee> getAllEmployees (@PathVariable("id") Long id) throws Throwable {
        Employee employees = employeeService.findEmployeeById(id);
        return new ResponseEntity<>(employees, HttpStatus.OK);
    }
    
    @PostMapping("/add")
    public ResponseEntity<Employee> addEmployee(@RequestBody Employee employee){
        Employee newEmployee = employeeService.addEmployee(employee);
        return new ResponseEntity<>(newEmployee, HttpStatus.CREATED);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<?> deleteEmployee(@PathVariable("id") Long id){
        employeeService.deleteEmployee(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}

例外

package com.employee.employeemanager.exception;

public class UserNotFoundException extends RuntimeException{
    public UserNotFoundException(String message) {
        super(message);
    }
}

波姆。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.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.employee</groupId>
    <artifactId>employeemanager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>employeemanager</name>
    <description>EMployee Manager App </description>
    <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-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><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>10</source><target>10</target></configuration></plugin>
        </plugins>
    </build>

</project>

共有1个答案

丰俊艾
2023-03-14

问题出在EmployeeMapper中。

这就是您在代码中所做的:

@RequestMapping
@RestController("/employee")

如果将其更改为:

@RequestMapping("/employee")
@RestController

它应该被修复。

 类似资料:
  • 控制器: spring-mvc-servlet.xml:

  • 你好,作为角2学习项目的一部分,我正在创建一个测试应用程序。 我试图急切地加载一个名为的模块,但是,出现以下错误: 错误:(SystemJS)XHR错误(404未找到)加载http://localhost:3000/app/core.js错误:XHR错误(未找到404)加载http://localhost:3000/app/core.js在XMLHttpRequest。wrapFn[as _onr

  • 我有一个带有Spring Boot的简单微服务应用程序,我尝试添加Spring云网关服务,但它不起作用。所有的微服务,包括Eureka服务器,都工作得很好,但当我尝试使用网关路由访问某些微服务时,它找不到微服务。 ApiGateway pom。xml API网关应用程序。yml公司 API网关主 Api网关未找到404错误

  • 这是我在SpringMVC中使用Maven的第一个应用程序。下面是应用程序结构。 控制器代码: 当我点击index.jsp中的链接时,页面应该被遍历到helloworld.jsp

  • 我有一个ReactJS应用程序,我正试图部署在heroku上。它在本地运行良好,但部署不正确!在页面上,我无法获得/ 应用程序链接:https://nameless-spire-06291.herokuapp.com/ 控制台错误:1。加载资源失败:服务器响应状态为404(未找到)2。拒绝加载图像的https://nameless-spire-06291.herokuapp.com/favicon

  • 问题内容: 我将在春季阅读有关宁静的Web服务的这本书。我决定放弃他们的工作,而使用Java配置文件。出于某种原因,切换到Java配置后,该服务将正确运行(在控制台窗口中),但是当我实际转到localhost上的端点时,我得到了: 白标错误页 此应用程序没有针对/ error的显式映射,因此您将其视为后备。 PDT 2016年4月23日星期六20:48:25发生意外错误(类型=未找到,状态= 40