简体   繁体   English

Spring 启动,GET 调用在 Postman 中不起作用

[英]Spring Boot , GET invocation not working in Postman

Spring newbie here. Spring 新手在这里。 Three classes三班

Employee.java员工.java

package com.example.model;

public class Employee {

private int employeeId;
private String name;
private String email; 

public Employee(int employeeId, String name, String email)
{
    this.setEmployeeId(employeeId);
    this.setName(name);
    this.setEmail(email); 
}

public int getEmployeeId() {
    return employeeId;
}

public void setEmployeeId(int employeeId) {
    this.employeeId = employeeId;
}

public String getName() {
    return name;
}

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

EmployeeDao.java EmployeeDao.java

@Component
public class EmployeeDao {

static List<Employee> list = new ArrayList<>();
        
        static 
        {
            list.add(new Employee(1234,"Nancy", "nancy@mail.com"));
            list.add(new Employee(5678, "Daniel","daniel@mail.com"));
            list.add(new Employee(9101, "Scott", "scott@mail.com"));
        }

public List<Employee> getAllEmployees()
 {
    return list;
 }

}

EmployeeController.java EmployeeController.java

@RestController
public class EmployeeController {

@Autowired
EmployeeDao service;

@GetMapping(path = "/employees")    
public List<Employee> getAll()
 {
    System.out.println(service.getAllEmployees());
    return service.getAllEmployees();
 }
}

Postman execution of GET employees throws up this error(404) Postman 执行 GET 员工引发此错误 (404) 在此处输入图像描述

Project Setup项目设置

在此处输入图像描述

In a spring boot application there can be a property server.servlet.context-path defined which is the root path of the application.在 spring 引导应用程序中,可以定义一个属性server.servlet.context-path ,它是应用程序的根路径。

Search in the application.properties for this property and if defined, add it to the url path that you call with Postman.在 application.properties 中搜索此属性,如果已定义,请将其添加到您使用 Postman 调用的 url 路径中。

If this is not the case then you can use the following annotation in the class that has the main method that has the @SpringBootApplication如果不是这种情况,那么您可以在 class 中使用以下注释,该注释具有具有@SpringBootApplicationmain方法

@ComponentScan({"com.example.service", "com.example.model"})

As your main application class is under the package demo, with the @SpringBootApplication, it scans the demo package and all its children packages.由于您的主要应用程序 class 在 package 演示下,它使用 @SpringBootApplication 扫描演示 package 及其所有子包。 Your controller is not scanned in this case.在这种情况下,不会扫描您的 controller。 One of the solutions is to reorganise your packages.解决方案之一是重新组织您的包。 For example, remove the demo package.例如,删除演示 package。

Did you put this @SpringBootApplication to your main class?你把这个@SpringBootApplication 放到你的主 class 了吗? Seems your controller is not scanned.似乎您的 controller 未被扫描。 https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/using-boot-using-springbootapplication-annotation.html https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/using-boot-using-springbootapplication-annotation.ZFC35FDC70D5FC69D2539883A822EZ7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM