如何验证@RequestParams是否为空?

时间:2017-03-07 08:53:04

标签: java spring-boot junit4 bean-validation jsr

我有一个计算器服务,可以从用户那里获取操作类型num1和num2。我需要验证用户是否实际输入了这些值,并且不要将其留空。

@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);
}

我有一个集成测试,我需要传递,因为它当前失败并出现错误:

  

{\"时间戳\":1488875777084,\"状态\":400,\"错误\":\"差   请求\" \"例外\":\" org.springframework.web.method.annotation.MethodArgumentTypeMismatchException \" \"消息\&# 34;:\"失败   转换类型' java.lang.String'的值要求的类型   ' java.lang.Double中&#39 ;;

以下是我的测试用例:

@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\"}"));
}

我不知道如何验证这一点。

3 个答案:

答案 0 :(得分:14)

我在here之前很久就回答了类似的问题,您也可以按照以下方式编写测试,如下所示:

@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);
    }
}

应修改相应的@Test以测试“可能不为空”消息的数组,如:

@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\"]}"));
}

答案 1 :(得分:2)

到目前为止,您还没有检查这些值;您可以将代码更改为:

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() + '"';
}

答案 2 :(得分:0)

@RestController
@RequestMapping("/")
@Validated
public class Controller {
    @GetMapping("/endpoint")
    public String getEndpoint(@RequestParam @NotBlank String name) {}
}