目录
1. @Controller:用于声明一个控制器类。
2. @RequestMapping:用于映射请求 URL 到控制器类或处理方法上。
3. @RequestParam:用于获取请求参数的值。
4. @PathVariable:用于获取 URL 中的参数值。
5. @ResponseBody:用于将返回值转化为 JSON 格式或其他格式的数据。
6. @ModelAttribute:用于将请求参数绑定到模型对象上。
7. @Valid:用于验证请求参数。
8. @InitBinder:用于定义数据类型转换和格式化规则。
9. @SessionAttributes:用于将模型对象存储到会话中。
10. @ExceptionHandler:用于处理控制器方法中抛出的异常。
11. @RequestMapping(value=“”, method=RequestMethod.GET) 用于处理 GET 请求。
12. @RequestMapping(value=“”, method=RequestMethod.POST) 用于处理 POST 请求。
13. @RequestMapping(value=“”, method=RequestMethod.PUT) 用于处理 PUT 请求。
14. @RequestMapping(value=“”, method=RequestMethod.DELETE) 用于处理 DELETE 请求。
- @RequestMapping("/hello1")
- public String toHello1(Integer bid,String bname){
- log.info(">>>> 基础类型+String传参:{},{}",bid,bname);
- return "index";
- }
- package com.zking.web;
-
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- /**
- * @author bin人
- * @site www.zking.com
- * @company xy集团
- * @create 2023-09-05-14:44
- */
- @Slf4j
- @Controller
- @RequestMapping("/param")
- public class ParamController {
- @RequestMapping("/hello1")
- public String index(String bname,Integer bid){
- // System.out.println("放下屠刀");
- log.info("简单类型参数:bname:{},bid:{}",bname,bid);
- return "index";
- }
- }
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
-
Title 立地成佛

测试结果

(用了这个required=false必须要传参数,要不然进入不了方法)

必须要传bname要不然会报错,也接受不到值
传了值之后就可以正常访问了

- @RequestMapping("/hello4/{bid}")
- public String hello4(@PathVariable("bid") Integer bid){
- log.info("@PathVariable:bid:{}",bid);
- // fail..error warning info debug
- return "index";
- }


