Pass Multiple Parameters to Rest API - Spring

Passing multiple parameters to API

If you need to receive the data as separate files from the backend, you can implement an API that supports multipart uploads.

e.g.: https://www.baeldung.com/spring-rest-template-multipart-upload

How to specify multiple parameters in POST method

Well, first of all, you shouldn't put parameters for POST in the URL.

URL parameters are used for GET, and they are separated with & so in your case:

localhost:8080/makeAction?loanId=1&clientName=Stive&clientSurname=Wassabi

For POST you should submit parameters as request body parameters. Parameters are bound with @RequestParam annotation like @SMA suggested.

how to pass several parameters in get 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"

How to send multiple parameters using Axios and access them using Java Spring Boot service?

Your are sending data with axios to the spring boot controller in the correct way, the problem stands in the multiple RequestBody annotations in your method :

public String generateTestData(@RequestBody String inputTemplate,
@RequestBody String identifiersTemplate) throws TestDataGeneratorException

In case of multiple params contained in the body of the request one of the ways to solve is the conversion to a Map<String, Object> and then inside the body extract the parameters :

public String generateTestData(@RequestBody Map<String, Object> map) 
throws TestDataGeneratorException {
//your map will be {params={inputTemplate=..., identifiersTemplate=...}}
}

The message Required request body is missing is probably due to the multiple RequestBody annotations and related to the presence of the second annotation that according to the message receives an empty body, but I am not sure about it.

Effectively way of handling multiple parameters as input in GET Request

You can take a look at this post: Correct way to pass multiple values for same parameter name in GET request

There's no standard for what you are asking for, but you can take an argument as comma-separated values and split them in Java, or something like that. For example:

my.api.com/search?color=black,red,yellow,orange

or

my.api.com/search?color=black&color=red&color=yellow&color=orange

There also is a comment I really like, that says that the first approach (csv) should be used as OR (I want to search either black, either red, either yellow, either orange objects), and the second approach should be used as an AND (I want an object which is black, red, yellow AND orange, all of them). That comment is the following one: "A would suggest using id=a&id=b as boolean AND, and id=a,b as boolean OR. – Alex Skrypnyk May 7 '16 at 5:13" (on the checked answer).



Related Topics



Leave a reply



Submit