Rest controller request multiple path variables with multiple query parameters - java

How can we create a Rest API (Spring controller) which allows multiple path variables to have query parameters?
Where
1) function is a path variable and id=functionname is query parameter
2) subfunction is a path variable and id=subfuntionname is query parameter
Request URL : /content/v1/clients/clientname/function?id=functionname&subfunction?id=subfunctionname
Update I am using matrix variations suggested by
/content/v1/clients/clientname/function;id=functionname/subfunction;id=subfunctionname
The method shown below is not working as expected.
What should the method definition look like?
public HashMap<String, List<Model>> getContent(
#PathVariable String clientname,
#MatrixVariable(name="id", pathVar="function") List<String> capabilitiesId,
#MatrixVariable(name="id", pathVar="subfunction") List<String> subcapabilitiesId) {
}
Error : Missing matrix variable 'id' for method parameter of type List

It's not possible.
In REST controller you have two type of parameters:
Path parameter: parameter usefull to select a resource. (a you class's method)
Query parameter: parameter useful to send other information.
In your case I think that is a good idea send all this informations inside payload, using POST or PUT http method.
If you can't use payload you can obtain the following solution:
Request URL : /content/v1/clients/clientname/function1/function2?id1=functionnamec&id2=subfunctionaname
In this way you can create your controller with 2 path parameters and 2 query parameters:
#GET
#Path("/basePath/{funct1}/{funct2}")
public Response <methodName>(#PathParam("funct1") String funct1, #PathParam("funct2") String funct2, #QueryParam("id1") String id1, #QueryParam("id2") String id2)

/content/v1/clients/clientname/function?id=functionnamec&subfunction?id=subfunctionaname
The parsing of URI is defined by RFC 3986. In particular, U+003F QUESTION MARK is a reserved character, the first instance of which serves a the delimiter between the relative-part and the query.
So your example breaks would parse as
path: /content/v1/clients/clientname/function
query: id=functionnamec&subfunction?id=subfunctionaname
And if we were to parse the query, as though it were an application/x-www-form-urlencoded value....
>>> import urllib.parse
>>> urllib.parse.parse_qs("id=functionnamec&subfunction?id=subfunctionaname")
{'id': ['functionnamec'], 'subfunction?id': ['subfunctionaname']}
We see that the second question mark becomes part of the parameter name.
In short, it's a perfectly valid URI, but it isn't likely to produce the results that you are hoping for.
/content/v1/clients/clientname/function/subfunction?id=functionnamec&id=subfunctionaname
This might be usable, but there's likely to be some confusion about the duplicate id query parameters
>>> urllib.parse.parse_qs("id=functionnamec&id=subfunctionaname")
{'id': ['functionnamec', 'subfunctionaname']}
/content/v1/clients/clientname/function/subfunction?function.id=functionnamec&subfunction.id=subfunctionaname
>>> urllib.parse.parse_qs("function.id=functionnamec&subfunction.id=subfunctionaname")
{'function.id': ['functionnamec'], 'subfunction.id': ['subfunctionaname']}
That might be easier.
I think it would be common to take the data out of the query and put it on the path instead
/content/v1/clients/clientname/function/functionname/subfunction/subfunctionaname
And then extract the path parameters you need.

Related

How to pass an url by a rest api URL string

I have a table a with columns like pageId and page_name where values are inserted line [1,https://google.com] and so on.
Now i created an api that takes the URL and returns the pageid, so now the scenario is like:
localhost:8080/api/v1/page/https://google.com
whenever i am trying to pass it via Postman is is showing Could not send response can anyone help me to fix this problem?
The problem is that you have reserved chars in your query param.
Consider encoding your text.
So:
http://www.google.com
will become:
http%3A%2F%2Fwww.google.com
localhost:8080/api/v1/page/https://google.com
According url format documentation (see for example this article )- impossible use reserved chars (: and /) as parameters. I reccoment use something like
localhost:8080/api/v1/page/google.com
And add "https://" in service
or use
localhost:8080/api/v1/page/https~~google.com
And replaced "~~" to "://".

Listing the parameters in a node using RESTassured library with Java

The webservice: http://services.groupkt.com/country/search?text=lands has 16 records returned from a GET request. In the records, there is a parameter called 'name'. So, there are 16 names and each name has a unique value (country). My intention is to list all the 16 values of the 'name'parameter using java and the RESTassured library. I tried this:
Response response = RestAssured.get("http://services.groupkt.com/country/search?text=lands").andReturn();
String json = response.getBody().asString();
JsonPath jp = new JsonPath(json);
List<String> ls = from(response).getList("RestResponse.result.name");// The 'from' text displays an error
An error was seen on the 'from' text and it says: The method from(String) in the type RestTest is not applicable for the arguments (Response). I am not sure how to rectify this. Is there a simple way to create a list of all the values of the 'name' parameter?
Try replacing from(response) to from(jp). That should take care of the error you are getting.
The Response class (which is what the get() method returns) also supports jsonPath, so you could also refactor it to something like this:
List names= get("http://services.groupkt.com/country/search?text=lands").jsonPath().getList("RestResponse.result.name");

How to pass an url as path param?In JAX-RS #Path

http://localhost:8181/RESTfulExample/entityid/https://www.youtube.com
#GET
#Path("/entityid/{entityid : [a-zA-Z][a-zA-Z_0-9]}")
public Response getUserByentityid(#PathParam("entityid") String entityid) {
return Response.status(200)
.entity("getUserByentityid is called, username : " + entityid)
.build();
}
How to modify the regular expression to accept an url in it? Or any other alternate solution to fetch the entityid which is an URL?
You shouldn't pass a URL as a path parameter. Either the entity id is a regular identifier (integer, GUID, ...), in which case it can be in the path like you have it, or it is a URL, in which case the URL would be https://example.com/myapp/entityid/123 and you're back to the entity id part of the entity id URL being just a regular identifier.
Now, technically, you can pass a URL as a path parameter by encoding all special characters using percent encoding, however I would not recommend it.
Let's say your app is at https://example.net/otherapp/, then the combined url would be:
https://example.net/otherapp/entityid/https%3A%2F%2Fexample.com%2Fmyapp%2Fentityid%2F123
The regular expression would match against the unencoded value, so this might work:
{entityid : https?://.*}
NOTE: Encoding a path segment must be done for all values, not just for URL value. Integer numbers are safe, but pretty much all other values must be encoded.

java.net.URI and percent in query parameter value

System.out.println(
new URI("http", "example.com", "/servlet", "a=x%20y", null));
The result is http://example.com/servlet?a=x%2520y, where the query parameter value differs from the supplied one. Strange, but this does follow the Javadoc:
"The percent character ('%') is always quoted by these constructors."
We can pass the decoded string, a=x y and then we get a reasonable(?) result a=x%20y.
But what if the query parameter value contains an "&" character? This happens for example if the value is an URL itself with query parameters. Look at this (wrong) query string:
a=b&c. The ampersand must be escaped here (a=b%26c), otherwise this can be considered as a query parameter a=b and some garbage (c). If I pass this to an URI constructor, it encodes it, and returns a wrong URL: ...?a=b%2526c
This issue seems to render java.util.URI useless. Am I missing something here?
Summary of answers
java.net.URI does know about the existence of the query part of an URI, but it does not understand the internals of the query part, which can differ for each scheme. For example java.net.URI does not understand the internal structure of the HTTP query part. This would not be a problem, if java.net.URI considered query as an opaque string, and did not alter it. But it tries to apply some generic percent-encoding algorithm, which breaks HTTP URLs.
Therefore I cannot use the URI class to reliably assemble an URL from its parts, despite there are constructors for it. I would also mention that as of Java 7, the implementation of the relativize operation is quite limited, only works if one URL is the prefix of another one. These two functionality (and its leaner interface for these purposes) were the reason why I was interested in java.net.URI, but neither of them works for me.
At the end I used java.net.URL for parsing, and wrote code to assemble an URL from parts and to relativize two URLs. I also checked the Apache HttpClient URIBuilder class, and although it does understand the internals of an HTTP query string, but as of 4.3, it has the same problem with encoding like java.net.URI when dealing with the query part as a whole.
The query string
a=b&c
is not wrong in a URI. The RFC on URI Generic Syntax states
The query component is a string of information to be interpreted by
the resource.
query = *uric
Within a query component, the characters ";", "/", "?", ":", "#",
"&", "=", "+", ",", and "$" are reserved.
The character & in the query string is very much valid (uric represents reserved, mark, and alphanumeric characters). The RFC also states
Many URI include components consisting of or delimited by, certain
special characters. These characters are called "reserved", since
their usage within the URI component is limited to their reserved
purpose. If the data for a URI component would conflict with the
reserved purpose, then the conflicting data must be escaped before
forming the URI.
Because the & is valid but reserved, it is up to the user to determine if it is meant to be encoded or not.
What you call a query parameter is not a feature of a URI and therefore the URI class has no reason to (and shouldn't) support it.
Related:
Which characters make a URL invalid?
The only workaround I found was to use the single-argument constructors and methods. Note that you must use URI#getRawQuery() to avoid decoding %26. For example:
URI uri = new URI("http://a/?b=c%26d&e");
// uri.getRawQuery() equals "b=c%26d&e"
uri = new URI(new URI(uri.getScheme(), uri.getAuthority(),
uri.getPath(), null, null) + "?f=g%26h&i");
// uri.getRawQuery() equals "f=g%26h&i"
uri = uri.resolve("?j=k%26l&m");
// uri.getRawQuery() equals "j=k%26l&m"
// uri.toString() equals "http://a/?j=k%26l&m"
Single working solution known for me is reflection (see https://blog.stackhunter.com/2014/03/31/encode-special-characters-java-net-uri/)
URI uri = new URI("http", null, "example.com", -1, "/accounts", null, null);
Field field = URI.class.getDeclaredField("query");
field.setAccessible(true);
field.set(uri, encodedQueryString);
//clear cached string representation
field = URI.class.getDeclaredField("string");
field.setAccessible(true);
field.set(uri, null);
Use URLEncoder.encode() method, in your case for example:
URLEncoder.encode("a=x%20y", "ISO-8859-1");

how do I create a servlet with parameters and a "/" at the ending?

My servlet needs to receive 2 parameters to respond.
My favorite solution (but it doesn't work in my context):
http://domain.com/?param1=something&param2=anything
because: I've another application which requires that a url ends with "/". But I can't create a servlet which accepts urls like "http://domain.com/?param1=something&param2=anything/" <<- / at the end.
My second solution is:
http://domain.com/param1/param2/
I could split the requested url by "/" and I would have my 2 parameters. But it's not that nice..
Is there a better way to pass through 2 parameters and have an url which ends on a "/"?
I think it is not possible. As it is defined in the HTTP RFC
"http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]
After the first "?" there is the query part. So in your example
http://domain.com/?param1=something&param2=anything/
That means param2 value is anything/ (with the slash in the end)
Of course you can bind your servlet to /* url-pattern and process the parameters in the servlet using ServletRequest.getParameter(). But don't forget that your param2 will end with a /
According to RFC 3986, section 3.3, it is possible to assign a set of parameters to each path segment like so:
http://domain.com/path;param1=value1;param2=value2/subpath/subsubpath/
So you can have parameters without the query part.
But the downside is:
What you want to achieve is mabye not the intended use case for that feature.
Other than for query parameters, there is no API support for segment parameters. So you have to parse the parameters on your own.

Categories