DEV Community

Cover image for Spring Boot Controllers Basics
Pete
Pete

Posted on

Spring Boot Controllers Basics

In Spring Boot, a controller is a class that contains methods that process HTTP requests. A controller is created by annotating a class with RestController.

@RestController
public class ExampleController {
    private ExampleRepository exampleRepository;
    private static final String template = "Hello, %s!";

    @GetMapping("/examples/{requestedId}")
    public ResponseEntity<Example> findById(@PathVariable Long requestedId) {
    }
}
Enter fullscreen mode Exit fullscreen mode

The GetMapping annotation is used to route all GET requests that match a string pattern to a method in a controller. In the code example you just looked at, the GetMapping annotation was used to route all GET requests to the URL /examples/* to the findById method. With this code, when a request is sent to /examples/, the findById method will be called.

RequestMapping

There are other annotations for other request methods like PostMapping and DeleteMapping. They are all derived from the parent annotation RequestMapping and you can use RequestMapping in their place, although, when you use RequestMapping you have to specify the HTTP method it is supposed to handle

@RestController
public class ExampleController {
    private ExampleRepository exampleRepository;
    private static final String template = "Hello, %s!";

    @RequestMapping(method="GET", path="/examples/{requestedId}")
    public ResponseEntity<Example> findById(@PathVariable Long requestedId) {
    }
}
Enter fullscreen mode Exit fullscreen mode

RequestMapping is most commonly used to specify the URLs that you want a RestController to handle.

@RestController
@RequestMapping("/api/v1/examples")  // This is the base path
public class ExampleController {
    @GetMapping
    public String getAllExamples() {
    }

    @GetMapping("/{id}")
    public String getExampleById(@PathVariable("id") Long id) {
    }

    @PostMapping
    public String createExample() {
    }

    @DeleteMapping("/{id}")
    public String deleteExample(@PathVariable("id") Long id) {
    }
}
Enter fullscreen mode Exit fullscreen mode

In the code snippet above, by annotating the RestController class with RequestMapping, the url /api/v1/examples acts as a prefix for all the routes within the class. This means the getExampleById method that has a GetMapping of /{id} actually has a GetMapping of /api/v1/examples/{id} and the same applies to every single method in this class that has a RequestMapping associated with it.

👋 While you are here

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay