Spring REST API -Part 4.1: Spring Controllers (RestController — GET)
Part 4.1:Spring Controllers (RestController — GET)
Note — Codes in the story is in continuation to the previous parts, so if you feel uncomfortable or disconnected please check the previous parts or navigate through git commit to have a better understanding.
Now you want your own implementation, you want to control the HTTP requests and response for your api
In Spring’s approach to building RESTful web services, HTTP requests are handled by a controller.
What is Controller ?
Controller is, thread-safe class, capable of handling multiple HTTP requests throughout the lifecycle of an application.
RestController, is a specialized version of the controller which is a convenience annotation, that is itself annotated with @Controller
and @ResponseBody
. By annotating the controller class with @RestController
annotation, you no longer need to add @ResponseBody
to all the request mapping methods. The @ResponseBody
annotation is active by default
Creating TopicController
@RestController
@RequestMapping ("/topics")
public class TopicController {
@Autowired
TopicRepository topicRepository;
@RequestMapping (method = RequestMethod.GET)
Collection<Topic> getAllTopics(){
return Lists.newArrayList(topicRepository.findAll());
}
}
RequestMethod.GET
Handles HTTP GET requests
CrudRepository
provides sophisticated CRUD functionality for the entity class that is being managed. It has implementation for some some of the very commonly user query:
findAll()
Returns all entities.
T findOne(ID id)
Return the entity with the given id.
Commit
Here you can see the commit of all this changes:
https://github.com/ziakhalid/spring-rest-sample/commit/77f59b37637bfcb1997d1abb3be482540ac4f043
Hit Run
Using Client : Postman
You see the list of topic :) … Wait….. but it doesn’t looks like the previous API which Spring produced by default, it don’t have the
navigable links which was pointing to available resource ???
Hold On till we talk about (Spring HATEOAS) in upcoming stories :)
Implementing custom Query
Now you wanna have more control over your repository , you want to find topics by topic name
Adding custom query to your repository
//select a from Topic a where a.topicname = :topicname
Collection<Topic> findByTopicName(String topicName);
Adding controller for custom query
We can pass parameter in URI topicName/{topicName}
which can be received by method by using @PathVariable
@PathVariable
annotation, indicates that a method parameter should be bound to a URI template
@RequestMapping(method = RequestMethod.GET, value = "topicName/{topicName}")
public Collection<Topic> getTopicByTopicName(@PathVariable String topicName){
return topicRepository.findByTopicName(topicName);
}
Commit
https://github.com/ziakhalid/spring-rest-sample/commit/5d077f6f4679e9dcf29d20ad09c4bc2727bbfc8f
Hit Run
Here you Go , HTTP Response based on your query parameter :)
If you find something to improve please let me know, I will try my best to improve this story.
If this article was helpful to you, please do hit the 💚 button below. Thanks!
See you in the next story!
Next Article
Part 4.2: Spring Controllers (RestController — POST,PUT,DELETE)