Detailed usage of @Nullable annotation

background

Recently, I found that the code generator (entity, dao, service, controller, vue) I wrote before was a bit buggy. When the Service layer judged empty, some conditions were missing. So I added it, and then my colleague asked me to post the @Nullable annotation in the code, but I don’t know how to use it? The brain is a good thing, you can check whether you have it or not! Where is the advertisement?

At first, did you not understand such a simple common annotation?

usage

@Nullable can be used on methods, properties, and parameters. The corresponding meanings are as follows:

Method: Indicates that the return value can be empty

Attribute: Indicates that the attribute value can be empty

Parameter: Indicates that the parameter value can be empty

used in the method

The return value of the method can be empty, and the specific usage is shown in the following code:

@Nullable
public ApiResult upload(@NotNull(message = "上传参数不能为空") @RequestParam("file") MultipartFile[] file) throws BaseException {
    ApiResult apiResult = new ApiResult();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    String format = simpleDateFormat.format(new Date());
    String realPath = filePath + File.separator + format;
    String returnPath = format;
    File targetFile = new File(realPath);
    if (!targetFile.exists()){
        targetFile.mkdirs();
    }
}

Using annotations in the method method can indicate that the return value of this method can be empty. It's that simple.

used on parameters

The parameter can be empty, and the specific usage is shown in the following code:

private void checkUser(String fansid, String openid, @Nullable String op) throws BaseException{
    Consumer consumer = consumerService.selectByPrimaryKey(fansid);
    if (consumer == null) {
        throw new ParamException("用户不存在");
    }
    Consumer consumer1 = consumerService.selectByPrimaryKey(openid);
    if(consumer1 == null){
        throw new ParamException("被关注者信息异常");
    }
}

The method used on parameters is also very simple, that is, add a @Nullable annotation in front of the parameter, so that the parameter can be marked as empty.

used on attributes

The attribute can be empty, the specific reference code is as follows:

@Validated
@RestController
@RequestMapping("miniapi/follow")
public class FollowController extends BaseController {
    @Nullable
    private String isTime;
    
    @Autowired
    private FollowService followService;
    @Autowired
    private ConsumerService consumerService;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
  }  

As can be seen from the part of the code we solved above, the @Nullable annotation is marked above the property isTime in this part of the code, indicating that the isTime property can be empty.

The above is a real case in our own use, so is there any reference case in our usual reference to the third-party package structure?

Use cases in the Spring toolkit source code

This method is used in the judgment empty method in org.springframework.util.StringUtils.

This is the example we are using on the parameter:

public static boolean isEmpty(@Nullable Object str) {
   return (str == null || "".equals(str));
}

Well, today I have chatted about the use of @Nullable. Friends are welcome to leave a message and exchange.

I also hope that everyone will pay attention to my "coder trainee"

おすすめ

転載: blog.csdn.net/ybb_ymm/article/details/128660507