【发布时间】: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