Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

queryparm support in spring for restful web services

How can I get queryparam support of restful web service by spring in true restful manner...

for example i have url like following

localhost:8080/myapp/booksearch/title/{title}/author/{author}?sortby=relevance&year=2013

title and author I can get by @PathVariable .....

where i want sortby and year optional..

like image 527
Ram Sharan Mittal Avatar asked Sep 21 '13 19:09

Ram Sharan Mittal


People also ask

Does Spring support REST API?

A REST API like Spring Boot REST API can help you retrieve information from another system. This article will guide you on how to build a Spring Boot REST API in Java. Spring Boot is an open-source Java-based tool used to create Web Applications and Microservices with ease.

What is RESTful Web Services in Spring?

RESTful web services try to define services using the different concepts that are already present in HTTP. REST is an architectural approach, not a protocol. It does not define the standard message exchange format. We can build REST services with both XML and JSON. JSON is more popular format with REST.


1 Answers

You can use the @RequestParam annotation on method parameters

@RequestMapping (...)
public String getBooks(@RequestParam(required = false, value = "sortby") String sortBy, @RequestParam(required = false, value = "year") String year) {...}

The @RequestParam annotation also has a defaultValue attribute to use as a value if the request parameter isn't provided. Without it, if the parameter is not provided, null will be passed as the argument for that parameter.

like image 55
Sotirios Delimanolis Avatar answered Oct 11 '22 03:10

Sotirios Delimanolis