简体   繁体   中英

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

It is landed into Spring MVC's below controller method -

@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.

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 . Solution is simple if I add @RequestParams("visitUuid") it works.

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.

No, the appropriate mapping annotation ( @RequestParam , @PathVariable , @ModelAttribute , or @RequestBody ) is required. 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.

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 .

You can use below either options

@RequestParams(value = "visitUuid" ,  defaultValue = "yourDefaultValue")

//Or

@RequestParams(value = "visitUuid" ,  required=false)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM