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