从0到1搭建SpringBoot框架,接口自动化-httpclient

tech2023-09-20  99

 

1. Springboot所需插件(基本大点的公司内部都有生成框架结构的代码。找RD要个链接,选择自己需要的插件生成就行)

2. 修改settings.xml路径(到哪个公司就找RD要最新的settings.xml配置文件)每个公司的镜像 源都不一样

所以要找RD要

Perferences - Maven - User setting file改成RD提供的文件

 

3. 加载完settings.xml,配置pom.xml各目录文件配置如下

3.1 API层:httpclient  Junit jacoco slf4j 

<?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> <parent> <groupId>com.baidu</groupId> <artifactId>dueros-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>dueros-api</artifactId> <dependencies> <dependency> <groupId>com.baidu.fengchao.hairuo</groupId> <artifactId>hairuo-meta</artifactId> </dependency> <!--httpclient配置开始--> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <!--httpclient配置结束--> <!--日志 start--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--<dependency>--> <!--<groupId>org.slf4j</groupId>--> <!--<artifactId>slf4j-api</artifactId>--> <!--<version>1.7.25</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.slf4j</groupId>--> <!--<artifactId>slf4j-log4j12</artifactId>--> <!--<version>1.7.25</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.slf4j</groupId>--> <!--<artifactId>slf4j-simple</artifactId>--> <!--<version>1.7.25</version>--> <!--<scope>test</scope>--> <!--</dependency>--> <!--日志end--> <!--fastjson配置开始--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.53</version> </dependency> <!--fastjson配置结束--> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>2.0.2-beta</version> </dependency> <dependency> <groupId>com.fireflysource</groupId> <artifactId>firefly</artifactId> <version>4.9.5</version> </dependency> <!-- 单元测试 --> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- jacoco配置 --> <dependency> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.0</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.0.1</version> <executions> <execution> <id>attach-sources</id> <phase>verify</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>

 

