Spring is a popular Java framework used to build web applications. It provides tools and conventions to help developers create applications more efficiently by handling common tasks and allowing them to focus on the core functionality of their apps.
The @RequestMapping annotation is used to connect web requests to specific methods in your controller. It tells Spring which method should handle which URL.
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String sayHello() { return "Hello, world"; } }
When you use @RequestMapping at the class level, you define a base URL that all methods in that class will use. This helps in organizing URL patterns for different controllers.
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/recipes") public class RecipeController { @GetMapping() public ListgetAllRecipes() { // return a list of recipes } }
Different types of HTTP requests (GET, POST, PUT, DELETE) are used to perform different actions. For example, GET retrieves data, POST creates new data, PUT updates existing data, and DELETE removes data.
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class FlowerController { @GetMapping("/flowers") public ListgetAllFlowers() { // return a list of flowers } @PostMapping("/flowers") public Flower addFlower(@RequestBody Flower flower) { // add a new flower } @PutMapping("/flowers/{id}") public Flower updateFlower(@PathVariable Long id, @RequestBody Flower flower) { // update an existing flower } @DeleteMapping("/flowers/{id}") public void deleteFlower(@PathVariable Long id) { // delete a flower } }
You can access parameters from the URL or request body using annotations like @RequestParam and @PathVariable. This helps you work with the data sent by the user.
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class FruitController { @GetMapping("/fruit") public Fruit getFruit(@RequestParam String type) { // find and return the fruit based on the type } }
REST controllers handle HTTP requests and send responses in a RESTful way. They use annotations like @RestController and @GetMapping to define how different requests are handled.
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class LocationController { @GetMapping("/locations/{coordinates}") public City getLocationByCoordinates(@PathVariable String coordinates) { // find and return the city based on coordinates } }
You can handle errors and send custom HTTP status codes by throwing exceptions like ResponseStatusException. This helps in managing error responses properly.
import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.server.ResponseStatusException; @RestController public class BookController { @GetMapping("/books/{id}") public Book getBook(@PathVariable String id) { if (id.matches("\d+")) { // find and return the book } else { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid ID format."); } } }
HTTP status codes are used to indicate the result of an HTTP request. For example, 200 means OK, 404 means not found, and 500 means server error.
HttpStatus.OK // 200: The request was successful HttpStatus.NOT_FOUND // 404: The resource was not found HttpStatus.INTERNAL_SERVER_ERROR // 500: There was a server error
You can specify the HTTP status code to return with a response by using the @ResponseStatus annotation. This helps indicate the result of an operation clearly.
import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; @RestController public class BookController { @PostMapping("/books") @ResponseStatus(HttpStatus.CREATED) public void addBook(@RequestParam String title) { // add a new book and return status 201 Created } }
When you receive data in your application, you can deserialize it (convert it from JSON) into Java objects using @RequestBody. This helps in handling complex data structures sent by clients.
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class BookController { @PostMapping("/books") public Book getBook(@RequestBody Book book) { // find and return the book based on the provided data return book; // Just returning the book for this example } }
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!