zoukankan      html  css  js  c++  java
  • 05.配置为开发模式、配置静态资源locations、自定义消息转化器、FastJson

    配置为开发模式,代码做了修改,不用重新运行 idea需要该配置,mac测试无效

     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>springloaded</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    

    访问静态资源

    src/main/resources/static

    # 设定静态文件路径
    spring.resources.static-locations=classpath:/static/
    

    自定义消息转化器

        //定义消息转换器
        //springboot默认配置org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration
        @Bean
        public StringHttpMessageConverter stringHttpMessageConverter(){
            return new StringHttpMessageConverter(StandardCharsets.ISO_8859_1);
        }
        @RequestMapping("/")
        public String first(){
            return "hello傅立叶" //因为使用的是ISO_8859_1,所以中文乱码
        }
    

    idea读取properties文件中的汉字乱码解决
    在IntelliJ IDEA中依次点击File -> Settings -> Editor -> File Encodings 全部设置UTF-8,Transparent native-to-ascii 打勾
    create UTF-8 files选择with NO BOM

    FastJson

    springboot默认使用的是Jackson

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.15</version>
    </dependency>
    
    @SpringBootApplication(scanBasePackages = {"com.fly"})//默认是本包及子报
    public class SpringDemoApp extends WebMvcConfigurerAdapter {
    
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            //创建FastJson的消息转换器
            FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
            //创建FastJson的配置对象
            FastJsonConfig config = new FastJsonConfig();
            //对Json数据进行格式化
            config.setSerializerFeatures(SerializerFeature.PrettyFormat);
            converter.setFastJsonConfig(config);
            converters.add(converter);
        }
        
        //不继承WebMvcConfigurerAdapter使用配置成Bean的方式
        //@Bean
        //public HttpMessageConverters fastJsonHttpMessageConverter(){
        //    //创建FastJson的消息转换器
        //    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        //    //创建FastJson的配置对象
        //    FastJsonConfig config = new FastJsonConfig();
        //    //对Json数据进行格式化
        //    config.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //    converter.setFastJsonConfig(config);
        //    return new HttpMessageConverters(converter);
        //}
    
        
        public static void main(String[] args){
            SpringApplication.run(SpringDemoApp.class,args);
        }
    }
    
        @RequestMapping("/person")
        public Object show(){
            Person person = new Person();
            person.setId(1);
            person.setDate(new Date());
            person.setName("fly傅立叶");
            return person;
        }
    

    发现中文乱码了

    #response编码设置为utf-8功能开启
    spring.http.encoding.force=true
    

    **时间显示的是时间戳

      @JSONField(format = "yyyy-MM-dd HH:mm:ss") //也可以加在字段上
        public Date getDate() {
            return date;
        }
    
  • 相关阅读:
    前端日期格式化
    前端显示省略号
    模糊搜索和时间区间搜索
    数字保持小数点后一位
    每天一个linux命令(tcpreplay)
    每天一个linux命令(find)
    每天一个linux命令(netstat)
    每天一个linux命令(lsof)
    每天一个linux命令(tcpdump)
    每天一个linux命令(route)
  • 原文地址:https://www.cnblogs.com/fly-book/p/11572843.html
Copyright © 2011-2022 走看看