0

I am missing something here. I am attempting to pull information using Spring Boot WebClient from a Dummy Api that's an Http request. I am not getting any info pulled when I go into postman.

Thanks for any insight you can give me. I am still very new to coding and self-taught.

Here's my employee controller:

@Autowired
WebClientApp webClientApp;

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

Web Client

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);
}

Employee

@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;
}

Service

@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

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

Main

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();
}
João Dias
  • 16,277
  • 6
  • 33
  • 45

1 Answers1

0

Flux only emits its content when it is subscribed. You are not subscribing to the Flux returned by the webClientBuilder() method. You shouldn't really do this, but try adding .block() to your Controller as follows:

@Autowired
WebClientApp webClientApp;

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

If this works, please consider reworking your code because while working with Spring WebFlux (reactive programming) you should always deal with Mono and Flux so that you can take full advantage of the reactive stack.

João Dias
  • 16,277
  • 6
  • 33
  • 45
  • Dias Tried adding .block() but no go. When I first wrote it out, I tried to use Flux but Eclipse threw up errors and gave suggestions to go to string. I originally thought the same with Flux. Not sure how to go about it without it throwing up errors again. I use flux in my web client. Which other spot specifically should I rewrite to use flux? – Julie Gladden Nov 12 '21 at 01:25
  • Not really sure to be honest, because you are mixing Spring MVC and Spring WebFlux and I am not entirely sure that this is possible. – João Dias Nov 12 '21 at 09:07