Julie Gladden
Julie Gladden

Reputation: 21

Using Spring Boot WebClient to call a dummy api to postman

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

Upvotes: 0

Views: 907

Answers (1)

Jo&#227;o Dias
Jo&#227;o Dias

Reputation: 17510

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.

Upvotes: 0

Related Questions