1、类型转换:
1.数组转化为List: String[] strArray= new String[]{"Tom", "Bob", "Jane"}; List strList= Arrays.asList(strArray); 2.数组转Set String[] strArray= new String[]{"Tom", "Bob", "Jane"}; SetstaffsSet = new HashSet<>(Arrays.asList(staffs)); staffsSet.add("Mary"); // ok staffsSet.remove("Tom"); // ok 3.List转Set String[] staffs = new String[]{"Tom", "Bob", "Jane"}; List staffsList = Arrays.asList(staffs); Set result = new HashSet(staffsList); 4.set转List String[] staffs = new String[]{"Tom", "Bob", "Jane"}; Set staffsSet = new HashSet<>(Arrays.asList(staffs)); List result = new ArrayList<>(staffsSet);
2、ObjectMapper类型转换LIst:
JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class,Task.class);
mapper.readValue(result, javaType);
JavaType javaType = mapper.getTypeFactory().constructParametricType(JsonResult.class,ArrayList.class);
JsonResult> tasks = mapper.readValue(result, javaType);
List
List>() {});
3、解决依赖冲突:
方式1: 排除传递依赖 compile 'org.slf4j:slf4j-api:1.7.22' compile ('org.hibernate:hibernate-core:3.6.3.Final'){ //排除某一个库(slf4j)依赖 exclude group: 'org.slf4j',module: 'slf4j-api' }
指定禁止传递依赖
compile('org.hibernate:hibernate-core:3.6.3.Final') { //指定禁止传递依赖 transitive false } compile 'org.slf4j:slf4j-api:1.7.22'
当遇到依赖冲突时,指定一个版本号 configurations.all() { Configuration configuration -> configuration.resolutionStrategy.force(['org.slf4j:slf4j-api:1.6.1']) //或者这样写 resolutionStrategy.setForcedModules(['org.slf4j:slf4j-api:1.6.1']) }
# provided ,runtime 和 compile 三者区别? compile : 依赖的包,编译并打包到最终的 apk 文件中。 provided : 依赖的包只参与编译而不会打包到最终的 apk 文件中。 runtime : 适用于依赖的包只作用在运行时而不需要在编译时。 dependencies { //optional, help to generate the final application provided('com.tencent.tinker:tinker-android-anno:1.9.1') //tinker's main Android lib compile('com.tencent.tinker:tinker-android-lib:1.9.1') }
4、构建一个gradle工程的配置:
buildscript { ext.spring_boot_version = '1.5.18.RELEASE' repositories { //maven {url "http://maven.aliyun.com/nexus/content/groups/public/" } mavenCentral() //jcenter() // 中央库 } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${spring_boot_version}") classpath "io.spring.gradle:dependency-management-plugin:1.0.5.RELEASE" } } apply plugin: 'java' apply plugin: 'maven' apply plugin: 'maven-publish' apply plugin: 'spring-boot' apply plugin: 'application' repositories { maven {url "http://maven.aliyun.com/nexus/content/groups/public/" } mavenCentral() jcenter() // 中央库 } springBoot { executable = true mainClass = 'com.liang.ready.AppServerBooter' } dependencies { compile "org.springframework.boot:spring-boot-starter-web" // Swagger2 compile "io.github.swagger2markup:swagger2markup:1.3.3" compile "io.springfox:springfox-swagger2:2.9.2" compile "io.springfox:springfox-swagger-ui:2.9.2" compile 'dom4j:dom4j:1.6.1' compile 'com.h2database:h2:1.4.187' compile 'org.apache.poi:poi-ooxml:3.17' compile 'org.jboss.resteasy:resteasy-jaxrs:3.0.14.Final' compile 'org.jboss.resteasy:resteasy-client:3.0.14.Final' //apache compile 'commons-net:commons-net:3.3' //file upload compile 'org.apache.commons:commons-vfs2:2.0' compile 'cn.rongcloud.im:server-sdk-java:3.0.6' compile 'ws.schild:jave-all-deps:2.6.0' }
server: port: 8080 contextPath: / spring: datasource: url: jdbc:mysql://127.0.0.1:3306/test username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver
"1.0" encoding="UTF-8"?>"true"> "STDOUT" class="ch.qos.logback.core.ConsoleAppender"> System.out %d{HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n class="com.cdv.common.misc.logger.LevelRangeFilter"> INFO "STDERR" class="ch.qos.logback.core.ConsoleAppender"> System.err class="ch.qos.logback.classic.PatternLayout"> %d{HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n "RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> class="ch.qos.logback.classic.filter.ThresholdFilter"> TRACE logs/app.log true class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> logs/app-%d{yyyy-MM-dd}.log 100 class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">logs/app.%i.log.zip 1 7 50MB UTF-8 "com.liang" level="INFO" /> "org.springframework.web.client" level="INFO" /> "org.springframework.web.servlet.DispatcherServlet" level="INFO"/> "org.springframework.web.filter.CommonsRequestLoggingFilter" level="INFO"/> "INFO"> ref ref="STDOUT" /> ref ref="STDERR" /> ref ref="RollingFile" />
package com.liang.ready; import java.io.IOException; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling @EnableAsync public class AppServerBooter { public static void main(String[] args) throws IOException { SpringApplication.run(AppServerBooter.class, args); } }
package com.liang.ready.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration @ComponentScan(basePackages = { "com.liang.ready.schedule", "com.liang.ready.service", "com.liang.ready.rs" }) public class AppAutoConfiguration { @Bean public Docket docketCommon() { return new Docket(DocumentationType.SWAGGER_2) .groupName("agents").select() .apis(RequestHandlerSelectors.basePackage("com.liang.ready.rs")) .paths(PathSelectors.any()) .build() .apiInfo(new ApiInfoBuilder(). title("Agents Restful API"). description("Agents接口的API"). contact(new Contact("", "", "")). version("1.0"). build()); } }
5、可变参数:
JDK 1.5 开始,Java支持传递同类型的可变参数给一个方法。一个方法中只能指定一个可变参数,它必须是方法的最后一个参数。任何普通的参数必须在它之前声明。
调用使用了可变参数的方法时:
1)可以不写参数,即传入空参;
2)可以直接在里边写入参数,参数间用逗号隔开;
3)可以传入一个数组;