英文:
Is there a more efficient way to handle patch request updates other than performing null-checks in spring boot?
问题
有没有比最初执行空值检查更好的更新方法?
@PatchMapping("/base/uri/{id}")
public void updateModel(@Valid @RequestBody Model newModel, @PathVariable Long id) {
    modelRepository.findById(id).map(model -> {
        if (newModel.getParam1() != null) model.setParam1(newModel.getParam1());
        if (newModel.getParam2() != null) model.setParam2(newModel.getParam2());
        if (newModel.getParam3() != null) model.setParam3(newModel.getParam3());
        if (newModel.getParam4() != null) model.setParam4(newModel.getParam4());
        ...
        modelRepository.save(model);
    }).orElseThrow(() -> new MyNotFoundException());
}
英文:
Is there a better approach to updating other than initially performing null-checks?
@PatchMapping("/base/uri/{id}")
public void updateModel(@Valid @RequestBody Model newModel, @Pathvariable Long id) {
    modelRepository.findById(id).map(model -> {
        if (newModel.getParam1() != null) model.setParam1(newModel.getParam1());
        if (newModel.getParam2() != null) model.setParam1(newModel.getParam2());
        if (newModel.getParam3() != null) model.setParam1(newModel.getParam3());
        if (newModel.getParam4() != null) model.setParam1(newModel.getParam4());
        ...
        modelRespository.save(model);
    }).orElseThrow(() -> MyNotFoundException());
}
答案1
得分: 2
你可以使用Spring框架的BeanUtils来添加要忽略的属性(在这种情况下是空属性),示例如下:
import java.beans.FeatureDescriptor;
import java.util.stream.Stream;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
@PatchMapping("/base/uri/{id}")
public void updateModel(@Valid @RequestBody Model newModel, @PathVariable Long id) {
    modelRepository.findById(id).map(model -> {
        String[] nulls = getNullPropertyNames(newModel);
        // 将newModel复制到model,避免在"nulls"中列出的属性
        BeanUtils.copyProperties(newModel, model, nulls);
        modelRepository.save(model);
    }).orElseThrow(() -> MyNotFoundException());
}
public static String[] getNullPropertyNames(Object source) {
    final BeanWrapper wrappedSource = new BeanWrapperImpl(source);
    return Stream.of(wrappedSource.getPropertyDescriptors())
        .map(FeatureDescriptor::getName)
        .filter(propertyName -> wrappedSource.getPropertyValue(propertyName) == null)
        .toArray(String[]::new);
}
英文:
You can use the BeanUtils of the spring framework adding the properties to ignore ( which in this case are the null ones ) like this:
import java.beans.FeatureDescriptor;
import java.util.stream.Stream;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
@PatchMapping("/base/uri/{id}")
public void updateModel(@Valid @RequestBody Model newModel, @Pathvariable Long id) {
modelRepository.findById(id).map(model -> {
       String[] nulls = getNullPropertyNames(newModel);
       // copy the newModel into model 
       // avoiding the properties listed in "nulls"
	   BeanUtils.copyProperties(newModel, model, nulls);
       modelRespository.save(model);
   }).orElseThrow(() -> MyNotFoundException());
}
public static String[] getNullPropertyNames(Object source) {
    final BeanWrapper wrappedSource = new BeanWrapperImpl(source);
    return Stream.of(wrappedSource.getPropertyDescriptors())
        .map(FeatureDescriptor::getName)
        .filter(propertyName -> wrappedSource.getPropertyValue(propertyName) == null)
        .toArray(String[]::new);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论