Springboot & Mybatis Building RESTful services three1 Front-facing conditions
2 RESTful service Add log1) New Logback.xml file (format of the log file generated by the configuration)
Src/main/resources/logback.xml
<?xml version= "1.0" encoding= "UTF-8"?>
When scan is true, the configuration file will be reloaded if it changes, and the default value is true.
Scanperiod, sets whether the monitoring profile has a modified interval, and if no time unit is given, the default unit is milliseconds. This property takes effect when scan is true. The default time interval is 1 minutes.
When Debug is true, the Logback internal log information is printed to see the logback running status in real time. The default value is False.
-
<configuration debug= "false" scan= "true" scanperiod= "1 Seconds" >
<!-- Set log information for record info and the above levels. Set file name and file content specific format--
<appender name= "Info_file"
class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
<rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >
<fileNamePattern>log/log.%d{yyyyMMddHH}</fileNamePattern>
</rollingPolicy>
<layout class= "Ch.qos.logback.classic.PatternLayout" >
<pattern>%d{yyyy-mm-dd HH:mm:ss. SSS}%-5level-[%-10.10thread]%15.15logger:%msg%n
</Pattern>
</layout>
</appender>
<!-- Set only log information for the error level is logged. Set file name and file content specific format--
<appender name= "Error_file"
class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
<filter class= "Ch.qos.logback.classic.filter.ThresholdFilter" >
<level>ERROR</level>
</filter>
<rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >
<FILENAMEPATTERN>LOG/ERROR.%D{YYYYMMDDHH}
</fileNamePattern>
</rollingPolicy>
<layout class= "Ch.qos.logback.classic.PatternLayout" >
<pattern>%d{yyyy-mm-dd HH:mm:ss. SSS}%-5level-[%-10.10thread]%15.15logger:%msg%n
</Pattern>
</layout>
</appender>
<root level= "Info" >
<appender-ref ref= "Info_file"/>
<appender-ref ref= "Error_file"/>
</root>
</configuration>
2) Modify the Application.properties file
Src/main/application.properties
#指定日志配置文件的位置
Logging.config=config/logback.xml
#指定当前项目的映射路径
Server.context-path=/accountbalance
3) Modify SY:
? Src/main/java/com/ejtone/controller/sy.java
Package Com.ejtone.controller;
Import java.util.List;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.web.bind.annotation.PathVariable;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.RestController;
Import Com.ejtone.entity.SettleAccount;
Import Com.ejtone.service.ISY;
?
@RestController
public class SY {
@Autowired
private ISY ISY;
Define a static log variable obtained from the Logger factory
private static Logger Logg = Loggerfactory.getlogger (Sy.class);
To find the user's balance by user ID
Passing parameters using GET requests
@RequestMapping (value= "/account/{accountcode}", method = Requestmethod.get)
Public Settleaccount Selectbyaccountcode (@PathVariable ("AccountCode") String AccountCode) {
//Add logger info to the log file
Settleaccount Settleaccount = Isy.selectbyaccountcode (AccountCode);
Add logger warn information to the log file when the account is not found
if (Settleaccount = = null)
Logg.warn ("Not Found account");
return settleaccount;
Find balances for all accounts (Smsnum)
@RequestMapping (value= "/accounts", method = Requestmethod.get)
Public list<settleaccount> selectaccounts () {
list<settleaccount> List = null;
//Add logger info to the log file
List = Isy.selectaccounts ();
Add logger warn information to the log file when the account is not found
if (list = = null)
Logg.warn ("Not Found accounts");
return list;
}
}
4) Enter the following test instructions at the terminal:
directory where #cd project is located
Cd/users/pengsijia/documents/pro/xm/accountbalance
MVN Clean Package
CD target
mkdir/users/pengsijia/desktop/t/
Copy #将 tar package to its own specified directory (/users/pengsijia/desktop/t/)
CP accountbalance-0.0.1-snapshot.tar/users/pengsijia/desktop/t/
#cd to the directory specified in the previous operation
cd/users/pengsijia/desktop/t
#解压 Tar Package
TAR-XVF Accountbalance-0.0.1-snapshot.tar
#此时可查看目录结构如要求所示
ll
#运行 executable jar, test results
Java-jar Accountbalance-0.0.1-snapshot.jar
#
#打开新的iterm window (command+n)
HTTP localhost:8101/accounts
HTTP localhost:8101/account/u20021sms001
#
#返回上个 iterm window, Control+c end service
Springboot & Mybatis Building RESTful Services Three