Java 使用单个字符串变量在@Requestparam中传递多个键

Java 使用单个字符串变量在@Requestparam中传递多个键,java,spring,spring-boot,spring-mvc,spring-restcontroller,Java,Spring,Spring Boot,Spring Mvc,Spring Restcontroller,[![在此图像中,我必须根据下拉选择搜索人员][1][1] 我想通过@Requestparam传递这些选择,我有多个键名,但我想一次传递一个键,因此,我只想使用一个字符串无论请求中的参数是什么,它都应该获取一个字符串中的键值 我不想这样 @RequestParam(value=“pname”,required=false)字符串pname,@RequestParam(value=“ssn”,required=false)字符串ssn) 我想要这样的东西(,即带有单个字符串变量的多个键值) pub

[![在此图像中,我必须根据下拉选择搜索人员][1][1]

我想通过
@Requestparam
传递这些选择,我有多个键名,但我想一次传递一个键,因此,我只想使用一个字符串无论请求中的参数是什么,它都应该获取一个字符串中的键值

我不想这样
@RequestParam(value=“pname”,required=false)字符串pname,@RequestParam(value=“ssn”,required=false)字符串ssn)

我想要这样的东西(,即带有单个字符串变量的多个键值

public List getPatientListLike(@RequestParam(required=false)字符串searchString)

您可以在UI代码中创建搜索字符串作为ssn=ssnValue(用户在屏幕上选择的ssnValue)并将其传递给url。

您可以在UI代码中创建搜索字符串作为ssn=ssnValue(用户在屏幕上选择的ssnValue)并将其传递给url。

您可以使用HttpServletRequest请求获取查询字符串

public List<Patient> getPatientListLike(HttpServletRequest request) {

    String searchString = request.getQueryString();

}
公共列表getPatientListLike(HttpServletRequest){ String searchString=request.getQueryString(); }
您可以使用HttpServletRequest获取查询字符串

public List<Patient> getPatientListLike(HttpServletRequest request) {

    String searchString = request.getQueryString();

}
公共列表getPatientListLike(HttpServletRequest){ String searchString=request.getQueryString(); }
您可以使用类似的方法

@PostMapping("/api/foos")
@ResponseBody
public String updateFoos(@RequestParam Map<String,String> allParams) {
    return "Parameters are " + allParams.entrySet();
}
curl -X POST -F 'name=abc' -F 'id=123' http://localhost:8080/api/foos
-----
Parameters are {[name=abc], [id=123]}
@PostMapping(“/api/foos”)
@应答器
公共字符串updatefos(@RequestParam-Map-allParams){
return“Parameters are”+allParams.entrySet();
}
curl-X POST-F'name=abc'-F'id=123'http://localhost:8080/api/foos
-----
参数为{[name=abc],[id=123]}

有关详细信息,请参阅此处

您可以使用类似的方法

@PostMapping("/api/foos")
@ResponseBody
public String updateFoos(@RequestParam Map<String,String> allParams) {
    return "Parameters are " + allParams.entrySet();
}
curl -X POST -F 'name=abc' -F 'id=123' http://localhost:8080/api/foos
-----
Parameters are {[name=abc], [id=123]}
@PostMapping(“/api/foos”)
@应答器
公共字符串updatefos(@RequestParam-Map-allParams){
return“Parameters are”+allParams.entrySet();
}
curl-X POST-F'name=abc'-F'id=123'http://localhost:8080/api/foos
-----
参数为{[name=abc],[id=123]}

有关详细信息,请参见此处

我认为您可以将该方法修改为:

public List<Patient> getPatientListLike(
    @RequestParam(required = false) String searchString,
    @RequestParam(required = false) String searchType
)
公共列表getPatientListLike(
@RequestParam(必需=false)字符串searchString,
@RequestParam(必需=false)字符串搜索类型
)

searchString
是文本框中的演示文稿,而
searchType
是演示文稿下拉值。

我认为您可以将该方法修改为:

public List<Patient> getPatientListLike(
    @RequestParam(required = false) String searchString,
    @RequestParam(required = false) String searchType
)
公共列表getPatientListLike(
@RequestParam(必需=false)字符串searchString,
@RequestParam(必需=false)字符串搜索类型
)

searchString
是文本框中的表示,而
searchType
是表示下拉值。

RequestParam也映射到对象。不要改变处理请求的方式,始终遵循最佳API设计原则。按原样传递查询参数,其中所有键值对都用符号和分隔

GET /search?pname=name&ssn=123
您可能必须使用带有Jackson注释的构造函数来帮助编组它。我更喜欢数据结构上的公共字段

class SearchParameters {
   private String pname;
   private String ssn;
   // getters-setters
}

@GetMapping("search")
public List<Patient> getPatientListLike(SearchParameters parameters)
类搜索参数{
私有字符串pname;
私有字符串ssn;
//吸气剂二传手
}
@GetMapping(“搜索”)
公共列表getPatientListLike(搜索参数)

请求参数也映射到对象。不要改变处理请求的方式,始终遵循最佳API设计原则。按原样传递查询参数,其中所有键值对都用符号和分隔

GET /search?pname=name&ssn=123
您可能必须使用带有Jackson注释的构造函数来帮助编组它。我更喜欢数据结构上的公共字段

class SearchParameters {
   private String pname;
   private String ssn;
   // getters-setters
}

@GetMapping("search")
public List<Patient> getPatientListLike(SearchParameters parameters)
类搜索参数{
私有字符串pname;
私有字符串ssn;
//吸气剂二传手
}
@GetMapping(“搜索”)
公共列表getPatientListLike(搜索参数)

您只需将
字符串
请求参数替换为
映射
请求参数即可

public List getPatientListLike(@RequestParam(required=false)映射searchParams)

您只需将
字符串
请求参数替换为
映射
请求参数即可

public List getPatientListLike(@RequestParam(required=false)映射searchParams)

@GetMapping(value=“/list/search”)公共列表getPatientListLike(@RequestParam(required=false)SearchParam参数){list pList=patientServices.getPatientListLike1(参数);返回pList;}
我正在传递
/search?pname=somename
但它在日志中打印
null
,将字段设置为私有,然后添加公共getter和setter。另一种解决方案是在构造函数上使用
@JsonCreator
,并使用
@JsonProperty
注释标记参数。
公共类SearchParam{@JsonProperty private String pname;@JsonCreator public SearchParam(String pname){this.pname=pname;}public String getPname(){return pname;}public void setPname(String pname){this.pname=pname;}}
检查我的服务实现
@Override public List getPatientListLike1(SearchParam参数){List patients;patients=patientRepository.findByName(parameters.getPname());return patients;}
我再次得到相同的空值从映射中删除@RequestParam注释。这不能使用。
@GetMapping(value=“/list/search”)公共列表getPatientListLike(@RequestParam(required=false)SearchParam参数){list pList=patientServices.getPatientListLike1(参数);返回pList;}
我正在传递
/search?pname=somename
但它在日志中打印
null
,将字段设置为私有,然后添加公共getter和setter。另一种解决方案是在构造函数上使用
@JsonCreator
,并使用
@JsonProperty
注释标记参数