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.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

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

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay