Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to perform parameter verification gracefully by SpringBoot

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to gracefully check the parameters of SpringBoot. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The validity of parameters directly affects the security and performance of the program, so it is necessary to detect whether the object is null, whether it is blank, whether it is initialized and so on.

Let's first look at how SpringBoot verifies the parameters.

Verification always comes first.

Let's analyze the Assert class again. Assert is actually a very simple utility class.

The Assert class, as a utility class, should not be instantiated, so it is decorated with abstract. Yes, this is the interview question often encountered, and the abstract class cannot be instantiated.

In addition, decorating constructors with private can also prevent classes from being instantiated, which is one of the common programming techniques.

The Assert class is widely used in SpringBoot projects.

Why use the Assert class? Because using Assert can make the code more concise. Three lines of code become one line of code, and the Controller class does not need to rely on a specific Exception class, less dependent on one kind of Exception, and the import statement is reduced by one line.

@ RequestMapping ("edit") @ RequiresPermissions (value = "fsdd-2") public String edit (@ RequestParam @ NonNull Long id, Model model) {Classify classify = classifyService.findById (id); / * if (classify = = null) {throw new EntityNotFoundException ("modified category cannot be empty" + id);} * / Assert.notNullEntity (classify, "modified category cannot be empty" + id) Model.addAttribute (classify, classify); return "manage/ClassifyController/edit";}

When modifying a record, the id parameter is required, and id cannot be empty, and entity objects queried according to id cannot be empty. Of course, you must also be in the login state and have the relevant role permissions.

At this point, we just verified that the parameter was legal and threw an exception, but did not tell the user what was going on in the background.

Using @ ControllerAdvice and @ ExceptionHandler, you can catch different types of errors and give feedback to foreground users according to the type of error.

About how SpringBoot gracefully carries on the parameter verification to share here, hoped that the above content can have the certain help to everybody, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report