- @RequestMapping("/hello5/{bid}")
- public String hello5(Map map){
- log.info("@RequestBody:map:{}",map);
- // fail..error warning info debug
- return "index";
- }
- @RequestMapping("/hello6/{bid}")
- public String hello6(@RequestBody Map map){
- log.info("@RequestBody:map:{}",map);
- // fail..error warning info debug
- return "index";
- }
想要用这个注解就必须导入架包依赖
- "1.0" encoding="UTF-8"?>
-
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
4.0.0 -
-
com.zking -
yxssm -
1.0-SNAPSHOT -
war -
-
yxssm Maven Webapp -
-
http://www.example.com -
-
UTF-8 -
1.8 -
1.8 -
3.7.0 -
-
-
5.0.2.RELEASE -
-
3.4.5 -
-
5.1.44 -
-
5.1.2 -
-
1.3.1 -
-
2.1.1 -
2.4.3 -
-
2.9.1 -
3.2.0 -
1.7.13 -
-
4.12 -
4.0.0 -
1.18.2 -
-
1.2 -
1.1.2 -
-
5.0.2.RELEASE -
2.9.3 -
-
-
-
-
org.springframework -
spring-context -
${spring.version} -
-
-
org.springframework -
spring-orm -
${spring.version} -
-
-
org.springframework -
spring-tx -
${spring.version} -
-
-
org.springframework -
spring-aspects -
${spring.version} -
-
-
org.springframework -
spring-web -
${spring.version} -
-
-
org.springframework -
spring-test -
${spring.version} -
-
-
-
org.mybatis -
mybatis -
${mybatis.version} -
-
-
-
mysql -
mysql-connector-java -
${mysql.version} -
-
-
-
com.github.pagehelper -
pagehelper -
${pagehelper.version} -
-
-
-
org.mybatis -
mybatis-spring -
${mybatis.spring.version} -
-
-
-
org.apache.commons -
commons-dbcp2 -
${commons.dbcp2.version} -
-
-
commons-pool2 -
org.apache.commons -
-
-
-
-
org.apache.commons -
commons-pool2 -
${commons.pool2.version} -
-
-
-
-
-
-
org.slf4j -
slf4j-api -
${slf4j.version} -
-
-
org.slf4j -
jcl-over-slf4j -
${slf4j.version} -
runtime -
-
-
-
org.apache.logging.log4j -
log4j-api -
${log4j2.version} -
-
-
org.apache.logging.log4j -
log4j-core -
${log4j2.version} -
-
-
-
org.apache.logging.log4j -
log4j-slf4j-impl -
${log4j2.version} -
-
-
slf4j-api -
org.slf4j -
-
-
-
-
-
org.apache.logging.log4j -
log4j-web -
${log4j2.version} -
runtime -
-
-
-
com.lmax -
disruptor -
${log4j2.disruptor.version} -
-
-
-
junit -
junit -
${junit.version} -
-
-
-
javax.servlet -
javax.servlet-api -
${servlet.version} -
provided -
-
-
org.projectlombok -
lombok -
${lombok.version} -
provided -
-
-
-
org.springframework -
spring-webmvc -
${spring.version} -
-
-
jstl -
jstl -
${jstl.version} -
-
-
taglibs -
standard -
${standard.version} -
-
-
com.fasterxml.jackson.core -
jackson-databind -
${jackson.version} -
-
-
com.fasterxml.jackson.core -
jackson-core -
${jackson.version} -
-
-
com.fasterxml.jackson.core -
jackson-annotations -
${jackson.version} -
-
-
-
yxssm -
-
-
-
src/main/java -
-
**/*.xml -
-
-
-
-
src/main/resources -
-
jdbc.properties -
*.xml -
-
-
-
-
-
org.apache.maven.plugins -
maven-compiler-plugin -
${maven.compiler.plugin.version} -
-
${maven.compiler.source} -
${maven.compiler.target} -
${project.build.sourceEncoding} -
-
-
-
org.mybatis.generator -
mybatis-generator-maven-plugin -
1.3.2 -
-
-
-
mysql -
mysql-connector-java -
${mysql.version} -
-
-
-
true -
-
-
-
maven-clean-plugin -
3.1.0 -
-
-
-
maven-resources-plugin -
3.0.2 -
-
-
maven-compiler-plugin -
3.8.0 -
-
-
maven-surefire-plugin -
2.22.1 -
-
-
maven-war-plugin -
3.2.2 -
-
-
maven-install-plugin -
2.5.2 -
-
-
maven-deploy-plugin -
2.8.2 -
-
-
在导入依赖后我们还需要postman或者apipost/eolink等工具发送请求数据。 因为浏览器发送不了JSON数据请求。所以我们需要安装Eolink等第三方工具进行测试。
安装好后,按照以下步骤把参数填写好,query参数是往域名链接添加属性,如下:
请看录像
通过方法五跟方法六的对比,可得出@RequestBody适用于专门接受Json

- @RequestMapping("/hello7")
- public String hello7(@RequestHeader("jwt") String jwt){
- // System.out.println("刘三金去拿奶茶喽。。。");
- log.info("@RequestHeader参数:jwt:{}",jwt);
- // fail..error warning info debug
- return "index";
- }
-
- @RequestMapping("/hello8")
- public String hello8(Book book,
- @RequestBody Map map,
- @RequestHeader("jwt") String jwt){
- // System.out.println("刘三金去拿奶茶喽。。。");
- log.info("Book:Book:{}",book.toString());
- log.info("@RequestBody参数:Map:{}",map);
- log.info("@RequestHeader参数:jwt:{}",jwt);
- // fail..error warning info debug
- return "index";
- }

先写工具类:
- package com.zking.utils;
-
- import java.io.PrintWriter;
-
- import javax.servlet.http.HttpServletResponse;
-
- import com.fasterxml.jackson.databind.ObjectMapper;
-
- public class ResponseUtil {
-
- public static void write(HttpServletResponse response,Object o)throws Exception{
- response.setContentType("text/html;charset=utf-8");
- PrintWriter out=response.getWriter();
- out.println(o.toString());
- out.flush();
- out.close();
- }
-
- public static void writeJson(HttpServletResponse response,Object o)throws Exception{
- ObjectMapper om = new ObjectMapper();
- // om.writeValueAsString(o)代表了json串
- write(response, om.writeValueAsString(o));
- }
- }
- package com.zking.web;
-
- import com.zking.utils.ResponseUtil;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import javax.servlet.http.HttpServletResponse;
- import java.util.HashMap;
- import java.util.Map;
-
- /**
- * @author bing人
- * @site
- * @company xy集团
- * @create 2023-09-06 22:12
- */
- @Controller
- @RequestMapping("/rs")
- public class ReturnController {
- @RequestMapping("/hello1")
- public void hello1(HttpServletResponse response){
- Map
map=new HashMap<>(); - map.put("code",200);
- map.put("msg","成功添加。");
- try {
- ResponseUtil.writeJson(response,map);
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
- }

- @ResponseBody//响应json数据
- @RequestMapping("/hello2")
- public Map hello2(HttpServletResponse response) {
- Map
map = new HashMap<>(); - map.put("code", 200);
- map.put("msg", "成功添加。");
- return map;
- }

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

- //String+model
- @RequestMapping("/hello4")
- public String hello4(Model model, HttpServletRequest request) {
- model.addAttribute("currentName","浏阳鞭炮");
- request.setAttribute("location","来自于刘老板的");
- return "index";
- }


- // 场景一:转发到后台的某一个方法(当前类)
- @RequestMapping("/hello6")
- public String hello6(){
- System.out.println("hello6");
- return "forward:hello2";
- }
-
- // 场景二:转发到后台的某一个方法(其他类)
- @RequestMapping("/hello7")
- public String hello7(){
- System.out.println("hello7");
- return "forward:/param/hello1";
- }
- // 场景三:重定向到后台的某一个方法(当前类)
- @RequestMapping("/hello8")
- public String hello8(){
- System.out.println("hello8");
- return "forward:hello2";
- }
-
- // 场景四:重定向后台的某一个方法(其他类)
- @RequestMapping("/hello9")
- public String hello9(){
- System.out.println("hello9");
- return "forward:/param/hello1";
- }
