In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 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 understand Spring-Assert. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
The back-end service layer needs to check the validity of the data submitted by the front-end form after receiving it. If the form data is illegal, the request will be rejected. As follows:
If (name = = null | | name.equls (")) {throw new IllegalArgumentException (" wrong parameter! ");}
Using the Assert assertion class can simplify the code for method input parameter detection, such as InputStream getData (String file). After applying the Assert assertion class, the code can be simplified to the following form:
Assert.hasText ((name, "wrong parameter!")
Code similar to the above detection method input parameters is very common, but it is not a good idea to write the detection logic by hand in each method. Read the Spring source code and you will find that Spring uses an org.springframework.util.Assert generic class to accomplish this task.
Assert is translated as "assertion" in Chinese, and readers who have used JUnit are familiar with the concept. It concludes that an actual run value is the same as expected, or an exception is thrown. Spring borrows this concept from the detection of method input parameters, and the Assert class it provides has many methods to assert method input parameters according to rules, which can meet the requirements of most method input parameters detection. These assertion methods throw an IllegalArgumentException when the input parameter does not meet the requirements. Next, let's take a look at the common assertion methods in the Assert class:
Assertion method description
NotNull (Object object)
When object is null, the notNull (Object object, String message) method allows you to customize the exception information through message. The method contrary to the assertion rule of the notNull () method is isNull (Object object) / isNull (Object object, String message), which requires that the input parameter must be null
IsTrue (boolean expression) / isTrue (boolean expression, String message)
When expression does not throw an exception for true
NotEmpty (Collection collection) / notEmpty (Collection collection, String message)
Throws an exception when the collection does not contain elements. NotEmpty (Map map) / notEmpty (Map map, String message) and notEmpty (Object [] array, String message) / notEmpty (Object [] array, String message) respectively judge the input parameters of Map and Object [] types.
HasLength (String text) / hasLength (String text, String message)
Throw an exception when text is null or length is 0
HasText (String text) / hasText (String text, String message)
Text cannot be null and must contain at least one character that is not a space, otherwise an exception is thrown
IsInstanceOf (Class clazz, Object obj) / isInstanceOf (Class type, Object obj, String message)
If obj cannot be correctly modeled as the class specified by clazz, an exception will be thrown
IsAssignable (Class superType, Class subType) / isAssignable (Class superType, Class subType, String message)
SubType must match superType by type, otherwise an exception will be thrown
Chestnut public class AssertTest {public static void main (String [] args) {Assert.notNull (null, "object is empty"); Assert.isNull (new Object (), "not empty"); Assert.isTrue (1 empty 2, "unequal"); Assert.isTrue (1 empty 2); Assert.notEmpty (new String [] {}, "empty 1"); Assert.notEmpty (new HashMap (), "empty 2") Assert.hasLength ("," length is 0 "); Assert.hasText (", "empty"); Assert.isInstanceOf (String.class,new Object (), "Type mismatch"); Assert.isAssignable (Object.class, String.class, "pass"); Assert.isAssignable (String.class,Object.class, "No") }} on how to understand Spring-Assert to share here, I hope that the above content can be of some help to you, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.