• Java笔记(2020)


    1. 复杂JSON字符串转模型

    1 import com.fasterxml.jackson.core.type.TypeReference;
    2 import com.fasterxml.jackson.databind.ObjectMapper;
    1 ObjectMapper mapper = new ObjectMapper();
    2 List<CustomModel> list =  mapper.readValue(json, new TypeReference<List<CustomModel>>() { });

    2. 在非Bean对象调用Spring Bean管理的对象

     1 package com.wunaozai.demo
     2 
     3 import org.springframework.beans.BeansException;
     4 import org.springframework.context.ApplicationContext;
     5 import org.springframework.context.ApplicationContextAware;
     6 import org.springframework.stereotype.Component;
     7 
     8 /**
     9  * 为不是spring所管理的对象,需要引用spring管理对象的时候所用的工具类
    10  * @author wunaozai
    11  *
    12  */
    13 @Component
    14 public class SpringApplicationContext implements ApplicationContextAware {
    15 
    16     protected static ApplicationContext context;
    17     
    18     @Override
    19     public void setApplicationContext(ApplicationContext ctx) throws BeansException {
    20         context = (ApplicationContext) ctx;
    21     }
    22 
    23     public static ApplicationContext getContext(){
    24         return context;
    25     }
    26     
    27 }

    调用

    1 CustomerService influxdbconnectService = SpringApplicationContext.getContext().getBean(CustomerService.class);

    3. Spring Boot 项目出现 pom.xml 第一行莫名的错误,在pom.xml 的 properties段下增加

    1 <maven-jar-plugin.version>3.0.0</maven-jar-plugin.version>

    4. eclipse maven 更换 阿里源, 需要配置 settings.xml 文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <settings>
     3     <mirrors>
     4         <mirror>
     5             <id>alimaven</id>
     6             <name>aliyun maven</name>
     7             <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
     8             <mirrorOf>central</mirrorOf>
     9         </mirror>
    10     </mirrors>
    11   <profiles>
    12     <profile>
    13        <id>nexus</id>
    14         <repositories>
    15             <repository>
    16                 <id>nexus</id>
    17                 <name>local private nexus</name>
    18                 <url>http://maven.oschina.net/content/groups/public/</url>
    19                 <releases>
    20                     <enabled>true</enabled>
    21                 </releases>
    22                 <snapshots>
    23                     <enabled>false</enabled>
    24                 </snapshots>
    25             </repository>
    26         </repositories>
    27          
    28         <pluginRepositories>
    29             <pluginRepository>
    30             <id>nexus</id>
    31             <name>local private nexus</name>
    32             <url>http://maven.oschina.net/content/groups/public/</url>
    33             <releases>
    34                 <enabled>true</enabled>
    35             </releases>
    36             <snapshots>
    37                 <enabled>false</enabled>
    38             </snapshots>
    39             </pluginRepository>
    40         </pluginRepositories>
    41     </profile></profiles>
    42 </settings>

     5. spring boot 项目,用maven编译打包时,使用wrapper时遇到下载apache-maven失败,可以到 .m2/wrapper/dists/apache-maven-** 目录下,将离线包放到该目录下。或者修改wrapper目录下的maven-wrapper.properties文件里面的distributionUrl 为本地离线地址,都是可以的。

    6. mybatis-plus 打印SQL详细日志,在application配置文件中增加

    1 #mybatis-plus
    2 mybatis-plus.mapper-locations=classpath:com/wunaozai/demo/mapper/*.xml
    3 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

     7. springboot中默认使用jackson做json序列化和反序列化,后台接收数据时将上述日期字符串转成LocalDateTime时,会报如下错误

    JSON parse error: Cannot deserialize value of type java.time.LocalDateTime from String “2018-09-20 08:01:00”: Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text ‘2018-09-20 08:01:00’ could not be parsed at index 10; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type java.time.LocalDateTime from String “2018-09-20 08:01:00”: Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text ‘2018-09-20 08:01:00’ could not be parsed at index 10

    解决办法:

    导入jackson-datatype

    1         <dependency>
    2             <groupId>com.fasterxml.jackson.datatype</groupId>
    3             <artifactId>jackson-datatype-jsr310</artifactId>
    4         </dependency>

    配置bean

     1     @Bean
     2     public ObjectMapper serializingObjectMapper() {
     3         JavaTimeModule module = new JavaTimeModule();
     4         LocalDateTimeDeserializer deserializer =
     5                 new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
     6         module.addDeserializer(LocalDateTime.class, deserializer);
     7         ObjectMapper object = Jackson2ObjectMapperBuilder.json()
     8                 .modules(module)
     9                 .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
    10                 .build();
    11         return object;
    12     }

    持续更新...

  • 相关阅读:
    python 闭包
    并行【parallel】和并发【concurrency】线程是并发还是并行,进程是并发还是并行
    生成器,送代器的区别
    深拷贝和浅拷贝的区别
    对缺省参数的理解
    对不定长参数的理解
    mysql三范式
    mysql基础查询语法
    mysql常见查询练习题
    Ubantu-Nginx部署
  • 原文地址:https://www.cnblogs.com/wunaozai/p/12175015.html
Copyright © 2020-2023  润新知