java requestparams_java – 如何验证@RequestParams不为空?
我有一個計算器服務(wù),從用戶獲取操作類型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);
}
我有一個集成測試,我需要傳遞,因為它當(dāng)前失敗并出現(xiàn)錯誤:
{\”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’;
以下是我的測試用例:
@Test
public void validates_all_parameters_are_set() throws Exception {
ResponseEntity 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\"}"));
}
我不知道如何驗證這一點.
總結(jié)
以上是生活随笔為你收集整理的java requestparams_java – 如何验证@RequestParams不为空?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 启动 Tomcat 日志乱码问题
- 下一篇: 【小程序项目分享】多功能抽签分组系统