3.2 父目录层配置:log4j(API层也需要配置

<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.baidu</groupId> <artifactId>dueros-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <name>dueros</name> <description>Demo project for Spring Boot</description> <packaging>pom</packaging> <properties> <java.version>1.8</java.version> <formula.version>2.1.1.1</formula.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <modules> <module>dueros-api</module> <module>dueros-app</module> </modules> <dependencyManagement> <dependencies> <dependency> <groupId>com.baidu.formula</groupId> <artifactId>formula-dependencies-all</artifactId> <version>${formula.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!--日志 start--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> </dependencyManagement> <distributionManagement> <repository> <id>Baidu_Local</id> <name>baidu local</name> <url>http://maven.baidu-int.com/nexus/content/repositories/Baidu_Local</url> </repository> <snapshotRepository> <id>Baidu_Local_Snapshots</id> <name>baidu local snapshot</name> <url>http://maven.baidu-int.com/nexus/content/repositories/Baidu_Local_Snapshots</url> </snapshotRepository> </distributionManagement> </project>

 

4. API层的resource文件(如果没有,自己创建文件夹,右击文件夹 - Mark Directory as - Source Root)下需要配置

log4j.properties 和 logback.xml

log4j.properties配置  

# #### \u8BBE\u7F6E### #log4j.rootLogger = DEBUG, stdout, D, E, I, W, F log4j.rootLogger = ERROR, stdout, E, I, W, F log4j.additivity.org.apache=true ### \u8F93\u51FA\u4FE1\u606F\u5230\u63A7\u5236\u62AC ### log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n #### /logs/debug.log ### #log4j.appender.D = org.apache.log4j.DailyRollingFileAppender #log4j.appender.D.File = /path/debug.log #log4j.appender.D.Append = true #log4j.appender.D.Threshold = DEBUG #log4j.appender.D.layout = org.apache.log4j.PatternLayout #log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n ### /logs/error.log ### log4j.appender.E = org.apache.log4j.DailyRollingFileAppender log4j.appender.E.File =/path/error.log log4j.appender.E.Append = true log4j.appender.E.Threshold = ERROR log4j.appender.E.layout = org.apache.log4j.PatternLayout log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n #### /logs/info.log ### log4j.appender.I = org.apache.log4j.DailyRollingFileAppender log4j.appender.I.File =/path/info.log log4j.appender.I.Append = true log4j.appender.I.Threshold = INFO log4j.appender.I.layout = org.apache.log4j.PatternLayout log4j.appender.I.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n ### /logs/wran.log ### log4j.appender.W = org.apache.log4j.DailyRollingFileAppender log4j.appender.W.File =/path/warn.log log4j.appender.W.Append = true log4j.appender.W.Threshold = WARN log4j.appender.W.layout = org.apache.log4j.PatternLayout log4j.appender.W.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n ### /logs/fatal.log ### log4j.appender.F = org.apache.log4j.DailyRollingFileAppender log4j.appender.F.File =/path/fatal.log log4j.appender.F.Append = true log4j.appender.F.Threshold = FATAL log4j.appender.F.layout = org.apache.log4j.PatternLayout log4j.appender.F.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

 

logback.xml配置

 

<configuration debug="false"> <!--以下三行一定注释掉,否则还会打印部分debug日志,o.apache.commons.httpclient.HttpClient--> <!--<logger name="org.apache" level="DEBUG" />--> <!--<logger name="org.apache.http.wire" level="DEBUG" />--> <!--<logger name="org.apache.http.headers" level="INFO" />--> <property name="CONSOLE_LOG_PATTERN" value="%date{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %magenta(%-4relative) --- [%yellow(%15.15thread)] %cyan(%-40.40logger{39}) : %msg%n"/> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> </encoder> </appender> <root level="ERROR"> <appender-ref ref="STDOUT"/> </root> </configuration>

 

Java日志管理方法:

1、log4j

log4j是Apache的一个开源项目,主要是用来做Java开发中的日志管理工作。主要是由三个重要组件构成的。可管理日志的优先级、输出目的地以及输出格式等。它的配置文件主要有XML和properties两种,当然,也可以在程序里配置,但实际开发中一般使用properties文件

2、Commons logging

Apache针对不同的开发语言做了一系列的日志工具包,在Java、.net、php、c++上都可以用,并且为这些日志制定了风格一致的操作方式

这里就实现了Commons-logging(JCL),Commons-logging主要是为哪些需要在不同环境下使用不同日志结构做开发的程序员而编写的,其中包括Apache Log4j和Java log

使用Commons-loogging的Log接口,并且在运行时决定使用哪种日志架构。现在使用Commons-logging和Log4J已经成为了Java日志的标准解决方案

 

2种日志的写法  一个log4j  一个slf4j

private static Logger logger_log4j = Logger.getLogger(asRespAssert.class);

private static Logger logger =LoggerFactory.getLogger(asRespAssert.class);

 

pom文件红色上面的为log4j   下面为slf4j

注意包冲突,如果为slf4j记得导包为

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

不是apache.org.slf4j.LoggerFactory; 

 



 

遇到的问题分析以及解决

发现pom文件有的地方飘红了,那是因为包没下载下来

正常下载下来后会在这里展示

 

右击整个工程项目,maven-Download Sources下载

底部就会出现下载进度 等着就好。要是不行就得换镜像了

 

如果有问题,在终端执行 mvn test -U

最终解决:因为我之前用阿里的配置文件下载了一部分jar包。而换成百度之后,说是包冲突了。

右击项目  open module settings - libraries - 去掉有问题的包

移除报错的文件后如图

 

如遇仍无法导入 import org.apache.log4j.Logger; 则需要配置父目录的pom.xml

配置正确都不报错的样子如下

 

如遇

log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

解决办法:需要把log4j.properties配置文件放在resource目录(API层)下,会自动拷贝到class下

效果

 

 

 

如遇

[ERROR]     Non-resolvable import POM: Could not transfer artifact com.baidu.formula:formula-dependencies-all:pom:2.1.1.1 from/to central (https://repo.maven.apache.org/maven2): sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target @ line 34, column 16 -> [Help 2]  

 

 

 

如遇

SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/Users/qa/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/Users/qa/.m2/repository/org/slf4j/slf4j-log4j12/1.7.25/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

  .   ____          _            __ _ _  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )   '  |____| .__|_| |_|_| |_\__, | / / / /  =========|_|==============|___/=/_/_/_/  :: Spring Boot ::        (v2.1.9.RELEASE)

[ERROR] 2020-12-15 21:03:20,541 method:org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter.report(LoggingFailureAnalysisReporter.java:40)

*************************** APPLICATION FAILED TO START ***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    org.springframework.context.annotation.AnnotationConfigUtils.processCommonDefinitionAnnotations(AnnotationConfigUtils.java:259)

The following method did not exist:

    org.springframework.beans.factory.annotation.AnnotatedBeanDefinition.setRole(I)V

The method's class, org.springframework.beans.factory.annotation.AnnotatedBeanDefinition, is available from the following locations:

    jar:file:/Users/qa/.m2/repository/org/springframework/spring-beans/5.0.11.RELEASE/spring-beans-5.0.11.RELEASE.jar!/org/springframework/beans/factory/annotation/AnnotatedBeanDefinition.class

It was loaded from the following location:

    file:/Users/qa/.m2/repository/org/springframework/spring-beans/5.0.11.RELEASE/spring-beans-5.0.11.RELEASE.jar

Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.beans.factory.annotation.AnnotatedBeanDefinition

Process finished with exit code 1  

 

解决办法:

删除pom文件的jdbc  redis的连接

删除slf4j的配置 Springboot默认的日志配置是logback 

成功后的样子如下

 

如遇

Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationListener : org.springframework.boot.context.logging.ClasspathLoggingApplicationListener

是因为jar包没有加载下来 查找按command + o 

刷新maven

 

脚本设计部分已完成 有空了再补充

。。。。

最新回复(0)