how to pass several parameters in get request - java

I'm trying to check if my method works through the API
#GetMapping(value = "/ads/in/rubrics/{ids}")
public List<Ad> findAllAdInRubricByIds(#PathVariable("ids") List<Integer> ids) {
return adService.findAllAdInRubricByIds(ids);
}
how can i set some parameters in get request?
that's how i tried
http://localhost:9999/mvc/ad/ads/in/rubrics/ids&ids=1&ids=2
http://localhost:9999/mvc/ad/ads/in/rubrics/ids&ids1=1&ids2=2
always get error 400 Bad Request

You're confusing PathVariables with RequestParams.
A PathVariable is a variable in the request path. It doesn't need to be the last character.
#GetMapping("/api/{version}/foo/{idFoo}")
public Void getFooNumber(#PathVariable("version") Integer version, #PathVariable("idFoo") Integer idFoo){
return "1";
}
Since PathVariables are part of the path, they're always required. If you don't incluide them in the request you'll end up invoking another endpoint or getting a 404 if the request can't be mapped to any endpoint.
The RequestParams are the parameters received at the end of the request URL, after the "?" character.
#GetMapping("/api/foo")
public Void getFooNumber(#RequestParam(value="version", required=false) Integer version, #RequestParam(value="idFoo", required=true) Integer idFoo){
return "1";
}
With RequestParams you can define for each one of them if it's required or not.
You can also mix them and have in the same method PathVariables and RequestParams.
In the first example the request URL would be ".../api/1/foo/25", while in the second example it would be ".../api/foo?version=1&idFoo=25"
As for having an array or a list, if you define the parameter as a List you can send multiple parameters of the same name:
#GetMapping("/ads/in/rubrics")
public Void findAllAdInRubricByIds(#RequestParam(value="ids", required=true) List<Integer> ids){
return adService.findAllAdInRubricByIds(ids);
}
In this case, you can use ".../ads/in/rubrics?ids=1&ids=2&ids=3&ids=4"

http://localhost:9999/mvc/ad/ads/in/rubrics/?ids1=1&ids2=2
For the first parameter use a ? and after that for each additional parameter a &

Related

I am using Postman, to pass the data to a REST api but my variable is showing null value

My main question is how to pass a (Map, String) to a REST API, I know if I use #RequestBody all the passed contents are stored to map but what can be done to pass map as well as any other parameters REST API.
#GetMapping(path="/invoices")
public String invoiceReceived( Map<String,Object> invoice,String format) throws MessagingException {
System.out.println(format); // this prints NULL
return "returnValue";
}
So I tried using PathVariable but they throw exception. What can be done?
#GetMapping(path="/invoices/{invoiceData}/{format}")
public String invoiceReceived(#PathVariable("invoiceData") Map<String,Object> invoice,
#PathVariable("format") String format) throws MessagingException {
System.out.println(format); // this prints NULL
return "returnValue";
}
What should I do to accept a map and a variable as input? And what should be the JSON file look like which should be given as input?
{
"invoiceData":[{"invoiceId":"23642",
"clientName":"Client",
"amount":"23742.67",
"email":"client#abc.com"
}],
"format":"html"
}
This question was identified similar to another question, So I am trying to explain how is this different, I know that I can use #RequestBody to get all the variables in the map, but The call will be made with two parameters some of which will be stored in map but one parameter will be used for another variable. So how can I send a map along with any other variable?
I think you can use query strings and path variables.
If you declare a controller's method like:
#GetMapping(path="/invoices")
public String invoiceReceived(#RequestBody Map<String,Object> invoice, #RequestParam String format) {
...
}
the url to which the request is send and the JSON request body will be something like below.
The url:
http://localhost:8080/invoices?format=html
The JSON request body:
{
"invoiceId":"23642",
"clientName":"Client",
"amount":"23742.67",
"email":"client#abc.com"
}
Also you can use a path variable like:
http://localhost:8080/invoices/html
#GetMapping(path="/invoices/{format}“)
public String invoiceReceived(#RequestBody Map<String,Object> invoice, #PathVariable String format) {
...
}

How to create REST API with optional parameters?

