分类 Java开发 下的文章

背景

项目中使用了Date,前端传递的格式为:yyyy-MM-dd。而全局配置了时间格式为:yyyy-MM-dd HH:mm:ss。导致接口请求报错。
修改过程中,查询到可以使用@DateTimeFormat注解进行参数格式化。但经过测试,发现无效。

原因

经查询,原因如下:@DateTimeFormat只会在类似@RequestParam的请求参数,也就是url传参时才会起作用。而body中以Json格式传递的参数,需要使用@JsonForma进行注解。

解决方案

如果是Url传参,使用以下方式:

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date end;

如果是Body中,以Json传参,使用:

    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date end;

背景

个人习惯,经常使用shift进行中英切换,有时候切换太快了就成了双击shift了,而Idea中双击shift会弹出全局搜索框,非常影响体验。

过程

网上搜索出来的大部分结果都是,通过registry,找到ide.suppress.double.click.handler,取消勾选的方式。
但是2021.2及之后版本是没有这一项的。

解决

  1. File-Settings 打开设置窗口。
  2. 点击左侧最下面的Advanced Settings选项卡
  3. 下拉找到User Interface栏,勾选最后一项Disable double modifier key shortcuts
  4. 应用保存即可。

背景

公司部门内,配置了内网使用的域名。服务器部署完成后,nginx配置时,使用了该域名。请求后发现抛出了502 Bad Gateway

原因

查看nginx日志,发现了如下信息

2022/03/14 09:04:19 [error] 27#27: *41 no resolver defined to resolve ***.***.com, client: 10.10.60.155, server: ***.***.com, request: "POST /demo/captcha/anon/getLoginCaptcha HTTP/1.1", host: "***.***.com", referrer: "http://***.***.com/login/login"

很明显,是因为nginx无法解析该域名导致了。我们只需要让nginx能识别该域名即可。

解决方案

解决方法很简单,只需要在nginx全局配置(nginx.conf)的http项内,添加对应的dns解析即可resolver dns服务器地址;。如下所示:

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    resolver 10.10.10.1; #配置dns地址
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

背景

项目采用Jenkins+Maven实现自动化部署springboot项目。本地运行正常,但是gitlab提交后,执行自动化部署过程中,出现了如下错误:

[INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/apache/poi/poi-ooxml/3.17/poi-ooxml-3.17.jar
[INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/apache/xmlbeans/xmlbeans/2.6.0/xmlbeans-2.6.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for  
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  02:14 min
[INFO] Finished at: 2022-03-14T06:17:49Z
[INFO] ------------------------------------------------------------------------
Waiting for Jenkins to finish collecting data
[ERROR] Failed to execute goal on project **-server: Could not resolve dependencies for project *****:jar:1.0-SNAPSHOT: The following artifacts could not be resolved: org.apache.poi:poi-ooxml:jar:3.17, org.apache.xmlbeans:xmlbeans:jar:2.6.0: Could not transfer artifact org.apache.poi:poi-ooxml:jar:3.17 from/to central (https://repo.maven.apache.org/maven2): Transfer failed for https://repo.maven.apache.org/maven2/org/apache/poi/poi-ooxml/3.17/poi-ooxml-3.17.jar: Connect to repo.maven.apache.org:443 [repo.maven.apache.org/151.101.40.215] failed: Connection timed out (Connection timed out) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.

这里的描述很清楚,本地找不到poi-ooxml包,去中央仓库下载,然后国内的网络环境下,无法在https://repo.maven.apache.org下载该包。导致了构建失败。

过程及分析

一、确认Jenkins配置是否正确

首先我想到的是maven配置问题,因为我使用的是自己部署的nexus3私库,在Jenkins-管理管理-Managed files中添加了settings.xml文件并在项目中进行了配置使用。反复检查了settings.xml文件,确认没有问题。

<!-- 检查是否添加了私库镜像 -->
  <mirrors>
    <mirror>
        <id>nexus</id>
        <mirrorOf>*</mirrorOf>
        <url>http://私库Ip/nexus/repository/maven-public/</url>
    </mirror>
  </mirrors>

二、nexus配置

后来想到,可能是nexus中配置的代理仓库没有修改成国内镜像。查看后发现也没有问题。nexus中添加了阿里云镜像的代理仓库。

请输入图片描述

并把其放在了靠前的位置。
请输入图片描述

三、pom.xml配置

以上检查无误后,陷入了僵局,这个问题困扰了我一整天。期间我一度想直接在本地拷贝了这个包放到Jenkins的本地仓库中,然后问题依然存在,o(╯□╰)o。
直到后来,无意中查询到, 自定义pom.xml都是继承自super pom,所以maven项目下载一些jar包时,默认会从中央仓库下载 。知道了这点,剩下的就好办了,只要在pom.xml中进行配置,覆盖掉super pom的配置即可。

<!-- pom.xml文件中添加如下配置 -->
 <repositories>
        <repository>
            <id>nexus</id>
            <url>http://nexus.dev.dhdc.com/nexus/repository/maven-public/</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <url>http://nexus.dev.dhdc.com/nexus/repository/maven-public/</url>
        </pluginRepository>
    </pluginRepositories>

说明

看标题很绕,那是因为我目前也不知道怎么处理,但是误打误撞找到了一个临时解决方案,为了防止以后复现此类问题无法解决,这里简单记录一下。

背景

springboot开发的项目、本地使用nexus搭建了maven库,使用gitlab做代码管理,同时配合jenkins进行自动部署。
整体流程就是提交代码到gitlab后,触发jenkins进行编译并部署到docker容器。开发过程中使用了一个第三方的jar包,手动上传到了nexus库中。

问题

清理了jenkins中的.m2缓存的包后,再次在jenkins中构建会提示如下错误:

Downloaded from maven-releases: http://我的nexusip/nexus/repository/maven-releases/e-iceblue/spire.presentation.free/3.9.0/spire.presentation.free-3.9.0.jar (37 MB at 64 MB/s)
[INFO] 
...
Waiting for Jenkins to finish collecting data
[ERROR] Failed to execute goal on project data-maintain: Could not resolve dependencies for project 我的项目:jar:0.0.1-SNAPSHOT: Could not find artifact e-iceblue:spire.presentation.free:jar:3.9.0 in central (https://repo.maven.apache.org/maven2) -> [Help 1]
[ERROR] 

这里的spire.presentation.free-3.9.0.jar便是我使用的第三方jar包,很明显前面已经从我的nexus库中下载了,但是后面又去repo.maven.apache.org中去查找没找的报错了。

解决方案

这个问题困扰了我好久,至今也没找到方案,不过阴差阳错间,找到了个临时方法。过程如下:

  1. pom文件中原本是这样的
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <repositories>
        <repository>
            <id>central</id>
            <url>http://我的nexus地址/nexus/repository/maven-releases</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>central</id>
            <url>http://我的nexus地址/nexus/repository/maven-releases</url>
        </pluginRepository>
    </pluginRepositories>

    <modelVersion>4.0.0</modelVersion>
...
  1. 提交,并自动部署。会报上述错误。
  2. 将pom修改为:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>
    <modelVersion>4.0.0</modelVersion>
...
  1. 提交,并触发自动部署。仍会报错。
  2. 修改pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- 删除了这些配置-->
    <modelVersion>4.0.0</modelVersion>
  1. 提交,并jenkins进行构建。
  2. 竟然好了。

总结

先这样吧,后面找到了具体原因,在更新。