Commit 3dde0a92 authored by zhangyong's avatar zhangyong

Merge remote-tracking branch 'origin/master'

parents 9ca95c97 54fc136a
package cn.ossip.admin.config;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import com.egolm.common.bean.Rjx;
@Component
public class ExceptionHandler implements HandlerExceptionResolver {
private static final Log logger = LogFactory.getLog(ExceptionHandler.class);
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
ModelAndView mav = new ModelAndView(new MappingJackson2JsonView());
try {
response.setContentType("application/json");
if (ex instanceof XException) {
mav.addAllObjects(((XException) ex).getRjx());
} else {
mav.addAllObjects(Rjx.jsonErr().setMessage("未处理异常[" + ex.getClass().getName() + ":" + ex.getMessage() + "]"));
}
} catch (Exception e) {
mav.addAllObjects(Rjx.jsonErr().setMessage("异常处理失败"));
} finally {
logger.error("", ex);
}
return mav;
}
}
package cn.ossip.admin.config;
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;
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.egolm.admin"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("在线文档")
.description("在线文档")
.termsOfServiceUrl("NO terms of service")
.version("1.0")
.build();
}
}
package cn.ossip.admin.config;
import java.util.EventListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false).setUseTrailingSlashMatch(false);
}
@Bean
public ServletListenerRegistrationBean<EventListener> getDemoListener(){
ServletListenerRegistrationBean<EventListener> registrationBean = new ServletListenerRegistrationBean<>();
registrationBean.setListener(new RequestContextListener());
return registrationBean;
}
public static HttpServletRequest getRequest() {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if(servletRequestAttributes != null) {
return servletRequestAttributes.getRequest();
}
return null;
}
public static HttpServletResponse getResponse() {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if(servletRequestAttributes != null) {
return servletRequestAttributes.getResponse();
}
return null;
}
public static HttpSession getSession() {
HttpServletRequest request = WebMvcConfig.getRequest();
if(request != null) {
return request.getSession();
}
return null;
}
}
package cn.ossip.admin.config;
import com.egolm.common.bean.Rjx;
public class XException extends RuntimeException {
private static final long serialVersionUID = 1L;
private Rjx rjx = Rjx.jsonErr().setStatus(500).setMessage("ERROR");
public XException(String msg) {
super(msg);
rjx.setMessage(msg);
}
public XException(String msg, Throwable e) {
super(msg, e);
rjx.setMessage(msg);
}
public XException(String msg, int code) {
super(msg);
rjx.setMessage(msg);
rjx.setStatus(code);
}
public XException(String msg, int code, Throwable e) {
super(msg, e);
rjx.setMessage(msg);
rjx.setStatus(code);
}
public Rjx getRjx() {
return rjx;
}
public static void assertNotBlank(Object obj, String message) {
if(obj == null || obj.toString().trim().equals("")) {
throw new XException((message == null || message.trim().equals("")) ? "对象不能为空" : message);
}
}
}
package cn.ossip.admin.config.datasource;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.common.jdbc.dialect.MySqlDialect;
@Configuration
public class ConfigDataSourceConfig {
@Bean
@Primary
@Qualifier("configDataSource")
@ConfigurationProperties(prefix = "spring.datasource.config")
public DataSource getDataSource() {
return DataSourceBuilder.create().type(DruidDataSource.class).build();
}
@Bean
@Primary
@Qualifier("configTxManager")
public PlatformTransactionManager getTransactionManager(@Qualifier("configDataSource")DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
@Primary
@Qualifier("configJdbcTemplate")
public JdbcTemplate getJdbcTemplate(@Qualifier("configDataSource")DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
jdbcTemplate.setDialect(new MySqlDialect());
jdbcTemplate.setSql_level(3);
return jdbcTemplate;
}
}
\ No newline at end of file
package cn.ossip.admin.config.datasource;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.common.jdbc.dialect.MySqlDialect;
@Configuration
public class ShopDataSourceConfig {
@Bean
@Qualifier("shopDataSource")
@ConfigurationProperties(prefix = "spring.datasource.shop")
public DataSource getDataSource() {
return DataSourceBuilder.create().type(DruidDataSource.class).build();
}
@Bean
@Qualifier("shopTxManager")
public PlatformTransactionManager getTransactionManager(@Qualifier("shopDataSource")DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
@Qualifier("shopJdbcTemplate")
public JdbcTemplate getJdbcTemplate(@Qualifier("shopDataSource")DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
jdbcTemplate.setDialect(new MySqlDialect());
jdbcTemplate.setSql_level(3);
return jdbcTemplate;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment