How do I validate that the @RequestParams are not empty?
Asked Answered
C

3

23

I have a calculator service that gets the operation type, num1 and num2 from the user. I need to validate that the user actually inputs these values and doesn't just leave it blank.

@RequestMapping(value = "/calculate")
@ResponseBody
public CalculationResult calculate(@RequestParam(name = "op") String operation, @RequestParam(name = "num1") Double num1, @RequestParam(name = "num2") Double num2) {
    System.out.print("Operation:" + operation);
    Double calculate = calculatorService.calculate(operation, num1, num2);
    return new CalculationResult(calculate);
}

I have an Integration test that I need to make pass as it is currently failing with error:

{\"timestamp\":1488875777084,\"status\":400,\"error\":\"Bad Request\",\"exception\":\"org.springframework.web.method.annotation.MethodArgumentTypeMismatchException\",\"message\":\"Failed to convert value of type 'java.lang.String' to required type 'java.lang.Double';

Below is my Test Case:

@Test
public void validates_all_parameters_are_set() throws Exception {
    ResponseEntity<String> response = template.getForEntity( "/calculate?op=&num1=&num2=",
            String.class);
    assertThat(response.getStatusCode(), equalTo(HttpStatus.BAD_REQUEST));
    assertThat(response.getBody(), equalTo("{\"error\":\"At least one parameter is invalid or not supplied\"}"));
}

I don't know how to validate this.

Cockatrice answered 7/3, 2017 at 8:53 Comment(4)
you are passing num2 as value for num1; that should be /calculate?op=&num1=&num2=Ruche
Thank you, when I fix that I get a "java.lang.AssertionError: Expected: <400> but: was <200>"Cockatrice
simple way is wrap all prams in bean and use @valid. Check this herePleurodynia
Or you can use this way If want params as is.Pleurodynia
R
4

You do not check the values up to now; you could change your code to:

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@RequestMapping(value = "/calculate")
@ResponseBody
public ResponseEntity<CalculationResult> calculate(@RequestParam(name = "op") String operation, 
    @RequestParam(name = "num1") Double num1, 
    @RequestParam(name = "num2") Double num2) {

    if(null == op || null == num1 || null == num2) {
        throw new IllegalArgumentException("{\"error\":\"At least one parameter is invalid or not supplied\"}")
    }

    System.out.print("Operation:" + operation);
    Double calculate = calculatorService.calculate(operation, num1, num2);

    return new ResponseEntity<>(new CalculationResult(calculate), HttpStatus.OK);
}    

@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public final String exceptionHandlerIllegalArgumentException(final IllegalArgumentException e) {
    return '"' + e.getMessage() + '"';
}
Ruche answered 7/3, 2017 at 10:22 Comment(5)
When I add this, and run the test I mentioned in the question, the test fails with: "java.lang.AssertionError: Expected: "{\"error\":\"At least one parameter is invalid or not supplied\"}" but: was null"Cockatrice
your service code is returning a CalculationResult object, but your test is expecting a String; this is inconsistent. I changed my answer so that an ExceptionHandler is used. I did not compile it; hope I didn't make any typosRuche
There should be a grey checkmark below the up/downvoting arrows for the answer, you have to click that to accept an answer.Ruche
Why we are checking for null inside method implementation if(null == op || null == num1 || null == num2), when we can handle it using JSR bean validation?Bixby
How do we check if an array is NULL?Moussaka
B
47

I answered similar problem long before here which you can follow to write your test as well , as follows:

@Validated
public class CalculationController {

    @RequestMapping(value = "/calculate")
    @ResponseBody
    public CalculationResult calculate(
            @Valid @NotBlank @RequestParam(name = "op") String operation,
            @Valid @NotNull @RequestParam(name = "num1") Double num1,
            @Valid @NotNull @RequestParam(name = "num2") Double num2) {
        System.out.print("Operation:" + operation);
        Double calculate = calculatorService.calculate(operation, num1, num2);
        return new CalculationResult(calculate);
    }
}

Corresponding @Test should be modified to test for an array of "may not be null" message, as:

@Test
public void validates_all_parameters_are_set() throws Exception {
    ResponseEntity<String> response = template.getForEntity( "/calculate?op=&num1=&num2=",
                String.class);
    assertThat(response.getStatusCode(), equalTo(HttpStatus.BAD_REQUEST));
    assertThat(response.getBody(), equalTo("{\"error\":[\"may not be null\",\"may not be null\"]}"));
}
Bixby answered 7/3, 2017 at 15:44 Comment(2)
This does not work. With these annotations one can still send a request with empty parameter value.Propellant
For this solution to work, you have to explicitly add: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>Leadwort
R
4

You do not check the values up to now; you could change your code to:

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@RequestMapping(value = "/calculate")
@ResponseBody
public ResponseEntity<CalculationResult> calculate(@RequestParam(name = "op") String operation, 
    @RequestParam(name = "num1") Double num1, 
    @RequestParam(name = "num2") Double num2) {

    if(null == op || null == num1 || null == num2) {
        throw new IllegalArgumentException("{\"error\":\"At least one parameter is invalid or not supplied\"}")
    }

    System.out.print("Operation:" + operation);
    Double calculate = calculatorService.calculate(operation, num1, num2);

    return new ResponseEntity<>(new CalculationResult(calculate), HttpStatus.OK);
}    

@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public final String exceptionHandlerIllegalArgumentException(final IllegalArgumentException e) {
    return '"' + e.getMessage() + '"';
}
Ruche answered 7/3, 2017 at 10:22 Comment(5)
When I add this, and run the test I mentioned in the question, the test fails with: "java.lang.AssertionError: Expected: "{\"error\":\"At least one parameter is invalid or not supplied\"}" but: was null"Cockatrice
your service code is returning a CalculationResult object, but your test is expecting a String; this is inconsistent. I changed my answer so that an ExceptionHandler is used. I did not compile it; hope I didn't make any typosRuche
There should be a grey checkmark below the up/downvoting arrows for the answer, you have to click that to accept an answer.Ruche
Why we are checking for null inside method implementation if(null == op || null == num1 || null == num2), when we can handle it using JSR bean validation?Bixby
How do we check if an array is NULL?Moussaka
L
-1
@RestController
@RequestMapping("/")
@Validated
public class Controller {
    @GetMapping("/endpoint")
    public String getEndpoint(@RequestParam @NotBlank String name) {}
}
Leverage answered 28/2, 2021 at 14:30 Comment(1)
Welcome to SO! While your answer may be correct, you should always try to give it some context and explain why it may be the solution.Mode

© 2022 - 2024 — McMap. All rights reserved.