使用 MySQL 的 Spring Boot REST 应用程序。
我正在尝试将记录添加到数据库中,并期望添加的记录和邮递员将其作为输出而不是输出:
“状态”:404,“错误”:“未找到”,“路径”:“/api/employees”
以下是课程:
package com.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.service.EmployeeService;
import com.model.Employee;
@RestController
@RequestMapping("/api")
public class EmployeeController {
private EmployeeService employeeservice;
public EmployeeController(EmployeeService employeeservice) {
super();
this.employeeservice = employeeservice;
}
//build create Employee REST API
@PostMapping("/employees")
public ResponseEntity<Employee> saveEmployee(@RequestBody Employee employee)
{
return new ResponseEntity<Employee>(employeeservice.saveEmployee(employee), HttpStatus.CREATED);
}
}
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@EnableJpaRepositories(
basePackages = {"com.model"})
@SpringBootApplication(scanBasePackages = {"service.impl"})
public class RestapimysqlApplication {
public static void main(String[] args) {
SpringApplication.run(RestapimysqlApplication.class, args);
}
}
看起来您没有扫描包含 REST 控制器(即 com.controller)的包,似乎您只是扫描 RestapimysqlApplication 类上的 service.impl 包。
您可能应该将 com.controller 添加到 Spring 正在扫描的包列表中:
@EnableJpaRepositories(basePackages = {"com.repository"})
@SpringBootApplication(scanBasePackages = {"service.impl", "com"})
@EntityScan({"com.model"})
public class RestapimysqlApplication {
...
}