Lädt...


🔧 Spring Boot Controllers Basics


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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

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

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

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.

...

🔧 Spring Boot Controllers Basics


📈 39.95 Punkte
🔧 Programmierung

🔧 Spring Boot Controllers Basics


📈 39.95 Punkte
🔧 Programmierung

🔧 Spring vs Spring MVC vs Spring Boot: A Detailed Comparison for Java Developers


📈 34.14 Punkte
🔧 Programmierung

📰 software-architektur.tv: GraalVM mit Spring Native, Spring Boot und Spring Cloud


📈 34.14 Punkte
📰 IT Nachrichten

🔧 How to Use Spring Boot Eureka Server in Spring Boot 3.3.0+


📈 32.78 Punkte
🔧 Programmierung

🔧 Java Spring Boot for Beginners: Diving into Project Structure, REST Controllers, and Request Handling


📈 29.77 Punkte
🔧 Programmierung

🔧 Building Your First Spring Boot App: A Complete Guide to MVC Architecture and REST Controllers


📈 29.77 Punkte
🔧 Programmierung

🔧 spring into spring: basics, API, beans


📈 27.94 Punkte
🔧 Programmierung

🪟 PowerA’s new FUSION Controllers for Xbox are putting the native controllers to shame


📈 26.75 Punkte
🪟 Windows Tipps

🪟 Microsoft is branching out from custom Xbox controllers to custom chocolate controllers


📈 26.75 Punkte
🪟 Windows Tipps

🕵️ Mozilla Firefox up to 1.0.8/1.5.0.2 windows.controllers window.controllers cross site scriting


📈 26.75 Punkte
🕵️ Sicherheitslücken

🕵️ NodeBB up to 0.7.2 controllers/index.js Controllers.outgoing cross site scripting


📈 26.75 Punkte
🕵️ Sicherheitslücken

🔧 Introducing Azure Spring Cloud: fully managed service for Spring Boot microservices


📈 25.27 Punkte
🔧 Programmierung

🔧 Spring & Spring Boot Interview Guide


📈 25.27 Punkte
🔧 Programmierung

📰 Spring Boot 2.0 mit Support für Spring Framework 5.0 veröffentlicht


📈 25.27 Punkte
📰 IT Nachrichten

🔧 Spring Boot 3 application on AWS Lambda - Part 9 Develop application with Spring Cloud Function AWS


📈 25.27 Punkte
🔧 Programmierung

🔧 Locking Down Your Spring Boot Apps: A Deep Dive into Spring Security


📈 25.27 Punkte
🔧 Programmierung

🔧 Securing Your Spring Boot Application with Spring Security


📈 25.27 Punkte
🔧 Programmierung

🔧 Integration Testing With Keycloak, Spring Security, Spring Boot, and Spock Framework


📈 25.27 Punkte
🔧 Programmierung

🔧 Connecting Spring Boot Applications to a Database with Spring Data JPA


📈 25.27 Punkte
🔧 Programmierung

🔧 Spring Boot 3 application on AWS Lambda - Part 8 Introduction to Spring Cloud Function


📈 25.27 Punkte
🔧 Programmierung

🔧 Implementing Token-Based Authentication in Spring Boot Using Spring Security, JWT, and JDBC Template


📈 25.27 Punkte
🔧 Programmierung

🔧 Java Spring vs Spring Boot - Understanding the Difference


📈 25.27 Punkte
🔧 Programmierung

🔧 Spring Boot with VueJS with Spring Security


📈 25.27 Punkte
🔧 Programmierung

🔧 Generative AI With Spring Boot and Spring AI


📈 25.27 Punkte
🔧 Programmierung

🔧 Learn Spring Boot and Spring Data JPA


📈 25.27 Punkte
🔧 Programmierung

🔧 Developing Microservices with Spring Boot and Spring Cloud


📈 25.27 Punkte
🔧 Programmierung

🎥 Spring Boot & Spring Data JPA – Complete Course


📈 25.27 Punkte
🎥 Video | Youtube

🔧 Master REST API Development and Streamline Data Access with Spring Boot and Spring Data JPA


📈 25.27 Punkte
🔧 Programmierung

🔧 Master Spring Boot and Spring Security: Build a Shopping Cart Backend


📈 25.27 Punkte
🔧 Programmierung

🔧 Upgrade Guide To Spring Boot 3.0 for Spring Data JPA and Querydsl


📈 25.27 Punkte
🔧 Programmierung

🔧 Build a Shopping Cart Backend with Spring Boot and Spring Security


📈 25.27 Punkte
🔧 Programmierung

🔧 Build CRUD RESTful API Using Spring Boot 3, Spring Data JPA, Hibernate, and MySQL Database


📈 25.27 Punkte
🔧 Programmierung

matomo