Pass parameters to URL placeholders in java - java

I am getting the URL dynamically which has few placeholders. But i need to pass the parameters to the placeholders.
Below is the URL which i am getting dynamically
http://example.com/name/{name}/age/{age}
i need to pass parameters for name and age for the above url. How can we achieve that using java.

I guess you are looking Rest Client to call this URL and get response.
http://example.com/name/{name}/age/{age}
String resourceURL = "http://example.com/name";
ResponseEntity<String> response = restTemplate.getForEntity(resourceURL + "/swarit/age/20", String.class);
I would also suggest to do little research before posting question.

Related

How to get value of query param array in spring boot?

I have this URL
http://127.0.0.1:3009/mac/view/:userId?ot-replace[0]=kin43
I need the value of ot-replace[0]
In the controller I am using
#RequestParam(name="ot-replace", required=false)String[] regexReplace
Also tried using
#RequestParam(name="ot-replace", required=false)List<String> regexReplace
I always get null.
Not sure what is the issue here
Your URL should like this.
http://127.0.0.1:3009/mac/view/:userId?ot-replace=kin43,value2,vlue3
then
you can get a String[].
ot-replace[0]=kin43
ot-replace[1]=value2
ot-replace[2]=value3
Try to use a POST methood when excute your url and set your array in the request body.

How can I prevent the automatic decoding of RequestParam Strings in a Spring controller

I have an HTTP GET request controller endpoint where I take in a fileName as a query param and pass that on to another service. For this request the param the filename could include any sort of special characters and I would like to keep these values encoded when passing them on. 2 Characters that have been causing issues are spaces (%20) and +(%2B).
How can I keep these characters encoded in the request params.
So far I have tried using the #RequestParam annotation as well as retrieving the params via HttpServletRequest.getParameterValues(String) but both return the decoded values as spaces.
Any help is appreciated thanks!
Yes, these are automatically decoded by the servlet API. You should be able to re-encode them -
encodedValue = URLEncoder.encode(value, StandardCharsets.UTF_8);
I found out that I could get the actual value passed in by using the HttpServletRequest.getQueryString() method. Parsing this query string I was able to get the un-decoded version of the fileName being passed in. I hope this helps someone in the future.

Retrofit2 complex request

I am trying to create a request using Retrofit2. I created the request using a standard library:
path = "https://www.iii.com/?id="+id+"&data=";
query = "{\"name\":\""+name+"\",\"quantity\":20}";
Final link is:
link = path+URLEncoder.encode(query, "UTF-8");
I tried different Retrofit2 options, but I can't understand how to translate my link to Retrofit2 link using together path and query with url encoded?
You can parse GET query parameters to retrofit using this code:
#GET("https://www.iii.com")
Observable<ResponseBody> getSomething(
#Query("id") int id,
#Query("data") String data
);
Retrofit will build it for you. Just pass your variables (assuming you know how to call retrofit requests) and retrofit will url encode it for you. You can refer to this link: https://square.github.io/retrofit/2.x/retrofit/index.html?retrofit2/http/Query.html
Values are converted to strings using Retrofit.stringConverter(Type, Annotation[]) (or Object.toString(), if no matching string converter is installed) and then URL encoded. null values are ignored. Passing a List or array will result in a query parameter for each non-null item.
you can add anotation for that like below
#Headers("charset=UTF-8")
#GET("https://www.iii.com")
Observable<ResponseBody> getSomething(
#Query("id") int id,
#Query("data") String data
);

How to add post query parameter string with double quote in url in retrofit?

I am accessing an api .which looks like
http://XXX.XX.XXX.XXX/api/Gallery/getgallerylist?userid="17" .
I have tried the following annotation.
#POST("Gallery/getgallerylist")
Call<GalleryImagesResponse> getGalleryImages(#Query("userid") String userid);
But the url it requests is http://XXX.XX.XXX.XXX/api/Gallery/getgallerylist?userid=17
So that how can I add double quote to userid's value like userid="17".
You can pass the string as #Path to the URL like this.
#POST("http://XXX.XX.XXX.XXX/api/Gallery/getgallerylist?" + "{path}")
Call<ResponseBody> getGalleryImages(#Path("path") String path);
and pass userid=17 to this call, it will append to the URL. and your done
you should try this before hitting url
URLDecoder.decode(url,UTF8);

How can I get the URL paramter dynamically in Java?

A url is hit from browser now I want to get that url in my dataBean to extract its parameters for some checks.
How can I get that URL dynamically in that particular databean?
for instance :
someone hit
https://someAddress/AjaxForm?id=someid
I need to capture this url and get value of Id. How to do it?
you can do something likewise,
request.getRequestURL() // gives your current URL
where request is an instance of HttpServletRequest.
UPDATED :
If you are trying to find parameter which are comes along with URL, then
rather then do above mentioned my trick,
directly do likewise,
request.getParameter("id");
String url=request.getRequestURL()
String id=request.getParameter("id");
To get the parameter from url
request.getParameter("id");
And to insert it, in javascript add the parameter by attaching it to url
var url=baseurl+"?id="+id;
in the doGET method you write
String idValue=request.getParameter("id");
so the method will look like this
protected void doGET(HTTPServletRequest request,HTTPServletResponse response){
//some code here
String idValue=request.getParameter("id");//idValue will hold the value you passed in URL
//some more code here
}

Categories