有没有一种比在Spring Boot中执行空值检查更高效处理补丁请求更新的方法?

huangapple go评论83阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年4月5日 18:01:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61040888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定