merge
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package com.xwbing;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.web.support.SpringBootServletInitializer;
|
||||
|
||||
/**
|
||||
* 项目名称: boot-module-demo
|
||||
* 创建时间: 2017/11/2 18:19
|
||||
* 作者: xiangwb
|
||||
* 说明: 启动类
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class Application extends SpringBootServletInitializer {
|
||||
@Override
|
||||
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||
return application.sources(Application.class);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.xwbing.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 说明: 自定义日志信息注解
|
||||
* 项目名称: sbdemo
|
||||
* 创建时间: 2017/5/10 16:36
|
||||
* 作者: xiangwb
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
@Documented
|
||||
public @interface LogInfo {
|
||||
String value() default "";
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.xwbing.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
/**
|
||||
* 说明: 程序上下文配置
|
||||
* 项目名称: sbdemo
|
||||
* 创建时间: 2017/5/10 16:36
|
||||
* 作者: xiangwb
|
||||
*/
|
||||
@Configuration//相当于.xml文件中的<beans></beans>
|
||||
//@ComponentScan(value = {"com.xwbing"}, excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)})
|
||||
//@Import(xxx.class)//用来导入其他配置类
|
||||
//@ImportResource("classpath:applicationContext.xml")//用来加载其他xml配置文件
|
||||
public class ApplicationContextConfig {
|
||||
/**
|
||||
* 线程池
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean(name = "taskExecutor")//相当于XML中的<bean></bean>
|
||||
public static ThreadPoolTaskExecutor getPoolTaskExecutor() {
|
||||
ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
|
||||
poolTaskExecutor.setCorePoolSize(5);//核心线程数
|
||||
poolTaskExecutor.setMaxPoolSize(1000);//最大线程数
|
||||
poolTaskExecutor.setKeepAliveSeconds(30000);//空闲线程的存活时间
|
||||
poolTaskExecutor.setQueueCapacity(200);//队列最大长度
|
||||
return poolTaskExecutor;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.xwbing.configuration;
|
||||
|
||||
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.web.filter.CharacterEncodingFilter;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 说明: 程序配置
|
||||
* 项目名称: sbdemo
|
||||
* 创建时间: 2017/5/10 16:36
|
||||
* 作者: xiangwb
|
||||
*/
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
//@ComponentScan(basePackages = {"com.xwbing.controller"}, includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = RestController.class)})
|
||||
public class DispatcherServletConfig extends WebMvcConfigurerAdapter {
|
||||
private static final Logger logger = LoggerFactory.getLogger(DispatcherServletConfig.class);
|
||||
|
||||
/***
|
||||
* 添加拦截器
|
||||
* @param registry
|
||||
*/
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
logger.info("addInterceptors......");
|
||||
//拦截器1:登录判断
|
||||
// registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**");
|
||||
// registry.addInterceptor(new LoginInterceptor())
|
||||
// .addPathPatterns("/**").excludePathPatterns("/user/login", "/servlet/captchaCode", "/doc.html", "/swagger-ui.html");
|
||||
//拦截器2...
|
||||
super.addInterceptors(registry);
|
||||
}
|
||||
|
||||
/**
|
||||
* 扩展消息转换器,增加fastjson
|
||||
*
|
||||
* @param converters
|
||||
*/
|
||||
@Override
|
||||
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
|
||||
converters.add(getFastJsonHttpMessageConverter());
|
||||
super.configureMessageConverters(converters);
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求处理程序映射适配器,使用fastJson
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public HttpMessageConverter getFastJsonHttpMessageConverter() {
|
||||
FastJsonHttpMessageConverter httpMessageConverter = new FastJsonHttpMessageConverter();
|
||||
List<MediaType> mediaTypes = new ArrayList<>();
|
||||
mediaTypes.add(MediaType.TEXT_HTML);//避免IE出现下载JSON文件的情况
|
||||
mediaTypes.add(MediaType.APPLICATION_JSON);
|
||||
mediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
|
||||
mediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
|
||||
httpMessageConverter.setSupportedMediaTypes(mediaTypes);
|
||||
return httpMessageConverter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传解析器
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public CommonsMultipartResolver getCommonsMultipartResolver() {
|
||||
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
|
||||
multipartResolver.setMaxUploadSize(104857600);
|
||||
multipartResolver.setDefaultEncoding("UTF-8");
|
||||
return multipartResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* encoding编码问题
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public Filter characterEncodingFilter() {
|
||||
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
|
||||
characterEncodingFilter.setEncoding("UTF-8");
|
||||
characterEncodingFilter.setForceEncoding(true);
|
||||
return characterEncodingFilter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.xwbing.configuration;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
/**
|
||||
* 说明: 定时任务定时类
|
||||
* 项目名称: sbdemo
|
||||
* 创建时间: 2017/5/10 16:36
|
||||
* 作者: xiangwb
|
||||
*/
|
||||
@Configuration
|
||||
@EnableScheduling // 启用定时任务
|
||||
public class SchedulingConfig {
|
||||
private static final Logger logger = LoggerFactory.getLogger(SchedulingConfig.class);
|
||||
|
||||
@Scheduled(cron = "0 */30 * * * ?") // 每半小时执行一次
|
||||
public void scheduler() {
|
||||
logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> scheduled ... ");
|
||||
//TODO
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.xwbing.configuration;
|
||||
|
||||
import com.xwbing.util.captcah.CaptchaServlet;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 说明: 统一的servlet配置
|
||||
* 项目名称: sbdemo
|
||||
* 创建时间: 2017/5/10 16:36
|
||||
* 作者: xiangwb
|
||||
*/
|
||||
@Configuration
|
||||
public class ServletConfig {
|
||||
@Bean
|
||||
public CaptchaServlet captchaServlet() {
|
||||
return new CaptchaServlet();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ServletRegistrationBean captchaServletRegistrationBean(CaptchaServlet captchaServlet) {
|
||||
ServletRegistrationBean registration = new ServletRegistrationBean(captchaServlet);
|
||||
registration.setEnabled(true);
|
||||
registration.addUrlMappings("/servlet/captchaCode");
|
||||
return registration;
|
||||
}
|
||||
/********************************************/
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.xwbing.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
/**
|
||||
* 说明: swagger config
|
||||
* 项目名称: sbdemo
|
||||
* 创建时间: 2017/5/10 16:36
|
||||
* 作者: xiangwb
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
@EnableSwagger2
|
||||
public class SwaggerConfig {
|
||||
@Bean
|
||||
public Docket createRestApi() {
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.apiInfo(apiInfo())
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.basePackage("com.xwbing.controller"))
|
||||
.paths(PathSelectors.any())
|
||||
.build();
|
||||
}
|
||||
|
||||
private ApiInfo apiInfo() {
|
||||
return new ApiInfoBuilder()
|
||||
.title("管理系统RESTful APIs")
|
||||
.description("管理系统api接口,包含登录、注册.......")
|
||||
.termsOfServiceUrl("http://localhost:8080/swagger-ui.html")
|
||||
.contact("xiangwb")
|
||||
.version("1.0")
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.xwbing.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xwbing.service.MailService;
|
||||
import com.xwbing.util.JSONObjResult;
|
||||
import com.xwbing.util.RestMessage;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 项目名称: sb-demo
|
||||
* 创建时间: 2017/9/18 13:42
|
||||
* 作者: xiangwb
|
||||
* 说明:邮件测试控制层
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/mail/")
|
||||
public class MailTestControl {
|
||||
@Autowired
|
||||
private MailService mailService;
|
||||
|
||||
/**
|
||||
* 发送纯文本邮件
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("sendSimpleMail")
|
||||
public JSONObject sendSimpleMail() {
|
||||
RestMessage restMessage = mailService.sendSimpleMail("786461501@qq.com", "测试邮件", "收到一个纯文本邮件");
|
||||
return JSONObjResult.toJSONObj(restMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送html格式邮件
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("sendHtmlMail")
|
||||
public JSONObject sendHtmlMail() {
|
||||
String content = "<html>" +
|
||||
"<body>" +
|
||||
"<h3>hello world ! 这是一封Html邮件!</h3>" +
|
||||
"</body>" +
|
||||
"</html>";
|
||||
RestMessage restMessage = mailService.sendHtmlMail("786461501@qq.com", "html测试邮件", content);
|
||||
return JSONObjResult.toJSONObj(restMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送带附件邮件
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("sendAttachmentsMail")
|
||||
public JSONObject sendAttachmentsMail() {
|
||||
String path = "C:\\Users\\admin\\Desktop\\qq.txt";
|
||||
RestMessage restMessage = mailService.sendAttachmentsMail("786461501@qq.com", "附件测试邮件", "收到一个带附件邮件", path);
|
||||
return JSONObjResult.toJSONObj(restMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送文本内嵌图片邮件
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("sendInlineResourceMail")
|
||||
public JSONObject sendInlineResourceMail() {
|
||||
String rscId = "pic";
|
||||
String content = "<html><body>这是有图片的邮件:<img src=\'cid:" + rscId + "\'></body></html>";
|
||||
String imgPath = "C:\\Users\\admin\\Desktop\\0000001.png";
|
||||
RestMessage restMessage = mailService.sendInlineResourceMail("786461501@qq.com", "内嵌图片测试邮件", content, imgPath, rscId);
|
||||
return JSONObjResult.toJSONObj(restMessage);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.xwbing.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xwbing.domain.SysConfig;
|
||||
import com.xwbing.service.SysConfigService;
|
||||
import com.xwbing.util.JSONObjResult;
|
||||
import com.xwbing.util.RestMessage;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 说明: 系统配置控制层
|
||||
* 项目名称: sbdemo
|
||||
* 创建时间: 2017/5/10 16:36
|
||||
* 作者: xiangwb
|
||||
*/
|
||||
@Api("系统配置")
|
||||
@RestController
|
||||
@RequestMapping("/config/")
|
||||
public class SysConfigControl {
|
||||
private static Logger log = LoggerFactory.getLogger(SysConfigControl.class);
|
||||
@Autowired
|
||||
private SysConfigService sysConfigService;
|
||||
|
||||
@ApiOperation(value = "新增", notes = "新增系统配置信息")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "code", value = "配置项的key,长度为1-50", paramType = "query", required = true, dataType = "string"),
|
||||
@ApiImplicitParam(name = "value", value = "配置项的值", paramType = "query", required = true, dataType = "string"),
|
||||
@ApiImplicitParam(name = "isEnable", value = "是否启用,格式为Y|N", paramType = "query", required = true, dataType = "string"),
|
||||
@ApiImplicitParam(name = "name", value = "配置名称,长度为1-10", paramType = "query", required = true, dataType = "string")
|
||||
})
|
||||
@PostMapping("save")
|
||||
public JSONObject save(@Valid SysConfig sysConfig) {
|
||||
String logMsg = "新增系统配置信息";
|
||||
log.info(logMsg);
|
||||
RestMessage result = sysConfigService.save(sysConfig);
|
||||
return JSONObjResult.toJSONObj(result);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "删除", notes = "根据code删除系统配置信息")
|
||||
@ApiImplicitParam(name = "code", value = "配置项的code", paramType = "query", required = true, dataType = "string")
|
||||
@PostMapping("removeByCode/{code}")
|
||||
public JSONObject removeByCode(@PathVariable String code) {
|
||||
String logMsg = "删除系统配置信息";
|
||||
log.info(logMsg + " code:{}", code);
|
||||
if (StringUtils.isEmpty(code)) {
|
||||
return JSONObjResult.toJSONObj("code不能为空");
|
||||
}
|
||||
RestMessage result = sysConfigService.removeByCode(code);
|
||||
return JSONObjResult.toJSONObj(result);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "查找", notes = "根据key查找系统配置信息")
|
||||
@ApiImplicitParam(name = "code", value = "配置项的code", paramType = "query", required = true, dataType = "string")
|
||||
@PostMapping("findByCode")
|
||||
public JSONObject findByCode(@RequestParam String code) {
|
||||
String logMsg = "根据code查找系统配置信息";
|
||||
log.info(logMsg + " code:{}", code);
|
||||
if (StringUtils.isEmpty(code)) {
|
||||
return JSONObjResult.toJSONObj("code不能为空");
|
||||
}
|
||||
SysConfig one = sysConfigService.findByCode(code);
|
||||
if (Objects.isNull(one)) {
|
||||
return JSONObjResult.toJSONObj("该对象不存在");
|
||||
}
|
||||
return JSONObjResult.toJSONObj(one, true, "");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.xwbing.controller;
|
||||
|
||||
import com.xwbing.annotation.LogInfo;
|
||||
import com.xwbing.redis.RedisService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 说明:
|
||||
* 项目名称: sbdemo
|
||||
* 创建时间: 2017/5/5 9:21
|
||||
* 作者: xiangwb
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/test/")
|
||||
public class TestControl {
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
private final Logger logger = LoggerFactory.getLogger(TestControl.class);
|
||||
|
||||
@LogInfo("redisLog")
|
||||
@GetMapping("redis")
|
||||
public void redis() {
|
||||
redisService.set("redis", "xwbing");
|
||||
String s = redisService.get("redis");
|
||||
System.out.println(s);
|
||||
logger.info("获取的数据为:" + s);
|
||||
}
|
||||
|
||||
@LogInfo("log")
|
||||
@GetMapping("log")
|
||||
public void log() {
|
||||
logger.info("info");
|
||||
logger.error("error");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.xwbing.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xwbing.domain.SysUser;
|
||||
import com.xwbing.service.UserService;
|
||||
import com.xwbing.util.JSONObjResult;
|
||||
import com.xwbing.util.RestMessage;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 说明: user
|
||||
* 项目名称: sbdemo
|
||||
* 创建时间: 2017/5/10 16:36
|
||||
* 作者: xiangwb
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/user/")
|
||||
public class UserControl {
|
||||
private final static Logger log = LoggerFactory.getLogger(UserControl.class);
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@PostMapping("save")
|
||||
public JSONObject save(@Valid SysUser sysUser) {
|
||||
RestMessage result = userService.save(sysUser);
|
||||
return JSONObjResult.toJSONObj(result);
|
||||
}
|
||||
|
||||
@PostMapping("removeById")
|
||||
public JSONObject removeById(@RequestParam String id) {
|
||||
if (StringUtils.isEmpty(id)) {
|
||||
return JSONObjResult.toJSONObj("主键不能为空");
|
||||
}
|
||||
RestMessage result = userService.removeById(id);
|
||||
return JSONObjResult.toJSONObj(result);
|
||||
}
|
||||
|
||||
@PostMapping("update")
|
||||
public JSONObject update(SysUser sysUser) {
|
||||
if (StringUtils.isEmpty(sysUser.getId())) {
|
||||
return JSONObjResult.toJSONObj("主键不能为空");
|
||||
}
|
||||
RestMessage result = userService.update(sysUser);
|
||||
return JSONObjResult.toJSONObj(result);
|
||||
}
|
||||
|
||||
@GetMapping("findById")
|
||||
public JSONObject findById(String id) {
|
||||
if (StringUtils.isEmpty(id)) {
|
||||
return JSONObjResult.toJSONObj("主键不能为空");
|
||||
}
|
||||
SysUser sysUser = userService.findOne(id);
|
||||
if (Objects.isNull(sysUser)) {
|
||||
return JSONObjResult.toJSONObj("未查到该对象");
|
||||
}
|
||||
return JSONObjResult.toJSONObj(sysUser, true, "");
|
||||
}
|
||||
|
||||
@GetMapping("findList")
|
||||
public JSONObject findList() {
|
||||
List<SysUser> list = userService.findList();
|
||||
return JSONObjResult.toJSONObj(list, true, "");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.xwbing.handler;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xwbing.exception.BusinessException;
|
||||
import com.xwbing.util.JSONObjResult;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 说明: GlobalExceptionHandler
|
||||
* 项目名称: sbdemo
|
||||
* 创建时间: 2017/5/10 16:36
|
||||
* 作者: xiangwb
|
||||
*/
|
||||
// 作用在所有注解了@RequestMapping的控制器的方法上
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
private final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||
|
||||
/**
|
||||
* 自定义业务异常
|
||||
*
|
||||
* @param request
|
||||
* @param ex
|
||||
* @return
|
||||
*/
|
||||
// 拦截处理控制器里对应的异常。
|
||||
@ExceptionHandler(value = BusinessException.class)
|
||||
// 返回给页面200状态码
|
||||
@ResponseStatus(value = HttpStatus.OK)
|
||||
@ResponseBody
|
||||
public JSONObject handlerGuideException(HttpServletRequest request,
|
||||
Exception ex) {
|
||||
logger.error(ex.getMessage());
|
||||
return JSONObjResult.toJSONObj(ex.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 表单检验(validator) 异常
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
* @param ex
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(value = BindException.class)
|
||||
@ResponseBody
|
||||
public JSONObject handlerBindException(HttpServletRequest request,
|
||||
HttpServletResponse response, BindException ex) {
|
||||
|
||||
List<ObjectError> list = ex.getAllErrors();
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
for (ObjectError objectError : list) {
|
||||
if (stringBuffer.length() > 0)
|
||||
stringBuffer.append(" && ");
|
||||
stringBuffer.append(objectError.getDefaultMessage());
|
||||
}
|
||||
logger.error(stringBuffer.toString());
|
||||
response.setStatus(HttpStatus.OK.value());
|
||||
return JSONObjResult.toJSONObj(stringBuffer.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param request
|
||||
* @param ex
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
@ResponseBody
|
||||
public JSONObject handlerException(HttpServletRequest request,
|
||||
Exception ex) {
|
||||
logger.error(ex.getMessage());
|
||||
return JSONObjResult.toJSONObj("系统异常,请联系管理员");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.xwbing.handler;
|
||||
|
||||
import com.xwbing.configuration.DispatcherServletConfig;
|
||||
import com.xwbing.constant.CommonConstant;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 说明: 登录拦截器
|
||||
* 项目名称: sbdemo
|
||||
* 创建时间: 2017/5/10 16:36
|
||||
* 作者: xiangwb
|
||||
*/
|
||||
public class LoginInterceptor extends HandlerInterceptorAdapter {
|
||||
private final Logger logger = LoggerFactory.getLogger(DispatcherServletConfig.class);
|
||||
private static final Set<String> set = new HashSet<>();//拦截器白名单
|
||||
static {
|
||||
set.add("/v2/api-docs");
|
||||
set.add("/servlet/captchaCode");
|
||||
set.add("/swagger-ui.html");
|
||||
}
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
String servletPath = request.getServletPath();
|
||||
if(!set.contains(servletPath) && !servletPath.contains("login")){
|
||||
HttpSession session = request.getSession();
|
||||
if (session.getAttribute(CommonConstant.SESSION_CURRENT_USER)!=null) {
|
||||
// TODO: 2017/5/15
|
||||
return true;
|
||||
}else {
|
||||
// TODO: 2017/10/3
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package com.xwbing.handler;
|
||||
|
||||
import com.xwbing.annotation.LogInfo;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterReturning;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* 说明: web层日志记录切面
|
||||
* 项目名称: sbdemo
|
||||
* 创建时间: 2017/5/10 16:34
|
||||
* 作者: xiangwb
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
public class WebLogAspect {
|
||||
private final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
|
||||
ThreadLocal<Long> startTime = new ThreadLocal<>();
|
||||
|
||||
@Pointcut("within(com.xwbing.controller..*) && @annotation(logInfo)")
|
||||
public void pointCutWithMsg(LogInfo logInfo) {
|
||||
}
|
||||
|
||||
@Pointcut("within(com.xwbing.controller..*)")
|
||||
public void pointCut() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 前置通知
|
||||
*
|
||||
* @param logInfo
|
||||
*/
|
||||
@Before("pointCutWithMsg(logInfo)")
|
||||
public void before(JoinPoint joinPoint, LogInfo logInfo) {
|
||||
startTime.set(System.currentTimeMillis());
|
||||
String info = logInfo.value();
|
||||
String targetName = joinPoint.getTarget().getClass().getName();
|
||||
String methodName = joinPoint.getSignature().getName();
|
||||
logger.info("{} {} : {} started", targetName, methodName, info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 后置通知
|
||||
*
|
||||
* @param logInfo
|
||||
*/
|
||||
@AfterReturning(pointcut = "pointCutWithMsg(logInfo)")
|
||||
public void afterReturning(JoinPoint joinPoint, LogInfo logInfo) {
|
||||
long end = System.currentTimeMillis();
|
||||
long ms = end - startTime.get();
|
||||
String info = logInfo.value();
|
||||
String targetName = joinPoint.getTarget().getClass().getName();
|
||||
String methodName = joinPoint.getSignature().getName();
|
||||
logger.info("{} {} : {} completed in {} ms", targetName, methodName, info, ms);
|
||||
}
|
||||
|
||||
/**
|
||||
* 异常通知
|
||||
*
|
||||
* @param e
|
||||
*/
|
||||
// @AfterThrowing(pointcut = "pointCut()", throwing = "e")
|
||||
public void afterThroing(Exception e) {
|
||||
logger.error("异常信息:{}", e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 环绕通知=前置+后置
|
||||
*
|
||||
* @param pjp
|
||||
* @param logInfo
|
||||
* @return
|
||||
*/
|
||||
// @Around("pointCutWithMsg(logInfo)")
|
||||
public Object around(ProceedingJoinPoint pjp, LogInfo logInfo) {
|
||||
String info = logInfo.value();// 获取注解信息
|
||||
Object result = null;// 被代理对象方法返回的结果
|
||||
try {
|
||||
logger.info("{} start", info);
|
||||
result = pjp.proceed();
|
||||
logger.info("{} end", info);
|
||||
} catch (Throwable e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取日志注解信息
|
||||
*
|
||||
* @param joinPoint
|
||||
* @return
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
@Deprecated
|
||||
public String getMsg(JoinPoint joinPoint) throws ClassNotFoundException {
|
||||
String targetName = joinPoint.getTarget().getClass().getName();// 类名
|
||||
String methodName = joinPoint.getSignature().getName();// 方法名
|
||||
Object[] arguments = joinPoint.getArgs();// 方法参数
|
||||
Class targetClass = Class.forName(targetName);
|
||||
Method[] methods = targetClass.getMethods();
|
||||
String description = "";
|
||||
for (Method method : methods) {
|
||||
if (method.getName().equals(methodName)) {
|
||||
Class[] clazzs = method.getParameterTypes();
|
||||
if (clazzs.length == arguments.length) {
|
||||
description = method.getAnnotation(LogInfo.class).value();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return description;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user