import java.util.Random;  
public static void main(String[] args) {
    System.out.println(new Random().nextInt(90)+10); //86 (取10~99)
    System.out.println(new Random().nextInt(10));    //3  (取0~9)
    System.out.println(new Random().nextInt(9)+1);   //4  (取1~9)
    System.out.println(new Random().nextInt(2));     //1  (取0~9)
}

new Random().nextInt(n):取0~(n-1)之正整數

n需為正數

refer:  https://docs.oracle.com/javase/7/docs/api/java/util/Random.htm


文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()

spring使用JdbcTemplate進行OR mapping的時候,對應column名稱支援底線_
主要是下面這段程式

   /**
         * Initialize the mapping meta-data for the given class.
         * @param mappedClass the mapped class
         */
        protected void initialize(Class<T> mappedClass) {
                this.mappedClass = mappedClass;
                this.mappedFields = new HashMap<>();
                this.mappedProperties = new HashSet<>();
                PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);
                for (PropertyDescriptor pd : pds) {
                        if (pd.getWriteMethod() != null) {
                                this.mappedFields.put(lowerCaseName(pd.getName()), pd);
                                String underscoredName = underscoreName(pd.getName());
                                if (!lowerCaseName(pd.getName()).equals(underscoredName)) {
                                        this.mappedFields.put(underscoredName, pd);
                                }
                                this.mappedProperties.add(pd.getName());
                        }
                }
        }

例如:

public class CardInfo {
  private String name;
  private String expirationYear;
  private String expirationMonth;
}

經過處理mapping對象為以下

mappedFields={
expiration_year=org.springframework.beans.GenericTypeAwarePropertyDescriptor[name=expirationYear],
expirationyear=org.springframework.beans.GenericTypeAwarePropertyDescriptor[name=expirationYear],
expiration_month=org.springframework.beans.GenericTypeAwarePropertyDescriptor[name=expirationMonth],
expirationmonth=org.springframework.beans.GenericTypeAwarePropertyDescriptor[name=expirationMonth],
name=org.springframework.beans.GenericTypeAwarePropertyDescriptor[name=name]
}
mappedProperties=[expirationYear, name, expirationMonth]

 

 

 

文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()

gson使用 @SerializedName

  @SerializedName("data")
  private String name;

 

jackson使用

  @JsonProperty("data")
  private String name;

 

REFER

https://www.javadoc.io/doc/com.google.code.gson/gson/2.6.2/com/google/gson/annotations/SerializedName.html

 

文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()

  1. 繼承變數同步印出
    增加callSuper設定@lombok.ToString(callSuper=true)
@lombok.Data
@lombok.ToString(callSuper=true)
public class addre extends member{
}
  1. 指定變數不印出
    增加exclude設定@lombok.ToString(exclude= {"status"})
@lombok.Data
@lombok.ToString(exclude= {"status"})
public class addre extends member{
  String status;
  String city;
}

文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()

AMAZON S3

使用官方SDK進行資料交換,

先安裝必要資料

 

POM.XML

<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3 -->
<dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-s3</artifactId>
        <version>1.11.767</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-kms -->
<dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-kms</artifactId>
        <version>1.11.767</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-core -->
<dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-core</artifactId>
        <version>1.11.767</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.amazonaws/jmespath-java -->
<dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>jmespath-java</artifactId>
        <version>1.11.767</version>
</dependency>

get Bucket ACL

String access = "access"; //ex: AKIA3PGEPUAAAAAABBBB
String secret = "secret"; //ex: W3pIE45y64DAjeRXyBUQAAAAAAAAAABBBBBBBBBB
Regions regions = Regions.AP_EAST_1;
String bucket = "bucket";

//憑證
AWSCredentials creds = new BasicAWSCredentials(access, secret); 

//建立連線
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withRegion(regions)
                .withCredentials(new AWSStaticCredentialsProvider(creds))
                .build();

AccessControlList aclRes = s3Client.getBucketAcl(bucket);

for (Grant grant : aclRes.getGrantsAsList()) {
        System.out.println(grant.getGrantee().getIdentifier()); //182168387c385e506634d27dd5284f
        System.out.println(grant.getPermission());              //FULL_CONTROL
}

get Bucket Version On or Off

文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()

序列和反序列設定,可以設定物件轉換模式

EX:

package com.bean.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;


@lombok.Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Response {

  @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
        String message;
  
  @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
        String code;

}

refer:  http://fasterxml.github.io/jackson-annotations/javadoc/2.6/com/fasterxml/jackson/annotation/JsonProperty.Access.html


文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()

HTTP基本認證(Basic access authentication):http reques建立時,將user name和password放入header進行認證。

  1. user傳送http request 給伺服器
  2. server返回401 Unauthozied,並在Response header 放入 Access Authentication(EX: WWW-Authenticate: Basic realm="target Realm")。觸發瀏覽器帳密表單彈出
  3. user輸入user name和password,瀏覽器用BASE64邊碼後將資料放在header  Authorization (EX:  Authorization: Basic asdqwetqqrrwqrwqrqw)
  4. server取得Authorization value,進行認證

優點:

  • 方便快捷

缺點:

  • user name和password容易被破解,建議在私有環境使用
  • 不關閉瀏覽器的情況下,並沒有一種有效的方法來讓用戶登出

 

refer: 

https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/WWW-Authenticate
https://carsonwah.github.io/http-authentication.html

文章標籤

咪卡恰比 發表在 痞客邦 留言(0) 人氣()