springboot常见的config配置

package com.qf.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.text.SimpleDateFormat;

@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {

重写方法
//    配置视图解析器
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp().prefix("/jsp/")
                .suffix(".jsp");
    }
//  日期格式
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper om = new ObjectMapper();
        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        converter.setObjectMapper(om);
        converters.add(0,converter);

    }



注册Bean
//    全局springbootd的配置日期格式
//    @Bean
//    ObjectMapper objectMapper(){
//        ObjectMapper om = new ObjectMapper();
//        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
//        return om;
//    }

//    spring配置日期格式
    @Bean
    MappingJackson2HttpMessageConverter httpMessageConverter(){
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper om = new ObjectMapper();
        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        converter.setObjectMapper(om);
        return converter;
    }
}
//    gson
    @Bean
    GsonBuilder builder(){
        return new GsonBuilder().setDateFormat("yyyy-MM-dd");
    }
//    fastjson配置
    @Bean
    FastJsonHttpMessageConverter httpMessageConverter(){
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        config.setCharset(Charset.forName("UTF-8"));
        config.setDateFormat("yyyy-MM-dd");
        converter.setFastJsonConfig(config);
//        这里还要设置一次
        converter.setDefaultCharset(Charset.forName("UTF-8"));
        return converter;
    }
}

配置gson

 配置类

 fastjson

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>2.0.2</version>
</dependency>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Spring Boot配置Redis,你需要进行以下步骤: 1. 在pom.xml文件中添加Redis的依赖项。可以使用以下代码片段添加依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 这将引入Spring Boot的Redis支持库。 2. 在Spring Boot应用程序的配置文件(application.properties或application.yml)中配置Redis连接信息。你需要提供Redis服务器的主机名、端口号、密码等信息。例如: ``` spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=yourpassword ``` 根据你的实际情况修改这些值。 3. 创建一个Redis配置,用于配置Redis连接工厂和其他相关的Bean。你可以使用以下示例代码作为参考: ``` @Configuration @EnableCaching public class RedisConfig { @Value("${spring.redis.host}") private String redisHost; @Value("${spring.redis.port}") private int redisPort; @Value("${spring.redis.password}") private String redisPassword; @Bean public JedisConnectionFactory jedisConnectionFactory() { RedisStandaloneConfiguration redisConfiguration = new RedisStandaloneConfiguration(redisHost, redisPort); redisConfiguration.setPassword(RedisPassword.of(redisPassword)); return new JedisConnectionFactory(redisConfiguration); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(jedisConnectionFactory()); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } } ``` 这个配置使用RedisStandaloneConfiguration配置了Redis连接,并创建了一个JedisConnectionFactory实例来管理连接。还创建了一个RedisTemplate实例,用于与Redis进行交互。你可以根据你的需求进行自定义修改。 以上就是在Spring Boot配置Redis的步骤。你可以根据你的实际情况进行相应的调整和配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [springBoot中redis的使用](https://blog.csdn.net/weixin_60821814/article/details/127297356)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot集成Redis以及RedisConfig,RedisUtils](https://blog.csdn.net/m0_52174905/article/details/120638392)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LI JS@你猜啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值