首页
归档
留言
友链
广告合作
壁纸
更多
美女主播
Search
1
博瑞GE车机升级/降级
5,577 阅读
2
Mac打印机设置黑白打印
4,888 阅读
3
修改elementUI中el-table树形结构图标
4,865 阅读
4
Mac客户端添加腾讯企业邮箱方法
4,646 阅读
5
intelliJ Idea 2022.2.X破解
4,318 阅读
后端开发
HarmonyOS Next
Web前端
微信开发
开发辅助
App开发
数据库
随笔日记
登录
/
注册
Search
标签搜索
Spring Boot
Java
Vue
Spring Cloud
Mac
MyBatis
WordPress
asp.net
Element UI
Nacos
MacOS
.Net
Spring Cloud Alibaba
Mybatis-Plus
Typecho
jQuery
MySQL
Java Script
微信小程序
Oracle
Laughing
累计撰写
606
篇文章
累计收到
1,417
条评论
首页
栏目
后端开发
HarmonyOS Next
Web前端
微信开发
开发辅助
App开发
数据库
随笔日记
页面
归档
留言
友链
广告合作
壁纸
美女主播
搜索到
100
篇与
的结果
2019-07-26
Spring Boot过滤器Filter
单个过滤器如果只是定义一个过滤器,直接通过@Configuration注解即可。package Cc.LiSen.Configurations; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Configuration; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; /** * ClassName: CustomFilter <br/> * Description: <br/> * date: 2019/7/25 22:04<br/> * * @since JDK 1.8 */ @WebFilter(filterName = "CustomFilter", urlPatterns = "{/*}") public class CustomFilter implements Filter { private Logger logger = LoggerFactory.getLogger(this.getClass()); @Override public void init(FilterConfig filterConfig) throws ServletException { logger.info("初始化过滤器CustomFilter"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { logger.info("过滤器CustomFilter开始工作,并转入下一个过滤"); chain.doFilter(request, response); logger.info("CustomFilter继续过滤"); } @Override public void destroy() { logger.info("过滤器CustomFilter销毁"); } }多个过滤器如果定义多个过滤器,需要通过FilterRegistrationBean提供setOrder方法,可以为filter设置排序值,让spring在注册web filter之前排序后再依次注册。启动类中利用@bean注册FilterRegistrationBean*温馨提示过滤器定义与上面类似,去掉@Configuration注解即可,这里不再赘述,然后修改启动类,增加以下代码package Cc.LiSen; import Cc.LiSen.Configurations.CustomFilter; import Cc.LiSen.Configurations.CustomFilterOther; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication //SpringBoot 默认从App类往下面的包扫描 //所以如果控制器、实体等类与App不在一个包,同时不在下面的包时,必须手动指定包 @EnableJpaRepositories(basePackages = {"Cc.LiSen.Repositories", "Cc.LiSen.Services"}) public class App { private static final Logger logger = LoggerFactory.getLogger(App.class); public static void main(String[] args) { // System.setProperty("log.root","DEBUG,info,error"); // // System.setProperty("log.base","D:\\log4j\\base"); SpringApplication.run(App.class, args); } @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(new CustomFilter()); filterRegistrationBean.setOrder(10); return filterRegistrationBean; } @Bean public FilterRegistrationBean filterRegistrationBeanOther() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(new CustomFilterOther()); filterRegistrationBean.setOrder(20); return filterRegistrationBean; } }
2019年07月26日
1,259 阅读
0 评论
0 点赞
2019-07-22
Spring Boot通过Redis共享Session
温馨提示以下内容基于SpringBoot 1.2.7版本<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.7.RELEASE</version> </parent>添加依赖主要是添加如下两个依赖<!-- 添加redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.3.8.RELEASE</version> </dependency> <!-- session依赖--> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.3.5.RELEASE</version> </dependency> 安装Redis具体操作方法请自行百度,不是本文重点配置Redis找到application.yml文件,添加如下内容#redis配置 redis: host: 我是IP port: 6379 password: 我是密码 timeout: 0 pool: max-active: 100 max-idle: 10 max-wait: 100000 database: 0 增加Redis配置类RedisConfiguration.javapackage Cc.LiSen.Configurations; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig; /** * ClassName: RedisConfiguration <br/> * Description: <br/> * date: 2019/7/22 9:17<br/> * * @since JDK 1.8 */ @Configuration @EnableAutoConfiguration public class RedisConfiguration { /* @Bean @ConfigurationProperties(prefix = "spring.redis.pool") public JedisPoolConfig getJedisPoolConfig() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); return jedisPoolConfig; } @Bean @ConfigurationProperties(prefix = "spring.redis") public JedisConnectionFactory getConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setUsePool(true); JedisPoolConfig jedisPoolConfig = getJedisPoolConfig(); factory.setPoolConfig(jedisPoolConfig); return factory; } @Bean public RedisTemplate<?,?> redisTemplate(){ JedisConnectionFactory factory=getConnectionFactory(); RedisTemplate<?,?> redisTemplate=new StringRedisTemplate(factory); return redisTemplate; } */ /** * redisTemplate 序列化使用的jdkSerializeable, 存储二进制字节码, 所以自定义序列化类 * @param redisConnectionFactory * @return */ @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>(); redisTemplate.setConnectionFactory(redisConnectionFactory); // 使用Jackson2JsonRedisSerialize 替换默认序列化 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); // 设置value的序列化规则和 key的序列化规则 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } }增加Session配置SessionConfiguration.javapackage Cc.LiSen.Configurations; import org.springframework.context.annotation.Configuration; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; /** * ClassName: SessionConfiguration <br/> * Description: <br/> * date: 2019/7/22 10:45<br/> * * @since JDK 1.8 */ @Configuration @EnableRedisHttpSession public class SessionConfiguration { }简单测试UUID uid = (UUID) session.getAttribute("uid"); if (uid == null) { uid = UUID.randomUUID(); } session.setAttribute("uid", uid); request.getSession().setAttribute("user",JSON.toJSONString(checkUser));
2019年07月22日
1,342 阅读
0 评论
0 点赞
2019-07-21
Spring Boot使用FastJson
什么是FastJson?FastJson是阿里巴巴旗下的一个开源项目之一,顾名思义它专门用来做快速操作json的序列化与反序列化的组件。它是目前json解析最快的开源组件没有之一!在这之前JackJson是最为出名的快速操作json的工具,当然,现在也很出名。阿里巴巴的FastJson虽然速度快,但是bug也是相当多,如果是在大型系统中并且对系统安全性要求比较高,仍然建议使用Jackjson。maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.75</version> </dependency>{message type="warning" content="fastjson虽然是阿里巴巴出品的,但是最近爆出很多bug,所以大项目上还是慎重使用"/}创建配置信息类我们接下来创建一个FastJsonConfiguration配置信息类,添加@Configuration注解让SpringBoot自动加载类内的配置,有一点要注意我们继承了WebMvcConfigurerAdapter这个类,这个类是SpringBoot内部提供专门处理用户自行添加的配置,里面不仅仅包含了修改视图的过滤还有其他很多的方法,比如拦截器,过滤器,Cors配置等。方式一:fastJson视图过滤配置package cn.notemi.configuration; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import java.util.List; /** * Title:FastJsonConfiguration * Description:FastJson配置信息 * * @create 2017-08-08 下午 4:33 **/ @Configuration public class FastJsonConfiguration extends WebMvcConfigurerAdapter { /** * 修改自定义消息转换器 * @param converters 消息转换器列表 */ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { //调用父类的配置 super.configureMessageConverters(converters); //创建fastJson消息转换器 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //创建配置类 FastJsonConfig fastJsonConfig = new FastJsonConfig(); //修改配置返回内容的过滤 fastJsonConfig.setSerializerFeatures( SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty ); fastConverter.setFastJsonConfig(fastJsonConfig); //将fastjson添加到视图消息转换器列表内 converters.add(fastConverter); } }方式二:自定义FastJsonHttpMessageConverter@Configuration public class FastJsonConfig { @Bean public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() { FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); com.alibaba.fastjson.support.config.FastJsonConfig fastJsonConfig = new com.alibaba.fastjson.support.config.FastJsonConfig(); fastJsonConfig.setDateFormat("yyyy-MM-dd"); fastJsonConfig.setCharset(StandardCharsets.UTF_8); fastJsonConfig.setSerializerFeatures( // SerializerFeature.WriteClassName, SerializerFeature.WriteMapNullValue, SerializerFeature.PrettyFormat, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty ); fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); // 解决中文乱码 List<MediaType> fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON); fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes); return fastJsonHttpMessageConverter; } }FastJson配置实体调用setSerializerFeatures方法可以配置多个过滤方式,下面我们来介绍下常用的SerializerFeatures配置。FastJson SerializerFeaturesWriteNullListAsEmpty :List字段如果为null,输出为[],而非nullWriteNullStringAsEmpty : 字符类型字段如果为null,输出为"",而非nullDisableCircularReferenceDetect :消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环)WriteNullBooleanAsFalse:Boolean字段如果为null,输出为false,而非nullWriteMapNullValue:是否输出值为null的字段,默认为false。项目运行我们使用了过滤,SerializerFeature.WriteNullStringAsEmpty,本该显示null,显示为"",所以成功使用。
2019年07月21日
1,349 阅读
0 评论
0 点赞
2019-07-21
Spring Boot包位置设置
/* * Copyright (C) 2019 李森的博客 https://lisen.cc * 项目名称:Cc.LiSen.Idea * 文件名称:App.java * Date:19-7-19 上午1:21 * Author:lisen@lisen.cc * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */ package Cc.LiSen; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.orm.jpa.EntityScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication //SpringBoot 默认从App类往下面的包扫描 //所以如果控制器、实体等类与App不在一个包,同时不在下面的包时,必须手动指定包 //@ComponentScan(basePackages = {"Cc.LiSen.Controllers", "Cc.LiSen.Services"}) //@EnableJpaRepositories(basePackages = {"Cc.LiSen.Repositories", "Cc.LiSen.Services"}) //@EntityScan(basePackages = {"Cc/LiSen/Pojos"}) public class App { public static void main(String[] args){ SpringApplication.run(App.class,args); } }
2019年07月21日
1,194 阅读
0 评论
0 点赞
1
...
8
9