二、参数传递
在讲解之前,先向大家介绍一下本次需要用到的依赖slf4j
SLF4J是一个简单的日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种各样的日志系统。按照官方的说法,SLF4J是一个用于日志系统的简单Facade,允许最终用户在部署其应用时使用其所希望的日志系统。
SLF4J的作用是提供了一种抽象层,使得开发人员可以使用任何他们喜欢的日志框架,而不必关心它们之间的差异。这样可以使开发人员更专注于编写代码,而不是处理日志记录的细节。
在实际运用中,SLF4J通常用于将日志记录从应用程序代码中分离出来,并将其委托给专门的日志记录器。这样可以使得应用程序更易于维护和扩展,并且可以提高性能和可靠性。
pom.xml
<log4j2.version>2.9.1</log4j2.version> <log4j2.disruptor.version>3.2.0</log4j2.disruptor.version> <slf4j.version>1.7.13</slf4j.version> //------------------------------------------------- <!--4.log日志相关依赖--> <!-- log4j2日志相关依赖 --> <!-- log配置:Log4j2 + Slf4j --> <!-- slf4j核心包--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${slf4j.version}</version> <scope>runtime</scope> </dependency> <!--核心log4j2jar包--> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>${log4j2.version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${log4j2.version}</version> </dependency> <!--用于与slf4j保持桥接--> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>${log4j2.version}</version> </dependency> <!--web工程需要包含log4j-web,非web工程不需要--> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-web</artifactId> <version>${log4j2.version}</version> <scope>runtime</scope> </dependency> <!--需要使用log4j2的AsyncLogger需要包含disruptor--> <dependency> <groupId>com.lmax</groupId> <artifactId>disruptor</artifactId> <version>${log4j2.disruptor.version}</version> </dependency>
2.1.基础类型+String
Controller编写:
@Slf4j @Controller @RequestMapping("/csdn") public class IndexController { @RequestMapping("/list") public String list(String name,Integer age){ log.info("基础类型+String➡name:{},age:{}",name,age); return "hello"; } }
此时请求映射的请求路径为:
http://localhost:8080/Spring_MyBatis/csdn/list?name=彭于晏&age=18
控制台打印结果:
2.2.复杂类型
Controller编写:
@Slf4j @Controller @RequestMapping("/csdn") public class IndexController { @RequestMapping("/list01") public String list01(Book book, HttpServletRequest req){ log.info("复杂类型:bid:{},bname:{}", req.getParameter("bid"), req.getParameter("bname")); log.info("book:{}",book.toString()); return "hello"; } }
此时请求映射的请求路径为:
http://localhost:8080/Spring_MyBatis/csdn/list01?bid=01&bname=斗破苍穹
控制台打印结果:
2.3.@RequestParam
Controller编写:
@Slf4j @Controller @RequestMapping("/csdn") public class IndexController { @RequestMapping("/list02") public String list02( @RequestParam(required = false) String name, @RequestParam Integer age) { log.info("@RequestParam注解传递参数name:{},age:{}",name,age); return "hello"; } }
此时请求映射的请求路径为:
http://localhost:8080/Spring_MyBatis/csdn/list02?name=彭于晏&age=18
控制台打印结果:
如果我们少传递一个age结果会是怎么样的呢 ?
http://localhost:8080/Spring_MyBatis/csdn/list02?name=彭于晏
结论:被@RequestParam注解的参数required默认为true表示请求中一定要传入对应的参数,否则会报404错误如果设置为false时,当请求中没有此参数,将会默认为null,而对于基本数据类型的变量,则必须有值,这时会抛出空指针异常。如果允许空值,则接口中变量需要使用包装类来声明。
2.4.@PathVariable
Controller编写:
@Slf4j @Controller @RequestMapping("/csdn") public class IndexController { @RequestMapping("/list03/{id}") public String list03(@PathVariable("id") Integer id) { log.info("@PathVariable: id{}",id); return "hello"; } }
此时请求映射的请求路径为:
http://localhost:8080/Spring_MyBatis/csdn/list03/7
控制台打印结果:
2.5.@RequestBody
讲解@RequestBody之前,将大家推荐postman或者apipost/eolink等工具发送请求数据。
这些工具可以帮助您模拟发送请求和接收响应,以便更好地理解@RequestBody参数的含义和使用方法。
Controller编写:
@Slf4j @Controller @RequestMapping("/csdn") public class IndexController { @RequestMapping("/list04") public String list04(Map map) { log.info("没有注解: Map:{}",map); return "hello"; } @RequestMapping("/list05") public String list05(@RequestBody Map map) { log.info("@ResponseBody: Map:{}",map); return "hello"; } }
此时请求映射的请求路径为:
http://localhost:8080/Spring_MyBatis/csdn/list04
eolink测试工具[没有注解]:
控制台打印结果:
eolink测试工具[有注解]:
控制台打印结果:
2.6.@RequestHeader
Controller编写:
@Slf4j @Controller @RequestMapping("/csdn") public class IndexController { @RequestMapping("/list06") public String list06(@RequestHeader String jwt) { log.info("@RequestHeader: jwt:{}",jwt); return "hello"; } }
此时请求映射的请求路径为:
http://localhost:8080/Spring_MyBatis/csdn/list06
控制台打印结果:
三、返回值
为了方便模拟效果,借助ResponseUtil工具类,ResponseUtil类提供了一种方便的方式来将对象以文本或JSON格式写入HTTP响应流中,以便在Web应用程序中向客户端返回数据。
ResponseUtil
package com.csdn.xw.util; import com.fasterxml.jackson.databind.ObjectMapper; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; 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)); } }
pom.xml依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.4</version> <!-- 请根据您的需求选择合适的版本 --> </dependency>