尝试访问 API 端点但在其他端点上正常工作时出现 404 错误代码

404 error code when attempting to access an API endpoint but works normally on others

我在 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 中设置上下文的内容,但是无论我放在那里什么都会导致 404 错误,即使是 Insomnia 软件也是如此。我尝试添加 /beneficiaries/api/beneficiares/api,但我认为这与它没有任何关系。控制台中没有错误消息可见。

打错了:)。我 运行 前段时间遇到过类似的问题。花了我几个小时来解决。只需从您的@PostMapping 中删除 ("/")