【问题标题】:404 error code when attempting to access an API endpoint but works normally on others尝试访问 API 端点但在其他端点上正常工作时出现 404 错误代码
【发布时间】:2019-04-16 01:58:08
【问题描述】:

我在com.project.users 包中有一个端点,它将获取登录用户的信息:

@RestController
@RequestMapping(path = "/api")
public class UserController {
    @Autowired
    private UserRepository repository;

    @GetMapping("me")
    public User me() {
        Optional<User> ouser = repository.findById(1);
        return ouser.get();
    }
}

我在com.project.beneficiary 中有另一个控制器,我试图通过 POST 请求访问一个方法,但我收到了 404 错误消息:

Request URL:http://localhost:8080/api/beneficiaries
Request method:POST
Remote address:127.0.0.1:8080
Status code:
404
Version:HTTP/1.1
Referrer Policy:no-referrer-when-downgrade

{"timestamp":"2019-04-16T01:46:37.395+0000","status":404,"error":"Not Found","message":"No message available","path":"/api/beneficiaries"}
@RestController
@RequestMapping(path = "/api/beneficiaries")
public class BeneficiaryController {
    @Autowired
    private BeneficiaryRepository repository;

    @PostMapping("/")
    public Beneficiary addBeneficiary(@Valid @RequestBody Beneficiary beneficiary) {
        return repository.save(beneficiary);
    }
}

我已经处理了 CORS,我认为它有效,因为我在任何地方都没有看到有关它的消息。所有这些包都与应用程序的起点处于同一级别,但奇怪的是为什么看到一个而另一个没有。与 POST 请求有关吗?

我发现了一些关于在application.properties 中设置上下文的信息,但无论我放在那里,即使是 Insomnia 软件也会导致 404 错误。我尝试添加/beneficiaries/api/beneficiares/api,但我认为这与它无关。控制台中看不到任何错误消息。

【问题讨论】:

  • 尝试从您的@PostMapping 中删除("/")
  • GET /api/me 有效吗?如果可行,请尝试使用/api2/beneficiaries 修改/api/beneficiaries 并使用新路径测试POST。
  • @Mr.J4mes,请写一个答案。

标签: spring-boot http http-status-code-404 endpoint


【解决方案1】:

这是一个错字:)。前段时间我遇到了类似的问题。我花了几个小时才解决。只需从您的@PostMapping 中删除("/")

【讨论】:

    猜你喜欢
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 2021-02-14
    相关资源
    最近更新 更多