lombok |
描述 |
lombok |
消除 POJO 冗長 |
validator |
描述 |
validator |
校驗框架 |
mapstruct |
描述 |
mapstruct |
映射框架 |
@Data
public class User {
public String name;
public Integer age;
}
@Data
public class Person {
public String PersonName;
public Integer PersonAge;
}
@Mapper
public interface PersonToUser {
PersonToUser MAPPER = Mappers.getMapper(PersonToUser.class);
@Mappings({
@Mapping(target = "name", source = "PersonName"),
@Mapping(target = "age", source = "PersonAge")
})
User personToUser(User user);
}
public static void main(String[] args) {
User user = new User();
user.setName("xx");
user.setAge(18);
User u = PersonToUser.MAPPER.personToUser(user);
System.out.println(u);
}
自定義注解 |
描述 |
@Target |
@Target表示 Annotation 所修飾的對象范圍 |
@Retention |
@Retention表示Annotation聲明周期旺隙,RUNTIME:運行時保留 |
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Custom{
String value(); //注解的屬性绒极,如果只有一個屬性,一般叫value
String name() default ""; //屬性蔬捷,默認(rèn)值""垄提,可以不寫
}
spring AOP注解 |
描述 |
@Apsect |
將當(dāng)前類標(biāo)識為一個切面 |
@Pointcut |
定義切點,這里使用的是條件表達式 |
如: "execution(* com.travels.controller.AlipayController .groupOrderPay(..))" |
com.travels.controller.AlipayController代表調(diào)用類周拐,groupOrderPay(..)代表調(diào)用方法铡俐,任意參數(shù) |
如:@annotation(com.travels.controller.AlipayController.groupOrderPay) |
匹配注解類 |
@Before |
前置增強,就是在目標(biāo)方法執(zhí)行之前執(zhí)行妥粟; |
@AfterReturning |
后置增強审丘,方法退出時執(zhí)行; |
@AfterThrowing |
有異常時該方法執(zhí)行勾给; |
@AfterThrowing |
有異常時該方法執(zhí)行滩报; |
@After |
最終增強,無論什么情況都會執(zhí)行播急; |
@Afround |
環(huán)繞增強 |
三元表達式 |
描述 |
三元表達式 |
return a>b?a:b |
Lambda表達式 |
描述 |
Lambda表達式 |
允許把函數(shù)作為一個方法的參數(shù),簡化匿名內(nèi)部類 |
@FunctionalInterface |
函數(shù)式接口:一個接口脓钾,然后就是在這個接口里面只能有一個抽象方法,加上了@FunctionInterface只能有一個抽象方法,否則編譯器會報錯 |
Supplier |
提供者 指定一個泛型并返回 |
Consumer |
消費者 指定一個泛型傳入桩警,沒有返回值 |
Predicate |
輸入?yún)?shù)進行判斷可训,然后返回布爾值 |
Function |
兩個參數(shù),T輸入 R輸出 |
@FunctionalInterface
public interface MyFunctional{
void myFunctional();
}
public class MyFunctionalTest {
public static void main(String[] args) {
MyFunctional myFunctional = () -> System.out.println("myFunctional");
myFunctional.myFunctional();
}
}
@FunctionalInterface
public interface MyFunctional{
String myFunctional(); //有返回值類型
}
public class MyFunctionalTest {
public static void main(String[] args) {
String str = "string";
MyFunctional myFunctional = () -> {return str;};
System.out.println(myFunctional.myFunctional());
}
}
public class SupplierTest {
public static String getString(Supplier<String> supplier) {
return supplier.get();
}
public static void main(String[] args) {
String str = getString(() -> {return "get string";});
System.out.println(str);
}
}
public class ConsumerTest {
public static void consumerTest(String string,Consumer<String> con) {
con.accept(string);
}
public static void main(String[] args) {
String str = "Consumer... ";
consumerTest(str, (String string) -> { System.out.println(str);});
}
}
public class PredicateTest {
public static Boolean check(String string,Predicate<String> pre) {
return pre.test(string);
}
public static void main(String[] args) {
String str = "true";
Boolean bl = check(str, (String string) -> { return true;});
System.out.println(bl);
}
}
public class FunctionTest {
public static int change(String string,Function<String, Integer> fun) {
Integer integer = fun.apply(string);
return integer;
}
public static void main(String[] args) {
String str = "1";
Integer integer = change(str, (String string) -> {return Integer.parseInt(str);});
System.out.println(integer);
}
}
Stream |
描述 |
Stream |
一種鏈?zhǔn)胶瘮?shù)式編程捶枢,流終結(jié)了就不能再次操作握截,簡化Collection集合操作 |
void forEach(Consumer<? super T> action); |
用來便利,終結(jié)方法 |
Stream<T> filter(Predicate<? super T> predicate); |
用于過濾烂叔,返回符合條件的元素 |
<R> Stream<R> map(Function<? super T, ? extends R> mapper); |
輸入一個參數(shù)谨胞,返回一個參數(shù) |
long count(); |
終結(jié)方法,返回元素個數(shù) |
Stream<T> limit(long maxSize); |
截取前n個元素 |
Stream<T> skip(long n); |
跳過前n個元素 |
public class StreamTest {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.stream()
.filter((String numeral) -> { return numeral.startsWith("1");})
.map((String numeral) -> {return Integer.parseInt(numeral);}).limit(1)
.forEach((Integer numeral) -> {System.out.println(numeral);});
}
}
Filter |
描述 |
Filter |
過濾器 |
init |
初始化 |
doFilter |
攔截 |
destroy |
消耗 |
public class FilterTest implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
}
@Override
public void destroy() {
// TODO Auto-generated method stub
Filter.super.destroy();
}
}
過濾器 |
描述 |
HandlerInterceptor 接口 |
|
HandlerInterceptorAdapter 抽象類 |
Spring MVC封裝 |
preHandle |
方法調(diào)用前執(zhí)行 |
postHandle |
方法執(zhí)行后調(diào)用 |
afterCompletion |
請求處理完畢后回調(diào) |
public class HandlerInterceptorTest implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// TODO Auto-generated method stub
return HandlerInterceptor.super.preHandle(request, response, handler);
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
public class HandlerInterceptorAdapterTest extends HandlerInterceptorAdapter{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// TODO Auto-generated method stub
return super.preHandle(request, response, handler);
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
super.afterCompletion(request, response, handler, ex);
}
}
異常處理 |
描述 |
異常處理 |
@RestControllerAdvice + @ExceptionHandler優(yōu)化try catch |
public class MyException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
private String message;
/**
* 自定義異常
*
* @param message :異常描述
*/
public MyException(String message) {
this.message = message;
}
}
@RestControllerAdvice
public class ExceptionResponseConfig {
@ExceptionHandler(value = MyException.class)
public Map<String, Object> myExceptionHandler(MyException exception) {
return StateCode.getMap(StateCode.OPERATION_ERROR, exception.getMessage());
}
public void test() throws MyException
{
throw new MyException("自定義異常");
}
}