【问题标题】:Getting resource not found 404 even URL is correct spring boot获取资源未找到 404 即使 URL 是正确的 spring boot
【发布时间】:2021-09-06 07:29:47
【问题描述】:

部门主管

@RestController
public class DepartmentController {
    
    @Autowired
    DepartmentControllerConsumer departmentService;
   
    @GetMapping("/departments")
    public ResponseEntity<List<DepartmentResponse>> getAllDepartments() {
        ResponseEntity<List<DepartmentResponse>> departments = departmentService.getAllDepartmentsUsingClient();
        return  departments;
    } 

}

部门控制器消费者

import reactor.core.publisher.Flux;
@Service
public class DepartmentControllerConsumer {
    
    public ResponseEntity<List<DepartmentResponse>> getAllDepartmentsUsingClient() {
        List<DepartmentResponse>list=new ArrayList<>();
        WebClient client =WebClient.create("http://localhost:8080/restservice/api/");
        System.out.println(client);
        Flux<DepartmentResponse> value = client.get().uri("departments").accept(MediaType.APPLICATION_JSON).retrieve()
                .bodyToFlux(DepartmentResponse.class);
        list=value.toStream().collect(Collectors.toList());
        System.out.println(value.toStream().collect(Collectors.toList()));
        return new ResponseEntity<List<DepartmentResponse>>(list, HttpStatus.OK);
    }   
}

上面是控制器和服务类,有一个端点。从邮递员那里调用它时,我得到 404 甚至给出了正确的 URL 路径

【问题讨论】:

  • 你在哪里定义/restservice/api/
  • 这是我本地的另一个 spring boot 应用程序,只是使用这个 boot 应用程序中的 webclient 调用它
  • Sicne 它是一个 GET API,您是直接在浏览器中尝试还是在邮递员中尝试?当我们使用错误的 URI 时出现 404 请检查以下 url 是否在任何浏览器或邮递员localhost:8080/departments 中工作
  • 问题是由于项目的包结构问题,通过将类保存在正确的包中解决了问题,感谢支持。

标签: java spring spring-boot rest


【解决方案1】:

是的,如果您的控制器类与源包不在同一个包中,您可能需要将它们放在同一个包中或使用@ComponentScan 来扫描您的包:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 2016-08-12
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    相关资源
    最近更新 更多