Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create REST API with optional parameters?

Tags:

java

rest

jax-rs

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?

like image 220
mbgsuirp Avatar asked Mar 01 '16 05:03

mbgsuirp


People also ask

How do I add optional parameters in REST API?

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) { ... }

How do you make an API parameter optional?

Optional Parameters in Web API Attribute Routing and Default Values: You can make a URI parameter as optional by adding a question mark (“?”) to the route parameter. If you make a route parameter as optional then you must specify a default value by using parameter = value for the method parameter.

How do I pass parameter values in REST API?

A REST API can have parameters in at least two ways: As part of the URL-path (i.e. /api/resource/parametervalue ) As a query argument (i.e. /api/resource? parameter=value )

Can parameters be optional?

The definition of a method, constructor, indexer, or delegate can specify its parameters are required or optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters. Each optional parameter has a default value as part of its definition.

What are parameters in REST API?

They are like search filters; they single out the data you want to receive from the API. Following are the most common types of parameters used in REST APIs: As their name suggests, they are included in the URL path of the endpoint. According to OpenAPI/Swagger spec, path parameters must be required and can't be optional.

How do I make a parameter optional in a web API?

Optional Parameters in Web API Attribute Routing and Default Values: You can make a URI parameter as optional by adding a question mark (“?”) to the route parameter. If you make a route parameter as optional then you must specify a default value by using parameter = value for the method parameter.

What is the default value of method parameter in web API?

In the second example (“ {stdid=1}”), the default value “1” assigned to the method parameter through the model-binding process. The default model-binder in Web API will convert the value “1” to the numeric value 1. In most of the cases, unless you have custom model binders in your pipeline, the two forms will be equivalent.

How to deal with optional request parameters in Spring Boot?

– The Full Stack Developer How to deal with optional request parameters in Spring Boot? Let’s say you are creating a REST API in Spring Boot which accepts request parameters. You use the annotation @RequestParam to retrieve the request values. And if user does not supply the request parameter Spring will throw error.


3 Answers

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.

like image 137
Jorn Avatar answered Sep 20 '22 08:09

Jorn


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.

like image 41
Emdadul Sawon Avatar answered Sep 22 '22 08:09

Emdadul Sawon


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) {
    ...
}
like image 23
cassiomolin Avatar answered Sep 20 '22 08:09

cassiomolin