Common Annotations for Spring MVC

Keywords: Java Spring JSON Attribute

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 Book project named demo.

Controller Notes

Controller annotations are used to modify Java classes, which act as controller roles in MVC.
Controller annotations use the @Component modifier. Classes modified with Controller annotations are detected by @ComponentScan and placed in containers as Spring bean s.
Medium.

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DemoController {

    @RequestMapping("/index")
    @ResponseBody
    public String index(){
        return "index";
    }
}

After running the project, the browser accesses http://localhost:8080/index, and the page displays:
index

Notes to RestController

RestController annotations are intended to make it easier to use @Controller and @ResponseBody.
@ ResponseBody modifies the controller method, and the return value of the method will be written into the HTTP response body. The returned content will not be placed in the model, nor will it 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;

@RestController
public class DemoController {

    @RequestMapping("/index")
    public String index(){
        return "index";
    }
}

3. Request Mapping Annotation

RequestMapping annotations can modify classes or methods, mainly for mapping requests and processing methods.
When used to modify classes and set URLs, it means that the URL prefix is set for each request.
RequestMapping annotations have the following properties:
(1) path and value: the url used to configure the mapping;
(2) method: mapping HTTP methods, such as GET, POST, PUT, DELETE;
You can also use several annotations that configure the method attribute of @RequestMapping by default:
@ GetMapping is equivalent to RequestMapping(method="RequestMethod.GET")
@ PostMapping, @PutMapping, @DeleteMapping are similar.
(3) params: configure parameter identifiers for mapping requests;
(4) consumers: configure the data type of the request, such as XML or JSON, etc.
(5) produces: configure the data type of the response, such as "application/json" returning 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 access:
http://localhost:8080/oa/index1
http://localhost:8080/oa/index2
http://localhost:8080/oa/index3
Pages are displayed separately:
index1
index2
index3

IV. PathVariable Annotations

The PathVariable annotation is mainly used to modify the method parameters, indicating that the method parameters are variables of the 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;

@RestController
public class DemoController {

    @GetMapping("/index1/{name}")
    public String index1(@PathVariable String name){
        return "index1: " + name;
    }

    //Can be@PathVariable Configure attribute values, explicitly bind method parameters and URL Variable value
    @GetMapping("/index2/{name}")
    public String index2(@PathVariable("name") String lc){
        return "index2: " + lc;
    }
}

Browsers access http://localhost:8080/index1/a
Page display:
a
Visit http://localhost:8080/index1/b
Page display:
b

V. RequestParam annotations

RequestParam annotations are used to obtain request parameters in the body of the request, such as page control name values after submission of the form.

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;

@RestController
public class DemoController {

    @PostMapping("/index1")
    public String index1(@RequestParam String userName){
        return userName;
    }

    //map Store all request parameters
    @PostMapping("/index2")
    public String index2(@RequestParam Map<String,String> map){
        String age = map.get("age");
        String sex = map.get("sex");
        return age + "," + sex;
    }
}

Create a new html file in the computer, such as desktop.

<html>
<body>
  <form method="post" action="http://localhost:8080/index1">
    <input type="text" name="userName" value="abc" />    
    <input type="submit" value="Submission 1" />
  </form>
  <form method="post" action="http://localhost:8080/index2">
    <input type="text" name="age" value="22" />
    <input type="password" name="sex" value="male" />    
    <input type="submit" value="Submission 2" />
  </form>
</body>
</html>

When the browser opens, if you click the "Submit 1" button, the page jumps to http://localhost:8080/index 1 and displays abc.
If you click the "Submit 2" button, the page jumps to http://localhost:8080/index2, showing 22,male.

VI. Document upload

File upload can be achieved using RequestParam annotations.

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;

@RestController
public class DemoController {

    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String filePath = "D:/";
        File dest = new File(filePath + fileName);
        file.transferTo(dest);
        return "Upload success";
    }

}

Create a new html file

<html>
<body>
  <form method="post" action="http://localhost:8080/upload" enctype="multipart/form-data">
    <input type="file" name="file" />    
    <input type="submit" value="Submission" />
  </form>  
</body>
</html>

After the browser opens, select a file, click Submit, and save the file to D disk.

Posted by renegade44 on Wed, 02 Oct 2019 00:23:18 -0700