[英]Spring MVC @RequestParams
This is the url : https://192.168.33.10/openmrs/ws/rest/v1/bahmnicore/bahmniencounter/search?includeAll=false&patientUuid=210d0739-7937-4fb7-8f53-752f393cb4b7&visitUuid=c1c26908-3f10-11e4-adec-0800271c1b75 这是网址: https : //192.168.33.10/openmrs/ws/rest/v1/bahmnicore/bahmniencounter/search? includeAll = false & PatientUuid = 210d0739-7937-4fb7-8f53-752f393cb4b7 & visitUuid = c1c26908-3f10-11e4-adec- 0800271c1b75
It is landed into Spring MVC's below controller method - 它被放入Spring MVC的以下控制器方法中-
@RequestMapping(method = RequestMethod.GET, value = "search")
@ResponseBody
public List<BahmniDiagnosisRequest> search(@RequestParam("patientUuid") String patientUuid, @RequestParam(value = "fromDate", required = false) String date, String visitUuid) throws Exception {
if (visitUuid != null) {
return bahmniDiagnosisService.getBahmniDiagnosisByPatientAndVisit(patientUuid, visitUuid);
} else {
return bahmniDiagnosisService.getBahmniDiagnosisByPatientAndDate(patientUuid, date);
}
}
If you see the spring annotation @Request
Params is not present for the visitUuid
parameter. 如果您看到visitUuid
注释,则visitUuid
参数不存在@Request
参数。
The code above used to work even though the parameter was not present. 即使没有该参数,上面的代码也可以正常工作。 But recently it throws an Exception, there is no request parameter for visitUuid
. 但是最近它抛出一个Exception,没有visitUuid
请求参数。 Solution is simple if I add @RequestParams("visitUuid")
it works. 如果我添加@RequestParams("visitUuid")
则解决方案很简单。
But my question is can parameter in a GET request get mapped to appropriate variables on the Controller code even if we don't have a @RequestParamter
mapping. 但是我的问题是,即使我们没有@RequestParamter
映射,GET请求中的参数能否映射到Controller代码上的适当变量。
No, the appropriate mapping annotation ( @RequestParam
, @PathVariable
, @ModelAttribute
, or @RequestBody
) is required. 否,则适当的映射注解( @RequestParam
, @PathVariable
, @ModelAttribute
,或@RequestBody
)是必需的。 The name of the parameter can be omitted if you compile with debug symbols (which is the usual default), but you have to tell Spring where to look for the value. 如果使用调试符号进行编译(通常是默认设置),则可以省略参数的名称 ,但是必须告诉Spring在哪里寻找该值。
Note also that Spring has an impressive type-conversion system, and if your service is capable of (or can be refactored to) taking a UUID
parameter instead of a String
, you can use @RequestParameter UUID patientUuid
. 还要注意,Spring有一个令人印象深刻的类型转换系统,如果您的服务能够(或可以重构)采用UUID
参数而不是String
,则可以使用@RequestParameter UUID patientUuid
。
You can use below either options 您可以在以下两个选项中使用
@RequestParams(value = "visitUuid" , defaultValue = "yourDefaultValue")
//Or
@RequestParams(value = "visitUuid" , required=false)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.