Spring Boot 基礎框架以及特性
阿新 • • 發佈:2018-09-01
path weight header 刪除服務 file delet stp 查看 asp
})
@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()); } }
- SpringBoot工程
- 參數解析
- HTTP Method
- Request / Response / Session
- Error/重定向
- Logger
- IoC
- AOP/Aspect
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()); } }
Spring Boot 基礎框架以及特性