Home > Java > javaTutorial > What are the common framework annotations for Spring Boot Rest?

What are the common framework annotations for Spring Boot Rest?

王林
Release: 2023-05-11 17:22:06
forward
1302 people have browsed it

Prerequisites for starting Spring Boot Rest

The most important thing to know is the Spring container or IOC. In addition to this, you must have basic knowledge of Java annotations. Because Spring Boot applications are full of annotations. Last but not least, the concept of REST should be clarified, at least the basic concepts. For example, GET, POST, PUT, DELETE and Request body and response, headers, etc.

Create a Spring Boot project in Spring Initializer

The best thing about Spring Boot is that it has a web application for getting started. Just click on a few form fields and buttons and you'll have your starter file ready. This includes pom. xml containing all required dependencies. Just go to Spring Initializer: https://start.spring.io/. Fill in the appropriate data as shown in the screenshot below. Then click the Add Dependencies button in the upper right corner. Type and select Web. I will also add Lombok for log and Pojo.

What are the common framework annotations for Spring Boot Rest?

Once completed, just click the generate button and a zip file (pom.xml) containing these files will be downloaded. Unzip it and open it in the IDE. I will use IntelliJ IDEA. This IDE helps me take advantage of various features to improve my work efficiency. IDE commonly used plug-ins

Spring Boot annotations

The entire Spring Boot relies on annotations. There are various notes for different needs and procedures. The rest of the section has 4-5 main notes. Let's look at them one by one and apply them to our example. The

@RestController

annotation is used on the

@RestController class. It defines the class as a controller for Rest. This is a RESTful version of the controller that adds the ResponseBy combination. This way your response is automatically converted to JSON, XML, or any defined response type without the need for a separate ResponseBy annotation.

@RestController
public class ArticleRestController {}
Copy after login

The remaining annotations are used under the @RestController class.

@RequestMapping

is used to map specific endpoints to functions or methods. This helps define the endpoint's path, method, response type, request type, etc.

@RequestMapping(value = "/articles", method = RequestMethod.GET,  produces = "application/json")
public List<Article> getArticles() { }
Copy after login

In the above example, the value represents the path it maps to. For example, localhost:8080/articles. The method type is GET, which generates "application/json" as the response (however, this is the default and you can skip it).

@RequestParam

The query parameters in the URL, that is, ? key=value&key1=value1 is obtained by @RequestParam annotation. This is used for parameters of functions. It has various options such as required, defaultValue, etc. Then load the query parameters into the specified parameters.

@RequestMapping(value = "/article", method = RequestMethod.GET)
public Article getArticleByName(
   @RequestParam(value = "articleName", required = true) String articleName
) {}
Copy after login

In the above example, if we call the URL GET localhost/article? articleName=springboot, "springboot" will be loaded in the articleName parameter. I've put reuired=true, so if we don't pass articleName, it will throw an error to the user.

@PathVariable

Ever wonder how the server knows which article to load without passing query parameters? Take a look at the URL of this post. The post URL does not contain query parameters, but a plain slash-delimited string. The server reads it with the help of PathVariable and it looks like this, /article/{articleslaug}. Any string replacing {articleSlug} will be treated as PathVariable.

@RequestMapping(value = "/article/{articleSlug}", method = RequestMethod.GET)
public Article getArticle(@PathVariable("articleSlug") String articleSlug) {}
Copy after login

Remember that {articleSlug} should be the same in RequestMapping and PathVariable (without {}). If it doesn't match, it won't load.

@RequestBody

For the POST or PUT method, you don't get all the data via the URL, right? These methods have an appropriate request body. RequestBody annotations help automatically map a given request body to parameters. Mainly JSON to POJO.

@RequestMapping(value = "/article", method = RequestMethod.POST)
public Article createArticle(@RequestBody NewArticlePojo newArticle){}
Copy after login

The request body will be automatically mapped to the NewArticlePojo class and the fields will be populated based on the key.

Specific Annotations for REST Methods

In the above explanation, you must have seen that I have written RequestMapping using a specific method. But this is too redundant. To solve this problem, Spring Boot provides predefined method mappings. These are just extensions to @RequestMapping.

@GetMapping

@GetMapping is used to replace RequestMapping with method=RequestMethod. Get parameters. Now by looking at it, we know this is a GET request.

@PostMapping and @PutMapping

PostMapping are replaced with method=RequestMethod for RequestMapping. Similarly, PutMapping will replace RequestMethod.PUT with

method=RequestMethod

and

RequestMethod.PUT

##@DeleteMappingDeleteMapping with method=RequestMethod

Replace###RequestMethod.DELETE######

The above is the detailed content of What are the common framework annotations for Spring Boot Rest?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template