Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common annotations of Spring MVC and how to use them

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the common annotations of Spring MVC and how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that you read the common notes of this Spring MVC which and how to use the article will have a harvest, let's take a look at it.

Spring Boot integrates Spring MVC by default. Here are some common annotations for Spring MVC.

Development environment: IntelliJ IDEA 2019.2.2

Spring Boot version: 2.1.8

Create a new Spring Boot project named demo.

1. Controller comments

The Controller annotation is used to modify the Java class, which acts as a controller in MVC.

Controller annotations are modified with @ Component, and classes modified with Controller annotations are detected by @ ComponentScan and placed in the container as the bean of Spring

Medium.

Package com.example.demo;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class DemoController {@ RequestMapping ("/ index") @ ResponseBody public String index () {return "index";}}

After running the project, the browser visits: http://localhost:8080/index, and the page shows:

Index

II. RestController comments

The RestController annotations are intended to make it easier to use @ Controller and @ ResponseBody.

@ ResponseBody modifies the controller method, the return value of the method will be written to the response body of the HTTP, and the returned content will not be put into the model and will not be interpreted as the name of the view.

The following example is equivalent to the above example.

Package com.example.demo;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class DemoController {@ RequestMapping ("/ index") public String index () {return "index";}}

III. RequestMapping comments

RequestMapping annotations can modify classes or methods and are mainly used to map requests and processing methods.

When used to decorate a class and URL is set, it means that the URL prefix is set for each request.

RequestMapping annotations have the following main attributes:

(1) path and value: url for configuration mapping

(2) method: HTTP method of mapping, such as GET, POST, PUT, DELETE

You can also use several annotations with @ RequestMapping's method attribute configured by default:

@ GetMapping is equivalent to RequestMapping (method= "RequestMethod.GET")

@ PostMapping, @ PutMapping, @ DeleteMapping are similar.

(3) params: configure parameter identification for mapped requests

(4) consumes: configure the data type of the request, such as XML or JSON

(5) produces: configure the data type of the response, for example, "application/json" returns json data

Package com.example.demo;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping ("/ oa") public class DemoController {@ RequestMapping (value = "/ index1") public String index1 () {return "index1" } @ RequestMapping (value = "/ index2", method = RequestMethod.GET) public String index2 () {return "index2";} @ GetMapping (value = "/ index3") public String index3 () {return "index3";}}

Browsers visit:

Http://localhost:8080/oa/index1

Http://localhost:8080/oa/index2

Http://localhost:8080/oa/index3

The pages are displayed respectively:

Index1

Index2

Index3

IV. PathVariable comments

PathVariable annotations are mainly used to modify method parameters, indicating that the method parameters are variables that request URL.

Package com.example.demo;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class DemoController {@ GetMapping ("/ index1/ {name}") public String index1 (@ PathVariable String name) {return "index1:" + name } / / you can configure an attribute value for @ PathVariable to explicitly bind the method parameter to the value of the URL variable @ GetMapping ("/ index2/ {name}") public String index2 (@ PathVariable ("name") String lc) {return "index2:" + lc;}}.

Browsers access http://localhost:8080/index1/a

The page shows:

A

Visit http://localhost:8080/index1/b

The page shows:

B

5. RequestParam comments

The RequestParam annotation is used to get the request parameters in the request body, such as the page control name value after the form is submitted.

Package com.example.demo;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import java.util.Map;@RestControllerpublic class DemoController {@ PostMapping ("/ index1") public String index1 (@ RequestParam String userName) {return userName } / / map stores all request parameters @ PostMapping ("/ index2") public String index2 (@ RequestParam Map map) {String age = map.get ("age"); String sex = map.get ("sex"); return age + "," + sex;}}

Create a new html file on your computer, such as your desktop:

After the browser opens, if you click the "submit 1" button, the page jumps to http://localhost:8080/index1 and displays abc.

If you click the "submit 2" button, the page jumps to http://localhost:8080/index2, which displays 22 male.

VI. File upload

RequestParam annotations can be used to upload files.

Package com.example.demo;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.multipart.MultipartFile;import java.io.File;import java.io.IOException;@RestControllerpublic class DemoController {@ PostMapping ("/ upload") public String upload (@ RequestParam ("file") MultipartFile file) throws IOException {String fileName = file.getOriginalFilename (); String filePath = "RequestParam /" File dest = new File (filePath + fileName); file.transferTo (dest); return "upload successfully";}}

Casually create a new html file

After the browser opens, select a file, click submit, and the file is saved to disk D.

This is the end of the article on "what are the common notes of Spring MVC and how to use them?" Thank you for your reading! I believe you all have a certain understanding of "what are the common notes of Spring MVC and how to use them". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report