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     }

 

8. PCM 转 WAV 格式

  PcmToWav.java

 1 package com.wunaozai.demo.pcm2wav;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 
 7 /**
 8  * PCM 转 WAV
 9  * @author wunaozai
10  * @Date 2020-11-03
11  */
12 public class PcmToWav {
13     public static void main(String[] args) throws Exception {
14         convertAudioFiles("F:\\voice\\1311458187862568961.pcm", "F:\\voice\\1311458187862568961.pcm.mp3");
15     }
16     
17     public static void convertAudioFiles(String src, String dest) throws IOException {
18         FileInputStream fis = new FileInputStream(src);
19         FileOutputStream fos = new FileOutputStream(dest);
20         //计算长度
21         byte[] buf = new byte[1024 * 4];
22         int size = fis.read(buf);
23         int PCMSize = 0;
24         while (size != -1) {
25             PCMSize += size;
26             size = fis.read(buf);
27         }
28         fis.close();
29  
30         //填入参数,比特率等等。这里用的是16位单声道 8000 hz
31         WaveHeader header = new WaveHeader();
32         //长度字段 = 内容的大小(PCMSize) + 头部字段的大小(不包括前面4字节的标识符RIFF以及fileLength本身的4字节)
33         header.fileLength = PCMSize + (44 - 8);
34         header.FmtHdrLeth = 16;
35         header.BitsPerSample = 16;
36         header.Channels = 1;
37         header.FormatTag = 0x0001;
38         header.SamplesPerSec = 16000;
39         header.BlockAlign = (short) (header.Channels * header.BitsPerSample / 8);
40         header.AvgBytesPerSec = header.BlockAlign * header.SamplesPerSec;
41         header.DataHdrLeth = PCMSize;
42  
43         byte[] h = header.getHeader();
44  
45         assert h.length == 44; //WAV标准,头部应该是44字节
46         // write header
47         fos.write(h, 0, h.length);
48         // write data stream
49         fis = new FileInputStream(src);
50         size = fis.read(buf);
51         while (size != -1) {
52             fos.write(buf, 0, size);
53             size = fis.read(buf);
54         }
55         fis.close();
56         fos.close();
57         System.out.println("Convert OK!");
58     }
59 }
View Code

相关文章:

  • 2021-12-05
  • 2021-12-27
  • 2021-08-23
  • 2021-07-07
  • 2021-12-30
  • 2021-06-05
  • 2021-12-18
  • 2021-10-15
猜你喜欢
  • 2021-04-13
  • 2021-12-10
  • 2022-12-23
  • 2021-05-30
  • 2021-08-30
  • 2021-11-30
  • 2021-12-02
相关资源
相似解决方案