Butchi Reddy Velagala
Butchi Reddy Velagala

Reputation: 854

How to get data from query string in Java Spring boot REST

We have a requirement to read data after '?' in service-url in Spring boot REST API.

For example, We exposed a service called sampleService and GET URL for this is

http://www.myservices.com/api/sampleServce

And clients will pass the data as http://www.myservices.com/api/sampleServce?dynamicdata

So we have to read that "dynamicdata" in my sample service and process.

Please let me know the possibilities.

Upvotes: 0

Views: 8803

Answers (4)

Shai Givati
Shai Givati

Reputation: 1156

You can add an HttpServletRequest object to your mapped method signature:

@RequestMapping("/api/sampleServce")
public Object sampleServce (HttpServletRequest request) {
    //print everything after the question mark: 
    System.out.println("queryString: " + request.getQueryString());
    
    //print key value pairs: 
    Enumeration<String> params = request.getParameterNames();
    while(params.hasMoreElements()){
        String paramName = params.nextElement();
        String paramValue = request.getParameter(paramName);
        System.out.println("name: " + paramName);
        System.out.println("value: " + paramValue);
    }
    return request.getQueryString();
}

Upvotes: 0

pcsutar
pcsutar

Reputation: 1821

Accept dynamic data through request param:

@GetMapping("/api/sampleServce")
public void test(@RequestParam Map<String, String> dynamicdata) {
    System.out.println(dynamicdata.keySet()); //http://localhost:9001/dag-backend/api/sampleServce?test&test11

    Optional<String> data = dynamicdata.keySet().stream().findFirst();
    String value = data.isPresent() ? data.get() : null;
    System.out.println(value); //http://localhost:9001/dag-backend/api/sampleServce?test
}

URLs:

http://www.myservices.com/api/sampleServce?dynamicdata http://www.myservices.com/api/sampleServce?dynamicdata&12344&1212 http://www.myservices.com/api/sampleServce?$999900&124434&234

Upvotes: 0

kakashiOfSharingan
kakashiOfSharingan

Reputation: 161

GET: http://localhost:8080/api/foos?id=abc here the query string is id=abc . Now to extract the value of id, you can use the code something like this.

@GetMapping("/api/foos")
@ResponseBody
public String getFoos(@RequestParam String id) {
    return "ID: " + id;
}

GET: http://www.myservices.com/api/sampleServce?dynamicdata is incorrect. Either it should be http://www.myservices.com/api/sampleServce/dynamicdata (PathVariable) or http://www.myservices.com/api/sampleServce?title=dynamicdata (RequestParam)

GET: http://www.myservices.com/api/sampleServce/dynamicdata to extract dynamicdata, you can use code something like

@GetMapping("/api/sampleServce/{id}")
@ResponseBody
public String getFooById(@PathVariable String id) {
    return "ID: " + id; // here id = "dynamicdata"
}

GET: http://www.myservices.com/api/sampleServce?title=dynamicdata to extract title, you can use code something like

@GetMapping("/api/sampleServce")
@ResponseBody
public String getFoos(@RequestParam String title) { 
    return "title: " + title; // title="dynamicdata"
}

Upvotes: 4

lkatiforis
lkatiforis

Reputation: 6255

dynamicdata is path param, it cannot be placed after ?. It should be something like this:

http://www.myservices.com/api/dynamicdata/sampleServce

Check when and how to use query or path parameters

Upvotes: 0

Related Questions