I need to implement an API with these path params.
#Path("/job/{param1}/{optional1}/{optional2}/{param2}")
Can the second and third params by optional? So the client need not pass these, but have to pass the first and last.
If this is not possible, then is it recommended to rearrange the params in this way?
#Path("/job/{param1}/{param2}/{optional1}/{optional2}")
How to provide the optional params?
It might be easier to turn the optional path parameters into query parameters. You can then use #DefaultValue if you need it:
#GET #Path("/job/{param1}/{param2}")
public Response method(#PathParam("param1") String param1,
#PathParam("param2") String param2,
#QueryParam("optional1") String optional1,
#QueryParam("optional2") #DefaultValue("default") String optional2) {
...
}
You can then call it using /job/one/two?optional1=test passing only the optional parameters you need.
You can match the entire path ending in the REST request
#Path("/location/{locationId}{path:.*}")
public Response getLocation(
#PathParam("locationId") int locationId,
#PathParam("path") String path) {
//your code
}
Now the path variable contain entire path after location/{locationId}
You can also use a regular expressions to make the path optional.
#Path("/user/{id}{format:(/format/[^/]+?)?}{encoding:(/encoding/[^/]+?)?}")
public Response getUser(
#PathParam("id") int id,
#PathParam("format") String format,
#PathParam("encoding") String encoding) {
//your code
}
Now if you format and encoding will be optional. You do not give any value they will be empty.
Rearrange the params and try the following:
#Path("/job/{param1}/{param2}{optional1 : (/optional1)?}{optional2 : (/optional2)?}")
public Response myMethod(#PathParam("param1") String param1,
#PathParam("param2") String param2,
#PathParam("optional1") String optional1,
#PathParam("optional2") String optional2) {
...
}
to make request parameter optional set #requestparam to false in controller class
(#RequestParam(required=false)

Remove "/" from api call when optional parameter is null

We are using RESTful Web Services (Jersey) for API calls on java. While API needs optional parameter, we are doing as:
api-interface/user/userid/9000/companyid/90909/{optionalparameter*}
and we have to call this api when there is no optional parameter as:
api-interface/user/userid/9000/companyid/90909/
What needed is:
Case:1 If optional parameter exists
api-interface/user/userid/9000/companyid/90909/name/john/address/MA/age/34
Case:2 If Optional parameter doesn't exists.
api-interface/user/userid/9000/companyid/90909
My present implementation is:
#GET
#Path("user/companyid/{companyid}/userid/{userid}/{optionalparameter:.*}")
#Produces(MediaType.APPLICATION_JSON)
public Response getUserList(#PathParam("companyid") String companyId, #PathParam("userid") String userId,
#PathParam("optionalparameter") String syncDate) throws BadRequestException, InternalServerException {
//parsing the param.
}
In above code, I need to add trailing "/" but I am looking the way to remove this trailing "/" if anybody doesn't wants to pass those param.
I followed this link but it didn't worked while the preceding parameter's length is more then 1.
Please, Suggest me the best way.
Looking at your reference, have you tried this:
#Path("userid/{userid}/companyid/{companyid}{optparam:(/[^/]+?)*}")
public Response getLocation(
#PathParam("userid") int userid,
#PathParam("companyid") int companyid,
#PathParam("optparam") String optparam) {
String[] params = parseParams(optparam);
...
}
private String[] parseParams(String params) {
if (params.startsWith("/")) {
params = path.substring(1);
}
return params.split("/");
}
That should work, giving you all the parameters in a single array.
EDIT: I have updated the search string and verified it on a local install.

Getting query parameters with similar prefixes in Jersey [duplicate]

I am using Jersey for Rest and have a method that accepts #QueryParam.
However, the users may send #QueryParam. like this:
contractName# where # is a number from 0-155.
How can I define it in QueryParam (like regex expression)?
You can't specify the regexp. However, you can define a custom Java type to represent that query param and implement your own conversion from String to that type - see http://jersey.java.net/nonav/documentation/latest/jax-rs.html#d4e255 (example 2.15).
I don't think you can do it with QueryParam, but you can get the list of parameters directly:
#GET
public String get(#Context UriInfo ui) {
MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
}
and iterate through that looking for keys that match your regular expression.
#GET
public String get (#QueryParam(value="param") String param){
boolean test =testYourParamWithNativeRegexpTools(param);
if( test==false)return 400;
else //work
.....
}

Why does #RequestParameter String someValue in Spring 3 return 2x values?

Let's say I have this method in my controller:
#RequestMapping(value="/home", method=RequestMethod.GET)
public void captcha(#RequestParam String someValue, HttpServletResponse response)
{
System.out.println(someValue);
}
Why does the result of this request:
http://something/home?someValue=testvalue123
return this?
testvalue123,testvalue123
Using an Int only gives a single value as expected, but not String. Getting the parameter directly from the request-object also gives a single value.
Turns out there was a filter applied from some other library that incorrectly added the same request parameter a second time!
does adding "test" value to the #RequestParam Annotation help:
#RequestParam("test") String someValue

Categories

Resources