简体   繁体   English

春季MVC @RequestParams

[英]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.

相关问题 @JsonCreator 不适用于 Spring MVC 中的 @RequestParams - @JsonCreator not working for @RequestParams in Spring MVC 使用AngularJS将URL中的RequestParams传递给Spring MVC后端 - Passing RequestParams in the URL with AngularJS to Spring MVC backend 春季-直接从@RequestParams填充@ModelAttributes - spring - populating @ModelAttributes directly from @RequestParams Spring Rest - 将嵌套的 DTO object 绑定到 @RequestParams - Spring Rest - bind nested DTO object to @RequestParams 有没有办法缩短 Spring 引导中的 boolean RequestParams ? - Is there a way to shorten boolean RequestParams in Spring Boot? 如何在 Spring Boot REST 中处理未知数量的 RequestParams? - How do I handle an unknown number of RequestParams in Spring Boot REST? 如何在 Spring Data Jpa 中过滤作为 RequestParams 传递的多个参数? - How to have filtering of multiple parameters passed as RequestParams in Spring Data Jpa? 在 Spring Boot 2 中,我如何接受 LocalDates 作为 RequestParams - In Spring Boot 2 how can I accept LocalDates as RequestParams Spring REST API 多个 RequestParams 与控制器实现 - Spring REST API multiple RequestParams vs controller implementation 使用SPRING BOOT时如何在RequestParams中保留ISO 8601日期格式 - How to retain ISO 8601 date format in RequestParams when using SPRING BOOT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM