作準備的筆記

一、類型轉換:java

1.數組轉化爲List:
String[] strArray= new String[]{"Tom", "Bob", "Jane"};

List strList= Arrays.asList(strArray);

2.數組轉Set
String[] strArray= new String[]{"Tom", "Bob", "Jane"};

Set<String> staffsSet = 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<String> staffsSet = new HashSet<>(Arrays.asList(staffs));

List<String> result = new ArrayList<>(staffsSet);
類型轉換

 二、ObjectMapper類型轉換LIst:mysql

JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class,Task.class);android

mapper.readValue(result, javaType);git

JavaType javaType = mapper.getTypeFactory().constructParametricType(JsonResult.class,ArrayList.class);
JsonResult<List<Task>> tasks = mapper.readValue(result, javaType);github

List<Task> list = tasks.getData();

List<Task> tasklist = mapper.convertValue(list, new TypeReference<List<Task>>() {});web

三、解決依賴衝突:spring

方式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'
}
 

  指定禁止傳遞依賴sql

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')
}
provided ,runtime 和 compile 三者區別

 四、構建一個gradle工程的配置:apache

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'

}
bulid.gradle
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
application.yml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration scan="true">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <target>System.out</target>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n</pattern>
        </encoder>
        <filter class="com.cdv.common.misc.logger.LevelRangeFilter">
            <levelMax>INFO</levelMax>
        </filter>
    </appender>

    <appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">
        <target>System.err</target>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n</pattern>
        </layout>
    </appender>

    <appender name="RollingFile"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>TRACE</level>
        </filter>

        <file>logs/app.log</file>
        <append>true</append>

        <!-- 可以讓天天產生一個日誌文件,最多 100 個,自動回滾 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>logs/app-%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>100</maxHistory>
        </rollingPolicy>

        <!-- 下面這段註釋的代碼能夠在日誌文件超過 50MB 時進行歸檔,而且歸檔文件後綴只要是 .zip 或 .gz 就會自動壓縮日誌歸檔 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>logs/app.%i.log.zip</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>7</maxIndex>
        </rollingPolicy>
        <triggeringPolicy
            class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>50MB</maxFileSize>
        </triggeringPolicy>

        <encoder>
            <charset>UTF-8</charset>
            <pattern><![CDATA[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger - %msg%n]]></pattern>
        </encoder>
    </appender>

    <!-- 自有類的日誌 -->
    <logger name="com.liang" level="INFO" />

    <!-- RESTful 客戶端請求響應日誌 -->
    <logger name="org.springframework.web.client" level="INFO" />

    <!-- RESTful 服務端被請求的地址日誌 -->
    <logger name="org.springframework.web.servlet.DispatcherServlet" level="INFO"/>

    <!-- RESTful 服務端被請求的地址/參數/請求內容/相應內容日誌 -->
    <logger name="org.springframework.web.filter.CommonsRequestLoggingFilter" level="INFO"/>

    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="STDERR" />

        <appender-ref ref="RollingFile" />
    </root>
</configuration>
logback.xml
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);
    }
}
Application.java
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());
    }
    
}
AppAutoConfiguration.java

 五、可變參數:api

JDK 1.5 開始,Java支持傳遞同類型的可變參數給一個方法。一個方法中只能指定一個可變參數,它必須是方法的最後一個參數。任何普通的參數必須在它以前聲明。

調用使用了可變參數的方法時:

1)能夠不寫參數,即傳入空參;

2)能夠直接在裏邊寫入參數,參數間用逗號隔開;

3)能夠傳入一個數組;

相關文章
相關標籤/搜索