This question already has an answer here:
Spring mvc @RequestMapping on class level and method level 404 Status
                            
                                (1个答案)
                            
                    
                8天前关闭。
        

    

我有一个简单的RestController,它将用作API。我需要每个单一端点方法
重用其路径的前缀,因此我在类级别的RequestMapping批注中编写了它。
当我尝试执行“ / api / companies / all”的GET时,它返回404错误:

@RestController
@RequestMapping("/api/companies")
public class CompanyApi {
  @GetMapping("/all")
  public ResponseEntity<String> getAllCompanies() {
    return ResponseEntity.ok("all companies");
  }
}


但是,当在每个方法的注释中写入前缀时,它会起作用,如下所示:

@RestController
public class CompanyApi {
  @GetMapping("/api/companies/all")
  public ResponseEntity<String> getAllCompanies() {
    return ResponseEntity.ok("all companies");
  }
}


我需要重用路径“ api / companies”,但是会发生错误。如何在春季解决此问题?

最佳答案

您不应该在/值中以@GetMapping开头,它将创建重复的/,请执行以下操作:

@GetMapping("all")

10-02 07:47