Postman输出 “status”: 404, “error”: “Not Found”, “path”: “/api/employees”

huangapple go评论56阅读模式
英文:

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实体代码(如&quot;)转换为相应的双引号。

英文:

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(&quot;/api&quot;)     
public class EmployeeController {      
private EmployeeService employeeservice;      
public EmployeeController(EmployeeService employeeservice) {     
super();     
this.employeeservice = employeeservice;     
}       
//build create Employee REST API     
@PostMapping(&quot;/employees&quot;)    
public ResponseEntity&lt;Employee&gt; saveEmployee(@RequestBody Employee employee)    
{     
return new ResponseEntity&lt;Employee&gt;(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 = {&quot;com.model&quot;})     
@SpringBootApplication(scanBasePackages = {&quot;service.impl&quot;})     
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 = {&quot;com.repository&quot;})
@SpringBootApplication(scanBasePackages = {&quot;service.impl&quot;, &quot;com&quot;})
@EntityScan({&quot;com.model&quot;})
public class RestapimysqlApplication {
...
}

huangapple
  • 本文由 发表于 2023年2月14日 07:53:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定