• 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;
        }
    
  • 相关阅读:
    android ListView布局之一(继承listActivity、使用arrayAdapter)
    android your project contains error
    wojilu系统的ORM代码解析[源代码结构分析,ObjectBase基类分析]
    ORM中启用数据库事务
    我记录网站综合系统 技术原理解析[11:ActionProcessor流程wojilu核心]
    互联网,让我们更安全了,还是更危险了【纯讨论】
    不用服务器也能跑的框架wojilu续篇
    使用wojilu 无代码实现 输入框提示 及其背后的原理
    wojilu日志系统可以单独使用
    “我有什么” 和 “你要什么” 框架制作的一些思考
  • 原文地址:https://www.cnblogs.com/fly-book/p/11572843.html
Copyright © 2020-2023  润新知