0

[![In this Image I have to search person by based on dropdown selection ][1]][1]

I want to pass this selections through @Requestparam, I have multiple keynames but I want to pass one key at a time So, I want to use only One String whatever the parameter comes from the request it should fetch the key value in One String

I don't want like this @RequestParam(value = "pname", required = false) String pName, @RequestParam(value = "ssn", required = false) String sSN)

I want Some thing like this (i.e multiple keyvalues with single String Variable)

    public List<Patient> getPatientListLike(@RequestParam(required = false) String searchString)

6 Answers 6

4

You may use something like this

@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]}

Please refer here for details https://www.baeldung.com/spring-request-param

Sign up to request clarification or add additional context in comments.

Comments

0

You may create searchString as ssn=ssnValue (ssnValue which user select on screen) in UI code and pass it to url.

Comments

0

You can use HttpServletRequest request to get query string

public List<Patient> getPatientListLike(HttpServletRequest request) {

    String searchString = request.getQueryString();

}

Comments

0

I think you can modify the method to:

public List<Patient> getPatientListLike(
    @RequestParam(required = false) String searchString,
    @RequestParam(required = false) String searchType
)

searchString is presentation the textbox and searchType is presentation drop-down value.

Comments

0

You can simply replace the String request param with a Map<String, String> request param. resource

public List<Patient> getPatientListLike(@RequestParam(required = false) Map<String, String> searchParams)

Comments

0

RequestParam also maps to object. Do not reinvent the way requests are processed and always follow best API design principles. Pass the query parameters as they are meant to, where all key value pairs are separated with ampersand.

GET /search?pname=name&ssn=123

You may have to use constructor with Jackson annotations to help marshalling it. I prefer public fields on data structures.

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

@GetMapping("search")
public List<Patient> getPatientListLike(SearchParameters parameters)

4 Comments

@GetMapping(value = "/list/search") public List<Patient> getPatientListLike(@RequestParam(required = false) SearchParam parameters){ List<Patient> pList = patientServices.getPatientListLike1(parameters); return pList; } i'm passing /search?pname=somename but it prints null in logs
Make the fields private and add public getters and setters then instead. Alternative solution would be to use @JsonCreator on the constructor and marking parameters with @JsonProperty annotations.
public class 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; } } check my service Implementation @Override public List<Patient> getPatientListLike1(SearchParam parameters) { List<Patient> patients; patients = patientRepository.findByName(parameters.getPname()); return patients; } Again I'm getting same null value
Remove the @RequestParam annotation from your mapping. This cannot be used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.