PathVariable and pathvariable with requestparam endpoints in rest controller [duplicate] - java

This question already has answers here:
create two method for same url pattern with different arguments
(2 answers)
Closed 11 months ago.
I want to create 2 endpoints in my #RestController annotated spring boot project.
Endpoint 1: api/account/balance/{accountnum}
Endpoint 2: api/account/balance/{accountnum}?datetime={datetime}
Both end points are having same URI until the path variable but second one has an additional query param in it. But when i send request it always go to first endpoint(without query param) eventhoug i send datetime query param in the request.
Is it possible to create these 2 endpoints with same URL but one with additional query param?

No, You can't, what you can do is get the datetime param value, if it has value, then do the operation based on it else do the other operation.

You can create a single endpoint with Queryparameter as optional by default.
If you want to specify you can use like below
#RequestParam(value = "myProp", required = false) String myProp

Related

Dependent RequestParam validation in Spring boot

I have a scenario where I have to rename one required request param. My application is already live. I want to deprecate the existing request param and want to add new request parameter. So my existing applications will be sending me the old request parameter and new application will start to send the new param.
Now I want to add validation, either I can mark both params as required or optional. But my business logic is either of param is required.
For example I already have a path parameter group and now I want to rename it to pageGroup. So either page or pageGroup is required.
How can I achieve it in Spring Boot? Any suggestions are welcome :)

SpringMVC - Naming strategy for #RequestParam

I would like all my APIs to use lower-case request parameters, but I would still like to use camel-case in my Java code.
For example, consider the following code:
#GetMapping("/ping")
public String ping(
String responseMessage)
{
return "PONG " + responseMessage;
}
To contact this endpoint and supply a response message, I would have to call: localhost:8080/ping?responseMessage=Hello. However, I would like spring to automatically bind the variable name responseMessage to a request parameter named responsemessage. I specifically do not want to manually write #RequestParam(name="responsemessage") in my controller, but rather configure a global naming strategy for request parameters. Is this possible?
There are a couple of other threads on SO that talks about making the request parameters or url mappings case-insensitive:
RequestParam value in spring MVC to be case insensitive
Is there any way we make PathVariable name case insensitive in Spring?
Spring mvc. case insensitive get parameters mapping
Making a request parameter binding case insensitive
However, the answers in these questions strikes me as quite hacky or requires manual codeing. Is there no simple setting for the behaviour I am after? I know this is possible when using JSON bodies, by configuring the Jackson propertyNamingStrategy. Is there an equivalent for request parameters?

How to develop a REST Webservice method which accepts multiple URI

How to develop a REST Webservice method which accepts multiple URIs for example
If we have a method
#RequestMapping(/add)
public Response add(#RequestParam List elements){
}
The method above serves for URL https://local host:8080/add
I have asked in interview how do we have a single Webservice which serves multiple URIs which I was not able to answer as I thought we should have unique URIs for each method
A follow up to this how do we have a method which returns the format whatever choosen (JSON, XML, PLAIN TEXT) from postman client.
Modify URI:
#RequestMapping(value = "/", produces = {
MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE
})
public Response add(#RequestParam List elements){
}
For the first question there is many ways to do that
Spring restTemplate can allow you to call a rest service in another method so all you have to do is to call your rest service when another uri is requested
You can also use proxies (web server proxies) to map same webservice to many URIs
For the second one you need to pass format as parameter and call specific service linked to format when executing
Hope that helps

Spring boot and validation of param in url

#RequestParam(value = "param") String param
How to validate this param in elegant way ? More exactly I must check if param is some value in database. My stack is: spring-boot+mybatis
This might be a duplicate of that one but anyway.
There's a difference in the way you validate forms and separate parameters. With POST it's actually impossible to break the request into separate parameters and you get the whole post body, and you use #Valid to process it. With GET it is possible to have separate parameters as arguments in method, and in this case Spring proprietary #Validated annotation should be used.

SpringMVC RequestMapping for GET url parameters

How to make the RequestMapping to handle GET parameters in the url? For example i have this url
localhost:8080/MyApplication/spm/gcNkyLXkwv
how to get the spm value from the above url
This can be done using PathVariable. I will just give example how it can be done. You can incorporate in your example
Suppose you want to write a url to fetch some order, you can say
www.mydomain.com/order/123
where 123 is orderId.
So now the url you will use in spring mvc controller would look like
/order/{orderId}
Now order id can be declared a path variable
#RequestMapping(value = " /order/{orderId}", method=RequestMethod.GET)
public String getOrder(#PathVariable String orderId){
//fetch order
}
if you use url www.mydomain.com/order/123, then orderId variable will be populated by value 123 by spring
Also note that PathVariable differ from requestParam as pathVariable are part of URL. The same url using request param would look like www.mydomain.com/order?orderId=123

Categories