英文:
Postman output "status":404,"error":"Not Found","path":"/api/employees"
问题
这是您提供的代码的翻译部分:
Spring Boot与MySQL的REST应用程序
我正在尝试将记录添加到数据库,并期望记录被添加,并且Postman将其作为输出而不是输出中的“status”:404,“error”:“Not Found”,“path”:"/api/employees"。以下是相关类:
```java
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;
}
// 创建员工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);
}
}
请注意,我已经将代码中的HTML实体代码(如"
)转换为相应的双引号。
英文:
the spring boot REST app with MySQL
I am trying to add record to DB and expect the record added and postman got it as output instead of the output "status":404,"error":"Not Found","path":"/api/employees" following is the classes
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);
}
}
答案1
得分: 1
不像你正在扫描包含 REST 控制器的包(即 com.controller), 似乎你只在 RestapimysqlApplication 类中扫描了 service.impl 包。
你可能需要将 com.controller 添加到 Spring 扫描的包列表中,如下所示:
@EnableJpaRepositories(basePackages = {"com.repository"})
@SpringBootApplication(scanBasePackages = {"service.impl", "com"})
@EntityScan({"com.model"})
public class RestapimysqlApplication {
...
}
英文:
It doesn't look like you're scanning the package that contains your REST controller (i.e. com.controller), seems like you're only scanning the service.impl package on your RestapimysqlApplication class.
You should probably add the com.controller to the list of packages being scanned by Spring here:
@EnableJpaRepositories(basePackages = {"com.repository"})
@SpringBootApplication(scanBasePackages = {"service.impl", "com"})
@EntityScan({"com.model"})
public class RestapimysqlApplication {
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论