编程小Tips

Collections.sort排序方法的最简化写法

假定按照Number对象的Id字段进行排序

正序排序

Collections.sort(resultList, Comparator.comparing(Number::getId));

逆序排序

Collections.sort(resultList, Comparator
                .comparing(Number::getId, Comparator.reverseOrder()));

初始化map集合长度最优值

  1. 假设我们期望集合中存放的元素的数量是num,那么需要将集合长度设置为(num/0.75 + 1)
  2. 或者使用guava包的Maps.newHashMapWithExpectedSize()方法来初始化map
  3. 优势:减少集合的扩容次数,属于是使用内存换性能.

参考:https://blog.csdn.net/csdn_ty/article/details/109742352

spring编程式事务实现

参考: https://blog.csdn.net/qq_33404395/article/details/83377382

Spring Boot之全局异常处理:404异常为何捕获不到?

spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false

参考:https://blog.csdn.net/w1014074794/article/details/106038996

kafka操作命令合集

https://juejin.cn/post/7022145069851361311

LocalDate获取某月第一天第一秒和最后一天最后一秒

/**
     * 获取某月份的第一天
     *
     * @param amount 传0获取当月第一天,传-1获取上个月第一天
     * @return
     */
    private LocalDateTime getFirstDayOfMonth(Integer amount) {
        LocalDate now = LocalDate.now();
        LocalDate date = now.plusMonths(amount);
        LocalDate firstDay = date.with(TemporalAdjusters.firstDayOfMonth());
        return LocalDateTime.of(firstDay, LocalTime.MIN);
    }

    /**
     * 获取某月最后一天
     *
     * @param amount 传0获取当月最后一天,传-1获取上个月最后一天
     * @return
     */
    private LocalDateTime getLastDayOfMonth(Integer amount) {
        LocalDate now = LocalDate.now();
        LocalDate date = now.plusMonths(amount);
        LocalDate lastDay = date.with(TemporalAdjusters.lastDayOfMonth());
        return LocalDateTime.of(lastDay, LocalTime.MAX);
    }

接口相应结果添加统一字段

@Component
@ControllerAdvice
public class CustomResponseBodyAdvice implements ResponseBodyAdvice<Object> {

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        // 指定需要处理的控制器方法的返回类型
        // 这里可以根据需求进行适当的判断,例如基于注解或返回类型等条件
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
            Class<? extends HttpMessageConverter<?>> selectedConverterType,
            ServerHttpRequest request, ServerHttpResponse response) {
        // 获取返回结果对象,并进行字段设置
        if (body instanceof ApiResponse) {
            ApiResponse responseObj = (ApiResponse) body;
            HttpServletRequest httpRequest = ((ServletServerHttpRequest) request).getServletRequest();

            // 从HttpServletRequest中获取请求的方法、URL和URI
            String method = httpRequest.getMethod();
            String url = httpRequest.getRequestURL().toString();
            String uri = httpRequest.getRequestURI();

            // 向返回结果对象中补充设置字段
            responseObj.setAction(method);
            responseObj.setPath(url);
            responseObj.setUri(uri);
            responseObj.setOrganization(url.split("/")[3]);
        }

        return body;
    }
}

服务端解决跨域


@Configuration
@ServletComponentScan
public class ServerConfiguration implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                // 设置允许跨域请求的域名
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS")
                .allowCredentials(true)
                .allowedHeaders("*");

    }
}
posted @ 2022-09-05 20:06  MorningBell  阅读(24)  评论(0编辑  收藏  举报