springboot controller demo
时间: 2023-09-02 07:15:41 浏览: 97
springboot demo
Here is a simple Spring Boot controller code that demonstrates the basic functionality of a controller:
```
@RestController
public class DemoController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, world!";
}
@PostMapping("/add")
public int addNumbers(@RequestParam int num1, @RequestParam int num2) {
return num1 + num2;
}
@DeleteMapping("/delete/{id}")
public void deleteById(@PathVariable Long id) {
// delete entity with given id
}
}
```
In this example, we have created a controller named `DemoController` with three endpoint mappings:
- `/hello`: a GET endpoint that returns a simple string response
- `/add`: a POST endpoint that accepts two request parameters and returns the sum of the two numbers
- `/delete/{id}`: a DELETE endpoint that accepts a path variable and deletes the entity with the given ID
We have also annotated the controller with `@RestController`, which indicates that it is a web controller that returns data rather than a view.
Note that the `@RequestParam` and `@PathVariable` annotations are used to extract request parameters and path variables, respectively. These annotations map the request parameters to the corresponding method parameters.
阅读全文