Springboot & Mybatis 构建restful 服务三
Springboot & Mybatis 构建restful 服务三
1 前置条件
成功执行完Springboot & Mybatis 构建restful 服务二
2 restful service 添加日志
1)新建 logback.xml文件(配置生成的日志文件的格式)
src/main/resources/logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 设置根节点
scan为true时,配置文件如果发生改变,将会被重新加载,默认值为true。
scanPeriod,设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。
debug为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。
-->
<configuration debug="false" scan="true" scanPeriod="1 seconds">
<!-- 设置记录info以及以上的等级的日志信息。设置文件名和文件内容特定格式-->
<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>
<!-- 设置只记录error等级的日志信息。设置文件名和文件内容特定格式-->
<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)修改 application.properties文件
src/main/application.properties
#指定日志配置文件的位置
logging.config=config/logback.xml
#指定当前项目的映射路径
server.context-path=/accountbalance
3)修改SY:
src/main/java/com/serena/controller/SY.java
package com.serena.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.serena.entity.SettleAccount;
import com.serena.service.ISY;
@RestController
public class SY {
@Autowired
private ISY iSY;
//定义一个从 Logger 工厂获得的静态日志变量
private static Logger logg = LoggerFactory.getLogger(SY.class);
// 通过用户 id 来查找用户余额
// 使用 get 请求传递参数
@RequestMapping(value="/account/{accountCode}",method = RequestMethod.GET)
public SettleAccount selectByAccountCode(@PathVariable("accountCode")String accountCode){
// 向日志文件添加 logger的 info 的信息
logg.info("select account by accountCode,accountCode = {}",accountCode);
SettleAccount settleAccount = iSY.selectByAccountCode(accountCode);
// 未查找到账户时,向日志文件添加logger的 warn 的信息
if(settleAccount == null)
logg.warn("not found account");
return settleAccount;
}
// 查找所有 account 的余额(SMSNum)
@RequestMapping(value="/accounts",method = RequestMethod.GET)
public List<SettleAccount> selectAccounts(){
List<SettleAccount> list = null;
// 向日志文件添加 logger的 info 的信息
logg.info("select all accounts");
list = iSY.selectAccounts();
// 未查找到账户时,向日志文件添加logger的 warn 的信息
if(list == null)
logg.warn("not found accounts");
return list;
}
}
4)在终端输入如下测试指令:
#cd 项目所在目录
cd /Users/psj/Documents/pro/xm/AccountBalance
mvn clean package
cd target
mkdir /Users/psj/Desktop/t/
#将 tar 包复制到自己指定目录(/Users/psj/Desktop/t/)
cp AccountBalance-0.0.1-SNAPSHOT.tar /Users/psj/Desktop/t/
#cd 到上个操作指定的目录
cd /Users/psj/Desktop/t
#解压 tar 包
tar -xvf AccountBalance-0.0.1-SNAPSHOT.tar
#此时可查看目录结构如要求所示
ll
#运行 可执行jar,测试结果
java -jar AccountBalance-0.0.1-SNAPSHOT.jar
#
#打开新的iterm 窗口(command+n)
http localhost:8101/accounts
http localhost:8101/account/U00001
#
#返回上个 iterm 窗口,control+c 结束服务
Springboot & Mybatis 构建restful 服务三的更多相关文章
- Springboot & Mybatis 构建restful 服务四
Springboot & Mybatis 构建restful 服务四 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务三 2 restful ...
- Springboot & Mybatis 构建restful 服务五
Springboot & Mybatis 构建restful 服务五 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务四 2 restful ...
- Springboot & Mybatis 构建restful 服务二
Springboot & Mybatis 构建restful 服务二 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务一 2 restful ...
- Springboot & Mybatis 构建restful 服务
Springboot & Mybatis 构建restful 服务一 1 前置条件 jdk测试:java -version maven测试:命令行之行mvn -v eclipse及maven插 ...
- 基于SpringBoot开发一个Restful服务,实现增删改查功能
前言 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便.快捷.但是当时并没有认真的去学习下,毕竟感觉自己在Struts和SpringMVC都用得不太熟练. ...
- jersey+maven构建restful服务
一.新建一个Maven Web项目 a) 新建一个简单的Maven项目 b) 将简单的Maven项目转成Web项目 (若没出现further configuration available--或里面的 ...
- 用Jersey构建RESTful服务7--Jersey+SQLServer+Hibernate4.3+Spring3.2
一.整体说明 本例执行演示了用 Jersey 构建 RESTful 服务中.怎样集成 Spring3 二.环境 1.上文的项目RestDemo 2.Spring及其它相关的jar ,导入项目 三.配置 ...
- JAX-RS(基于Jersey) + Spring 4.x + MyBatis构建REST服务架构
0. 大背景 众所周知,REST架构已经成为现代服务端的趋势. 很多公司,已经采用REST作为App, H5以及其它客户端的服务端架构. 1. 什么是JAX-RS? JAX-RS是JAVA EE6 引 ...
- 使用spring mvc或者resteasy构建restful服务
看到最近一个项目里用resteasy来构建restful接口,有点不明白,不少Spring mvc4.0以后也可以很方面的实现restful服务吗,为啥还要在Spring MVC的项目里还引入rest ...
随机推荐
- vmware 进入虚拟机VMware的系统后鼠标不能点
vmware 进入虚拟机VMware的系统后鼠标不能点 1)关闭虚拟机,重启win10,再打开虚拟机好了 2)
- 在CentOS7中安装scala-2.11.12
从官网下载scala的相关版本 https://www.scala-lang.org/download/2.11.12.html 解压安装包 tar zxf scala-.tgz -C /usr/sc ...
- JAVA设计方法思考之如何实现一个方法执行完毕后自动执行下一个方法
今天编程时,突然想起来在一些异步操作或Android原生库的时候,需要我们实现一些方法, 这些方法只需要我们具体实现,然后他们会在适当的时候,自动被调用! 例如AsyncTask,执行玩doInBac ...
- Vue note
1.npm run build 时,font:xx/xx "xxxx" 这种样式打包后会无效,只能写成font-size:xxx; line-height:xxx; font-fa ...
- 【题解】洛谷 P1014 【Cantor表】
1. 我们先引入三角形数的概念: >定数目的点或圆在等距离的排列下可以形成一个等边三角形,这样的数被称为三角形数. >古希腊著名科学家毕达哥拉斯把数1,3,6,10,15,21……这些数量 ...
- [原创] debian 9.3 搭建Jira+Confluence+Bitbucket+crowd+seafile (零) 修改端口的问题
[原创] debian 9.3 搭建Jira+Confluence+Bitbucket+seafile (零) 修改端口的问题 来来来,今天说个没有人说过的事, 搭建好Jira+Confluenc ...
- 问题:页面输出正常,php写入sqlserver乱码/空白。
问题一:php连接sqlsever2005,输入中文,然后查询sqlserver中对应的数据,由于提交中文是UTF-8,而sqlserver的中文为GBK,所以字段无法匹配,没有查询结果. 问题二,p ...
- tensorflow,model,object_detection,训练loss先下降后递增,到几百万,解决tensorflow,model,object,detection,loss,incease
现象:训练loss一开始下降一部分,跌代到若干次(具体多少和你的learning rate大小有关,大就迭代小就发生,小就需要多几次迭代) 日志如下(下面的日志来源于网络,我自己的日志已经clear掉 ...
- python之科学函数课——Numpy
一般来讲,数据都是由行列表示的,也就是矩阵,类似于Excel表格一样的东西. 首先我们学习一下Numpy,装好anaconda之后默认是装好的,下面是numpy的一些函数库:Numpy是科学计算库,是 ...
- Java8 之stream
总概述:Java 8 中的 Stream 是对集合(Collection)对象功能的增强,它专注于对集合对象进行各种非常便利.高效的聚合操作(aggregate operation),或者大批量数据操 ...