我一直在尝试搜索是否可以通过function video_get_post_meta($object, $field_name, $request){
return get_post_meta($object['id'], $field_name, true);
}
function video_update_post_meta($value, $object, $field_name){
return update_post_meta($object['id'], $field_name, $value);
}
add_action('rest_api_init', function(){
register_rest_field('post', 'length',
array(
'get_callback' => 'video_get_post_meta',
'update_callback' => 'video_update_post_meta',
'schema' => null
)
);
register_rest_field('post', 'file_name',
array(
'get_callback' => 'video_get_post_meta',
'update_callback' => 'video_update_post_meta',
'schema' => null
)
);
register_rest_field('post', 'thumbnail',
array(
'get_callback' => 'video_get_post_meta',
'update_callback' => 'video_update_post_meta',
'schema' => null
)
);
register_rest_field('post', 'video_url',
array(
'get_callback' => 'video_get_post_meta',
'update_callback' => 'video_update_post_meta',
'schema' => null
)
);
});
添加一些自定义字段来获得'嵌套'JSON响应
我有这段代码:
"length": "100",
"file_name": "video_test.mp4",
"thumbnail_": "https://example.com/the_thumb.jpg",
"video_url": "https://example.com/media/20180228.mp4",
这给我一个结果:
"video": {
"length": "100",
"file_name": "video_test.mp4",
"thumbnail_": "https://example.com/the_thumb.jpg",
"video_url": "https://example.com/media/20180228.mp4"
},
但我想知道是否有可能采用相应的响应格式:
@GetMapping("/users")
public Mono<User> getPopulation() {
return userRepository.findOldest()
.flatMap(user -> { // process the response from DB
user.setTheOldest(true);
return userRepository.save(user);
})
.map(user -> {...}); // another processing
}