Assertion vs If in the controller

What is the best way to check the value that was passed to the controller?

Is it approved or if?

if this is an example url ....

I would like to check if 1 and 2 is a number and if it is zero. For example, if the user changed the URL to

We use approval in our company. And I just think what happens if it's already in production mode and assert is disabled.

Someone let me figure it out.


source to share

3 answers


intended as a debugging tool for checking conditions under your control. If it works, it indicates an error in the code. Checking the supplied user login is not in this category, instead it is a function of the correct programs. Thus, you must use if statements to validate user input.



Checks are mainly used to check preconditions.

It is like if the given prerequisites are not a mate roll AssertionError

. Testing preconditions in this way is good practice in test environments. Where we can make sure that all prerequisites are helpers and satisfied.

In a production environment, if debug mode hasn't been activated yet, it certainly won't hurt. this statement will act as if it were commented out.

In your case, if you just want to check what value is accepted at the controller level, use if.

but if you want to check this as a precondition and then you want to throw an error if those preconditions are not met and ideally in debug mode . (AssertionError.) Then you can use assertions.



Use the following logic instead of using assert.

@RequestMapping(value = " /read/{var1}/{var2}", method=RequestMethod.GET)
public String read(@PathVariable Integer var1,@PathVariable Integer var2){
     if (var1 ==null || var2 == null) {
        // do something
     return "something";


I assumed you are using spring mvc.



All Articles