自定义拦截器
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 自定义拦截器
*/
@Configuration
public class MyInterceptor extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
HandlerInterceptor inter = new HandlerInterceptor() {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("request:"+request.getRequestURL());
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
};
registry.addInterceptor(inter).addPathPatterns("/**");
}
}
全局异常处理类
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.HashMap;
import java.util.Map;
/**
* 全局异常处理类
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public Map<String,Object> handleException(Exception e){
Map<String, Object> map = new HashMap<>();
map.put("errorCode",500);
map.put("errorMsg",e.toString());
return map;
}
}
异步调用
@SpringBootApplication(scanBasePackages = {"com.fly"})
@EnableAsync
public class SpringDemoApp{
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import java.util.Random;
import java.util.concurrent.Future;
@Service
public class AsyncServiceImpl {
private static Random random = new Random();
@Async
public Future<String> doTask1() throws Exception{
System.out.println("doTask1 start");
long b = System.currentTimeMillis();
Thread.sleep(random.nextInt(1000));
long e = System.currentTimeMillis();
System.out.println("doTask1 end:"+(e-b));
return new AsyncResult<>("doTask1 end");
}
@Async
public Future<String> doTask2() throws Exception{
System.out.println("doTask2 start");
long b = System.currentTimeMillis();
Thread.sleep(random.nextInt(1000));
long e = System.currentTimeMillis();
System.out.println("doTask2 end:"+(e-b));
return new AsyncResult<>("doTask2 end");
}
@Async
public Future<String> doTask3() throws Exception{
System.out.println("doTask3 start");
long b = System.currentTimeMillis();
Thread.sleep(random.nextInt(1000));
long e = System.currentTimeMillis();
System.out.println("doTask3 end:"+(e-b));
return new AsyncResult<>("doTask3 end");
}
}
@Autowired
private AsyncServiceImpl asyncService;
@RequestMapping("/asyn")
public String asyn() throws Exception {
long b = System.currentTimeMillis();
Future<String> f1 = asyncService.doTask1();
Future<String> f2 = asyncService.doTask2();
Future<String> f3 = asyncService.doTask3();
while (true){
if (f1.isDone()&&f2.isDone()&&f3.isDone()){
break;
}
}
long e = System.currentTimeMillis();
return "asyn end:"+(e-b);
//doTask2 start
//doTask1 start
//doTask3 start
//doTask1 end:290
//doTask2 end:607
//doTask3 end:795
//"asyn end:802"
}