- SpringBoot工程
- 参数解析
- HTTP Method
- Request / Response / Session
- Error/重定向
- Logger
- IoC
- AOP/Aspect
1:SpringBoot工程
框架学习,首先接触看官方文档:(先看做什么,官方的文档细节先忽略,用到去查找)
start.spring.io
//controller演示
public class IndexController {
@RequestMapping(path = { "/","/index"})
@ResponseBody
public String Index( ) {
return " name ";
}
}
2:参数解析
//controller携带参数的演示,路径里面的参数可以解析到函数里面
@RequestMapping(value = "/profile/{groupId}/{userId}")
@ResponseBody
public String profile(@PathVariable("groupId") String groupId,
@PathVariable("userId") int userId,
@RequestParam(value = "type", defaultValue = "1") int type,
@RequestParam(value = "key", defaultValue = "nowcoder") String key) {
return String.format("{%s},{%d},{%d},{%s}", groupId, userId, type, key);
}
//设置 type =
//设置 key =
//controller携带参数,并且携带@requestparam
@RequestMapping(value = "/profile/{groupId}/{userId}")
@ResponseBody
public String profile(@PathVariable("groupId") String groupId,
@PathVariable("userId") int userId,
@RequestParam(value = "type", defaultValue = "1") int type,
@RequestParam(value = "key", defaultValue = "nowcoder") String key) {
return String.format("{%s},{%d},{%d},{%s}", groupId, userId, type, key);
}
3:HTTP Method
HTTP Method(代码演示)
GET 获取接口信息
HEAD 紧急查看接口HTTP的头
POST 提交数据到服务器
PUT 支持幂等性的POST //执行两次是一样的结果;
DELETE 删除服务器上的资源
OPITIONS 查看支持的方法
可以设置get post
Fidder web debugger工具
4:Request / Response / Session
request HttpServletResponse
参数解析 response.addCookie(new
Cookie(key, value));
response.addHeader(key, value);
cookie读取
http请求字段
文件上传
HttpServletRequest
request.getHeaderNames();
request.getMethod()
request.getPathInfo()
request.getQueryString()
response
页面内容返回
cookie下发
http字段设置,headers
5:Error/重定向
//重定向
//301:永久转移
//302:临时转移
//异常的统一处理
@RequestMapping(path = { "/admin"}, method = { RequestMethod.GET})
@ResponseBody
public String admin(@RequestParam("key") String key) {
if ("admin".equals( key )) {
return "hello admin";
}
throw new IllegalArgumentException( "参数不对" );
}
@ExceptionHandler()
@ResponseBody
public String error(Exception e) {
return "出现了错误error:" + e.getMessage();
}
6:IoC 控制反转:无需关注对象的初始化(享元模式)
servicecs包下面:通过标记@Services来设置对象,
controller包下面:通过@Autowired,直接引入对象无需初始化。不需要new Services中的对象;
7:AOP/Aspect 面向切面
@Aspect
@Component
public class LogAspect {
private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);
@Before("execution(* com.nowcoder.controller.*Controller.*(..))")
public void beforeMethod(JoinPoint joinPoint) {
StringBuilder sb = new StringBuilder();
for (Object arg : joinPoint.getArgs()) {
sb.append("arg:" + arg.toString() + "|");
}//切点打印参数
logger.info("before method:" + sb.toString());
}
@After("execution(* com.nowcoder.controller.IndexController.*(..))")
public void afterMethod() {
logger.info("after method" + new Date());
}
}