继续上集SSM项目-抢红包案例
一、引入Swagger
1、引入pom
依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
2、添加 Swagger Config
新增一个类 config.SwaggerConfig
,内容如下:
package com.wishuok.config;
import com.google.common.base.Predicate;
import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;
@WebAppConfiguration
@EnableSwagger2
@ComponentScan(basePackages = "com.wishuok.controller")//配置Swagger要扫描的包
@EnableWebMvc
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
//use controllers annotated with @Api
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) //Selection by RequestHandler
.paths(PathSelectors.any()) // and by paths
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SSM Swagger接口信息")
.description("Spring SpringMVC Mybatis 示例程序")
.contact(new Contact("keith", "http://wishuok.com", "keithhuang0124@gmail.com"))
.version("0.0.1")
.build();
}
}
只有 @Api
注解的Controller
才会在 Swagger
中显示
3、修改spring-mvc.xml
配置文件
添加如下配置:
<!--向容器自动注入配置 -->
<context:annotation-config/>
<!--配置 Swagger-ui 资源 -->
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
<!--将 SwaggerConfig 配置类注入 -->
<bean class="com.wishuok.config.SwaggerConfig"/>
4、修改 UserRedPacketController
添加注解
@Api(value = "/userRedPacket", tags = {"抢红包模拟"})
public class UserRedPacketController {
给每个 uri
添加注解
@ApiOperation(value = "redis实现", notes = "利用redis抢红包", httpMethod = "GET", response = Map.class)
5、启动项目
浏览器输入地址http://localhost/swagger-ui.html
6、 Github Commit
二、使用 log2j
记录日志
1、引入pom
依赖
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2、配置log4j.properties
改为如下内容
# LOG4J configuration
log4j.rootLogger=INFO, Appender1
log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
log4j.logger.ssmcustomlog=INFO, ssmcustomlog
log4j.appender.ssmcustomlog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ssmcustomlog.File=D:/logs/ssmcustomlog/daily.log
#每天生成一个新的log文件(如要每分钟生成新的,用:'.'yyyy-MM-dd-HH:mm
log4j.appender.ssmcustomlog.DatePattern='.'yyyy-MM-dd
log4j.appender.ssmcustomlog.layout=org.apache.log4j.PatternLayout
log4j.appender.ssmcustomlog.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
3、新增一个LogHelper
类
package com.wishuok.utils;
import org.apache.log4j.Logger;
public class LogHelper {
private static Logger logger = Logger.getLogger("ssmcustomlog");
public static void LogInfo(String log){
logger.info(log);
}
public static void LogError(String log){
logger.error(log);
}
}
4、使用log
在RedisController
添加
5、运行测试
文件位置
log内容