package com.hnyfkj.productmarket.web.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
@Configuration
public class DateConfig {
/**
* 默认日期时间格式
*/
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
/**
* 默认日期格式
*/
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
/**
* 默认时间格式
*/
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
/**
* LocalDate转换器,用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalDate> localDateConverter() {
return new Converter<String, LocalDate>() {
@Override
public LocalDate convert(String source) {
if (source.isEmpty()) {
return null;
}
return LocalDate.parse(source, DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT));
}
};
}
/**
* LocalDateTime转换器,用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalDateTime> localDateTimeConverter() {
return new Converter<String, LocalDateTime>() {
@Override
public LocalDateTime convert(String source) {
if (source.isEmpty()) {
return null;
}
return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT));
}
};
}
/**
* LocalTime转换器,用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, LocalTime> localTimeConverter() {
return new Converter<String, LocalTime>() {
@Override
public LocalTime convert(String source) {
if (source.isEmpty()) {
return null;
}
return LocalTime.parse(source, DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT));
}
};
}
/**
* Date转换器,用于转换RequestParam和PathVariable参数
*/
@Bean
public Converter<String, Date> dateConverter() {
return new Converter<String, Date>() {
@Override
public Date convert(String source) {
if (source.isEmpty()) {
return null;
}
SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
try {
return format.parse(source);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
};
}
/**
* Json序列化和反序列化转换器,用于转换Post请求体中的json以及将我们的对象序列化为返回响应的json
*/
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
// ObjectMapper忽略多余字段
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// LocalDateTime系列序列化和反序列化模块,继承自jsr310,我们在这里修改了日期格式
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class,
new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
javaTimeModule.addSerializer(LocalDate.class,
new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
javaTimeModule.addSerializer(LocalTime.class,
new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
javaTimeModule.addDeserializer(LocalDateTime.class,
new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
javaTimeModule.addDeserializer(LocalDate.class,
new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
javaTimeModule.addDeserializer(LocalTime.class,
new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
// Date序列化和反序列化
javaTimeModule.addSerializer(Date.class, new JsonSerializer<Date>() {
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException {
SimpleDateFormat formatter = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
String formattedDate = formatter.format(date);
jsonGenerator.writeString(formattedDate);
}
});
javaTimeModule.addDeserializer(Date.class, new JsonDeserializer<Date>() {
@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {
SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
String date = jsonParser.getText();
try {
return format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
});
// 注册新的模块到objectMapper
objectMapper.registerModule(javaTimeModule);
return objectMapper;
}
}
package com.hnyfkj.productmarket.web.config;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import com.google.common.net.HttpHeaders;
/**
* 跨域配置
*/
//@Configuration
public class CorsConfig extends WebMvcConfigurer {
@Bean
public WebMvcConfigurer corsConfigurer()
{
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").
allowedOrigins("*"). //允许跨域的域名,可以用*表示允许任何域名使用
allowedMethods("*"). //允许任何方法(post、get等)
allowedHeaders("*"). //允许任何请求头
allowCredentials(true). //带上cookie信息
exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L); //maxAge(3600)表明在3600秒内,不需要再发送预检验请求,可以缓存该结果
}
};
}
}
package com.hnyfkj.productmarket.web.config;
import com.hnyfkj.garden.user.core.conf.Conf;
import com.hnyfkj.garden.user.core.filter.HnyfkjSsoTokenFilter;
import com.hnyfkj.garden.user.core.filter.HnyfkjSsoWebFilter;
import com.hnyfkj.garden.user.core.user.JedisUtil;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* sso 配置
* @date 2020-02-20
* @author zzw
* @version v0.0.1
*/
@Configuration
public class HnyfkjSsoConfig implements DisposableBean {
@Value("${hnyfkj.sso.server}")
private String hnyfkjSsoServer;//http://127.0.0.1:8080/
@Value("${hnyfkj.sso.logout.path}")
private String hnyfkjSsoLogoutPath;///logout
@Value("${hnyfkj.sso.excluded.paths}")
private String hnyfkjSsoExcludedPaths;
@Value("${hnyfkj.sso.redis.address}")
private String hnyfkjSsoRedisAddress;//redis://127.0.0.1:6379
@Bean
public FilterRegistrationBean<HnyfkjSsoWebFilter> hnyfkjSsoFilterRegistration() {
// hnyfkj-sso, redis init
JedisUtil.init(hnyfkjSsoRedisAddress);
// hnyfkj-sso, filter init
FilterRegistrationBean<HnyfkjSsoWebFilter> registration = new FilterRegistrationBean<>();
registration.setName("hnyfkjSsoWebFilter");
registration.setOrder(1);
registration.addUrlPatterns("/*");
registration.setFilter(new HnyfkjSsoWebFilter());
registration.addInitParameter(Conf.SSO_SERVER, hnyfkjSsoServer);
registration.addInitParameter(Conf.SSO_LOGOUT_PATH, hnyfkjSsoLogoutPath);
registration.addInitParameter(Conf.SSO_EXCLUDED_PATHS, hnyfkjSsoExcludedPaths);
return registration;
}
@Bean
public FilterRegistrationBean<HnyfkjSsoTokenFilter> hnyfkjSsoTokenFilterRegistration() {
// hnyfkj-sso, redis init
JedisUtil.init(hnyfkjSsoRedisAddress);
// hnyfkj-sso, filter init
FilterRegistrationBean<HnyfkjSsoTokenFilter> registration = new FilterRegistrationBean<>();
registration.setName("HnyfkjSsoTokenFilter");
registration.setOrder(1);
registration.addUrlPatterns("/*");
registration.setFilter(new HnyfkjSsoTokenFilter());
registration.addInitParameter(Conf.SSO_SERVER, hnyfkjSsoServer);
registration.addInitParameter(Conf.SSO_LOGOUT_PATH, hnyfkjSsoLogoutPath);
registration.addInitParameter(Conf.SSO_EXCLUDED_PATHS, hnyfkjSsoExcludedPaths);
return registration;
}
@Override
public void destroy() throws Exception {
// hnyfkj-sso, redis close
JedisUtil.close();
}
}
package com.hnyfkj.productmarket.web.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* mybatis plus 配置
*
* @author 2020-02-13 zhangzw
* @version v0.0.1
*/
@Configuration
@MapperScan("com.hnyfkj.productmarket.web.mapper")
public class MybatisPlusConfig {
/**
* 配置分页拦截器
*
* @return
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setOverflow(true);
paginationInterceptor.setLimit(500);
return paginationInterceptor;
}
}
@Configuration
public class MybatisPlusConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
final MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
scannerConfigurer.setBasePackage("com.hnyfkj.**.dao");
return scannerConfigurer;
}
}
throw new BusinessException (“提示信息”)就可以了
public class BusinessException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String msg;
private int code = 500;
public BusinessException(final String msg) {
super(msg);
this.msg = msg;
}
public BusinessException(final String msg, final Throwable e) {
super(msg, e);
this.msg = msg;
}
public BusinessException(final String msg, final int code) {
super(msg);
this.msg = msg;
this.code = code;
}
public BusinessException(final String msg, final int code, final Throwable e) {
super(msg, e);
this.msg = msg;
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(final String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(final int code) {
this.code = code;
}
}
GetDay(day) {
var time = new Date(this.Vatime);
time.setDate(time.getDate() + day); //获取Day天后的日期
var y = time.getFullYear();
var m = time.getMonth() + 1; //获取当前月份的日期
var d = time.getDate();
return y + "-" + m + "-" + d
}
Math.floor(Math.random()*10);
replaceAll("(\\d{4})\\d{10}(\\d{4})","$1****$2")
for (int i = 0; i < sort.length - 1; i++) {
for (int j = 0; j < sort.length - i - 1; j++) {
if (sort[j] < sort[j + 1]) {
temp = sort[j];
sort[j] = sort[j + 1];
sort[j + 1] = temp;
}
}
}
net stop RabbitMQ && net start RabbitMQ
netstat -a -n
http://kesin.oschina.io/update-site/4.5/