0

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.

4
  • What problem are you having doing this? Commented Sep 4, 2021 at 7:23
  • @tgdavies, As we are not mentioning any parameter (i mean key value pair), How to read that data ("dynamicdata")? Commented Sep 4, 2021 at 7:27
  • What is the content of dynamicdata? And why aren't you using key value pairs? Commented Sep 4, 2021 at 7:44
  • Actually this request is coming from a device. And they are going to prepare the URL like this. myservices.com/api/sampleServce?<dynamicdata> Example myservices.com/api/sampleServce?$999900&124434&234* Commented Sep 4, 2021 at 7:48

4 Answers 4

4

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"
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Actually this request is coming from a device. And they are going to prepare the URL like this. myservices.com/api/sampleServce?<dynamicdata>
You can't do this. This is an invalid REST request.
0

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

Comments

0

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();
}

Comments

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.