-
使用日志:
在HttpAspect中,注意引進(jìn)的是org.slf4j內(nèi)的包,
private final static Logger logger=LoggerFactory.getLogger(HttpAspect.class);
查看全部 -
使用AOP的步驟
1、在pom中引進(jìn)aop
<dependency>
<groupid>org.springframework.boot</>
<artifactId>spring-boot-starter-aop</>
</>
2、在*.aspect中創(chuàng)建HttpAspect
使用注解的方式:
@Before("execution(public * com.包名.*Controller.*(..))")
public void log(){
????System.out.println("這是會(huì)攔截的方法。。。");
}
查看全部 -
異常應(yīng)該自己定義一些,
查看全部 -
spring 拋異常 runtimeException時(shí)才會(huì)回滾 如果是exception就不會(huì)回滾
查看全部 -
使用@Valid表單驗(yàn)證
@Min
查看全部 -
Controller單元測(cè)試:
使用@AutoConfigureMockMvc注解,注入MockMvc類。
MockMvcRequestBuilders,使用".get()"/“.put()”/".post()"表示請(qǐng)求方式和訪問路徑。
".status()"獲取返回狀態(tài)碼,“isOk”判斷是否符合期望;
“.content()”獲取返回內(nèi)容。
在項(xiàng)目打包發(fā)布時(shí),springboot會(huì)自動(dòng)執(zhí)行單元測(cè)試用例,并在控制臺(tái)打印出執(zhí)行結(jié)果。
查看全部 -
利用IDEA創(chuàng)建測(cè)試類:
右鍵要測(cè)試的方法>GO TO>Test>Create New Test>選中要測(cè)試的方法
查看全部 -
單元測(cè)試注解
(1)@RunWith(SpringRunner.class) 表示此類在測(cè)試環(huán)境運(yùn)行;
(2)@SpringBootTest注解表示將啟動(dòng)整個(gè)spring工程;
(3)只測(cè)試某個(gè)方法時(shí),選中特定方法,Run“XXX()”
查看全部 -
定義枚舉類,表示異常信息,方便維護(hù)
查看全部 -
springboot框架只對(duì)RuntimeException異常進(jìn)行事務(wù)回滾,Exception異常不回滾。
查看全部 -
ServletRequestAttributes?attributes?=?(ServletRequestAttributes)?RequestContextHolder.getRequestAttributes(); HttpServletRequest?request?=?attributes.getRequest(); //url logger.info("url={}",request.getRequestURL()); //ip logger.info("id={}",request.getRemoteAddr()); //method logger.info("method={}",request.getMethod()); //類方法 logger.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName()?+?"."?+?joinPoint.getSignature().getName()); //參數(shù) logger.info("args={}",joinPoint.getArgs());
查看全部 -
ExceptionHandle必須放在springbootApplication同一個(gè)包下面嗎?
查看全部 -
//類名和類方法
request.getSignature().getDeclaringTypeName();
request.getSignature().getName;查看全部 -
ServletRequestAttribute attributes = requestContextHolder.getRequestAttributes();
查看全部 -
這里教了一種枚舉的使用方法
查看全部
舉報(bào)