【问题标题】:Using Spring Boot WebClient to call a dummy api to postman使用 Spring Boot WebClient 调用一个虚拟 api 给邮递员
【发布时间】:2021-12-23 14:07:34
【问题描述】:

我在这里遗漏了一些东西。我正在尝试使用 Spring Boot WebClient 从作为 Http 请求的 Dummy Api 中提取信息。当我进入邮递员时,我没有得到任何信息。

感谢您提供的任何见解。我对编码和自学还很陌生。

这是我的员工控制器:

@Autowired
WebClientApp webClientApp;

@GetMapping("/consume")
public String getEmployee(Model model) {
  model.addAttribute("listEmployees", empServiceImpl.getAllEmployees());
  model.addAttribute("listemps", webClientApp.webClientBuilder());
  return "index";
}

网络客户端

private WebClient webClient;

public  void SimpleWebClient(WebClient webClient) {
  this.webClient = webClient;
}

public Flux<Employee> webClientBuilder() {
  
  return this.webClient
  //this.webClientBuilder = webClientBuilder.baseUrl(DummyEmployee)
    .get()
    .uri("api/v1/employees")
    .retrieve()
    .bodyToFlux(Employee.class);
}

员工

@Data
@ToString
//@AllArgsConstructor
//@NoArgsConstructor
@JsonRootName(value = "data")
public class Employee {

  @JsonProperty("id")   
  public int employeeID;
  @JsonProperty("employee_name")
  public String employeeName;
  @JsonProperty("employee_salary")
  public String employeeSalary;
  @JsonProperty("employee_age")
  public int employeeAge;
  @JsonProperty("employee_image")
  public Blob employeeImage;
}

服务

@Repository
@ComponentScan(basePackages = {"com.example.app.repository"})
@Service
public class ServiceImpl implements EmpService{

    @Autowired
    private EmployeeRepository employeeRepo;
    
    @SuppressWarnings("unchecked")
    public List<Employee> getAllEmployees() {
      return (List<Employee>) employeeRepo.findAll();
    }
}

服务

@Service
public interface EmpService {
  
    static List<Employee> getAllEmployees() {
      // TODO Auto-generated method stub
      return null;
    }
}

主要

public static void main(String[] args) {
  SpringApplication.run(RestWebsiteDataProjectApplication.class, args);
}

@Bean
public WebClient  webClientFromScratch() {
  return WebClient.builder()
    .baseUrl("https://dummy.restapiexample.com/")   
    .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
    .build();
}

【问题讨论】:

    标签: spring-boot httprequest spring-webflux webclient dummy-data


    【解决方案1】:

    Flux 仅在订阅时发出其内容。您没有订阅webClientBuilder() 方法返回的Flux。 您实际上不应该这样做,而是尝试将.block() 添加到您的控制器中,如下所示:

    @Autowired
    WebClientApp webClientApp;
    
    @GetMapping("/consume")
    public String getEmployee(Model model) {
      model.addAttribute("listEmployees", empServiceImpl.getAllEmployees());
      model.addAttribute("listemps", webClientApp.webClientBuilder().block());
      return "index";
    }
    

    如果可行,请考虑重新编写代码,因为在使用 Spring WebFlux(反应式编程)时,您应该始终处理 MonoFlux,以便充分利用反应式堆栈。

    【讨论】:

    • Dias 尝试添加 .block() 但没有成功。当我第一次写出来时,我尝试使用 Flux 但 Eclipse 抛出错误并给出了使用字符串的建议。我最初对 Flux 也是这么想的。不知道如何去做而不会再次引发错误。我在我的网络客户端中使用了通量。我应该专门重写哪个其他地方来使用助焊剂?
    • 说实话不太确定,因为您正在混合使用 Spring MVC 和 Spring WebFlux,我不完全确定这是可能的。
    猜你喜欢
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多