我在java中有以下记录。
@Introspected
public record ProductViewModel
(
String id,
@NotBlank(message = ConstantValues.TAG_REQUIRED)
String title
)
在记录执行后BeanIntrospection.getIntrospection
,我可以访问所有 POJO 属性,如下所示
final BeanIntrospection<ProductViewModel> introspection = BeanIntrospection.getIntrospection(ProductViewModel.class);
String[] productViewModel = introspection.getPropertyNames();
productViewModel
拥有记录的所有属性
现在我想为键构造 MultipartBody 作为记录属性,例如
Arrays.stream(productViewModel).forEach(item -> {
requestBody.addPart(
item,
'value');
});
上面的代码将所有属性名称作为键添加到 requestBody 中,我如何设置从这个 API 端点接收到的值。下面是完整的代码。
public Maybe<HttpResponse<?>> post(Publisher<CompletedFileUpload> files, ProductViewModel model) {
final BeanIntrospection<ProductViewModel> introspection = BeanIntrospection.getIntrospection(ProductViewModel.class);
String[] productViewModel = introspection.getPropertyNames();
return Flowable.fromPublisher(files)
.collect(MultipartBody::builder, (requestBody, file) -> {
requestBody
.addPart("file", file.getFilename(), MediaType.MULTIPART_FORM_DATA_TYPE, file.getBytes());
})
.flatMapMaybe(requestBody -> {
Arrays.stream(productViewModel).forEach(item -> {
requestBody.addPart(
item,
Optional.ofNullable(introspection.getValue(item)).orElse(null).toString());
});
return iProductClient.post(requestBody.build());
});
}
这些值在模型中 post 方法的参数中。现在如何将值放入Arrays.stream(productViewModel).forEach()