Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java开发经验0x2 #31

Open
ichengzi opened this issue Jan 20, 2021 · 4 comments
Open

java开发经验0x2 #31

ichengzi opened this issue Jan 20, 2021 · 4 comments
Labels

Comments

@ichengzi
Copy link
Owner

ichengzi commented Jan 20, 2021

REST controller

image

国际化

https://www.liaoxuefeng.com/wiki/1252599548343744/1282384236380194

Properties读写

https://www.liaoxuefeng.com/wiki/1252599548343744/1265119084411136
image
image

ClassPath

https://www.liaoxuefeng.com/wiki/1252599548343744/1260466914339296
image

CompletableFuture-回调

https://www.liaoxuefeng.com/wiki/1252599548343744/1306581182447650
image

@PostMapping

spring mvc 参数绑定, @PostMapping 并没有默认从 http body读取输入,仍是默认读取QueryString, 需要 @RequestBody标记后才会从 http body绑定参数

@javax.validation.Valid

  1. 标记在字段上说明这个字段需要嵌套验证

@org.springframework.validation.annotation.Validated

  1. 支持分组功能
  2. 可以用在类型、方法和方法参数上。但是不能用在成员属性(字段)上
@ichengzi ichengzi changed the title spring boot 使用 java开发经验0x2 Jan 20, 2021
@ichengzi ichengzi added the JAVA label Jan 20, 2021
@ichengzi
Copy link
Owner Author

时间解析

    public static void testParse() {
        // import org.joda.time.DateTime;
        DateTime time1 = DateTime.parse("5:10", DateTimeFormat.forPattern("H:mm"));
        DateTime time2 = DateTime.parse("15:10", DateTimeFormat.forPattern("H:mm"));
        DateTime time3 = DateTime.parse("5:1", DateTimeFormat.forPattern("HH:mm"));
        // time1: 1970-01-01T05:10:00.000+08:00
        // time2: 1970-01-01T15:10:00.000+08:00
        // time3: 1970-01-01T05:01:00.000+08:00

        // joda datetime 对格式限制没有很严格, 所以这里的 pattern可以直接写作 HH:mm
    }

@ichengzi
Copy link
Owner Author

字符串格式化

        String str1 = MessageFormat.format("xxx{0}yyyy", null);
        // out: xxx{0}yyyy
        String str2 = "xxx" + null;
        // out: xxxnull

@ichengzi
Copy link
Owner Author

时间段格式化

    // org.joda.time.format.PeriodFormatter 
    public static String minutesFormat(int minutes) {
        PeriodFormatter period_formatter = new PeriodFormatterBuilder()
                .appendDays().appendSuffix("天")
                .appendHours().appendSuffix("小时")
                .appendMinutes().appendSuffix("分钟")
                .toFormatter();

        minutes = Math.abs(minutes);
        if (minutes == 0) {
            return "";
        }
        Period timeSpan = Period.minutes(minutes).normalizedStandard(PeriodType.dayTime());
        return period_formatter.print(timeSpan);
    }
        String str1 = minutesFormat(5);
        String str2 = minutesFormat(50);
        String str4 = minutesFormat(2 * 60);
        String str3 = minutesFormat(5 * 60 + 10);
        String str5 = minutesFormat(24 * 60 + 1 * 60 + 10);
        String str6 = minutesFormat(24 * 60 * 7 + 1 * 60 + 10);
        String str7 = minutesFormat(24 * 60 * 28 + 1 * 60 + 10);
        String str8 = minutesFormat(24 * 60 * 35 + 1 * 60 + 10);
        String str81 = minutesFormat(24 * 60 * 100 + 1 * 60 + 10);
        String str9 = minutesFormat(24 * 60 * 300 + 1 * 60 + 10);
        String str10 = minutesFormat(24 * 60 * 365 + 1 * 60 + 10);
        String str11 = minutesFormat(24 * 60 * 400 + 1 * 60 + 10);
        String str12 = minutesFormat(24 * 60 * 4000 + 1 * 60 + 10);

image

@ichengzi
Copy link
Owner Author

ichengzi commented May 24, 2021

array , list 相互转换

        // -------------set to array
        Set<String> set1 = new HashSet<>();
        set1.add("hello");

        // 方法1. 非泛型, 不推荐
        Object[] objects = set1.toArray();

        // runtime error, 无法强制转换
        //String[] array = (String[]) aa.toArray();

        // 方法2,泛型方式转换, 推荐
        String[] array2 = set1.toArray(new String[0]);

        // ------------array to list, set
        List<String> strList = Arrays.asList(array2);
        // strList is java.util.Arrays.ArrayList extends AbstractList<E>,
        // 只是 Arrays工具类的内部类,实现了部分List方法。
        // AbstractList<E> 没有实现 add(E), 会抛 UnsupportedOperationException
        // Arrays.ArrayList 也没有继承实现 add(E), 所以strList不支持 add,remove
        // 如果 strList业务上需要修改,可以 new java.util.ArrayList<E>(strList) 新建真正的arrayList

        Set<String> set2 = new HashSet<>(strList);
        set2.add("world");
        System.out.println(set2);
        // [world, hello]

        Set<String> set3 = new LinkedHashSet<>(strList);
        set3.add("world");
        System.out.println(set3);
        // [hello, world]

        List<String> list1 = new ArrayList<>(strList);
        list1.add("world");
        System.out.println(list1);
        // [hello, world]

c# 实现

            // list to array
            var list = new List<string>() { "hello" };
            var array = list.ToArray();

            // array to list
            var list1 = new List<string>(array);

NOTE: java vs c# ,两个实现差异巨大, 本质原因是 java 采用擦出法实现泛型, 导致编译后类型信息丢失,
jdk运行时无法获取实际类型, 所以需要编写代码时采用特殊方法来处理.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant