Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
I
ils-spring-example
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
1
合并请求
1
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
OpsTeam
ils-spring-example
Commits
e5ddec04
提交
e5ddec04
authored
12月 17, 2019
作者:
tobyfan1980
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
initialize example project
上级
e81030b4
显示空白字符变更
内嵌
并排
正在显示
27 个修改的文件
包含
1090 行增加
和
0 行删除
+1090
-0
.gitignore
.gitignore
+20
-0
README.md
README.md
+20
-0
pom.xml
ils-common/pom.xml
+61
-0
BadRequestException.java
...om/ilabservice/common/exceptions/BadRequestException.java
+11
-0
ILabServiceLogicException.java
...bservice/common/exceptions/ILabServiceLogicException.java
+24
-0
NotFoundException.java
.../com/ilabservice/common/exceptions/NotFoundException.java
+13
-0
ServerInternalException.java
...labservice/common/exceptions/ServerInternalException.java
+12
-0
CommonPageResult.java
.../java/com/ilabservice/common/result/CommonPageResult.java
+57
-0
CommonResult.java
...main/java/com/ilabservice/common/result/CommonResult.java
+86
-0
ResponseMessageCollection.java
.../ilabservice/common/result/ResponseMessageCollection.java
+13
-0
ResultErrorCode.java
...n/java/com/ilabservice/common/result/ResultErrorCode.java
+61
-0
pom.xml
ils-service/pom.xml
+111
-0
ExampleServiceApplication.java
...va/com/ilabservice/service/ExampleServiceApplication.java
+44
-0
MybatisPlusConfig.java
...ava/com/ilabservice/service/config/MybatisPlusConfig.java
+44
-0
SwaggerConfig.java
...in/java/com/ilabservice/service/config/SwaggerConfig.java
+30
-0
DeviceController.java
.../com/ilabservice/service/controller/DeviceController.java
+78
-0
GlobalExceptionHandler.java
...labservice/service/exceptions/GlobalExceptionHandler.java
+104
-0
Device.java
...e/src/main/java/com/ilabservice/service/model/Device.java
+62
-0
Measurement.java
.../main/java/com/ilabservice/service/model/Measurement.java
+40
-0
DeviceItem.java
.../src/main/java/com/ilabservice/service/vo/DeviceItem.java
+19
-0
application-test.yml
ils-service/src/main/resources/application-test.yml
+67
-0
application.yml
ils-service/src/main/resources/application.yml
+4
-0
bootstrap.sql
...e/src/main/resources/db_migration/bootstrap/bootstrap.sql
+57
-0
en.json
ils-service/src/main/resources/translation/en.json
+9
-0
zh.json
ils-service/src/main/resources/translation/zh.json
+9
-0
pom.xml
pom.xml
+30
-0
ExampleApplication.java
...main/java/com/ilabservice/example/ExampleApplication.java
+4
-0
没有找到文件。
.gitignore
浏览文件 @
e5ddec04
...
...
@@ -4,6 +4,26 @@
# Log file
*.log
# Intellij
**/.idea
*.iml
*.iws
*.factorypath
# Eclipse
*.classpath
*.project
.settings/
# Mac
.DS_Store
# Maven
**/log/
**/target/
**/logs/
# BlueJ files
*.ctxt
...
...
README.md
0 → 100644
浏览文件 @
e5ddec04
# ils-spring-example
这是一个 spring boot 的样本代码. 希望做 java 的服务端研发同学使用这个样本为模板来开发.
主要有一下几个方面的内容
## 异常类的定义
需要定义每个会被抛出的异常, 对应的 http 状态码.
在 GlobalExceptionHandler 中处理所有的异常类.
每一种逻辑异常都应该定义为枚举类 ResultErrorCode 中的一项, 包括错误码和错误串
## 异常返回消息
服务端做多语言处理. 客户端传入 header x-language. 服务端对每个语言维护一个 json 文件, 把错误串翻译为描述文字. 服务启动的时候加载.
原则上来讲: 只有暴露给网关的服务需要做语言处理. 更下层的服务, 只需要抛出异常.
## swagger 嵌入
我们用 swagger 来统一我们的接口文档. 在接口评审的时候, 开发可以使用这个模板做前期的定义. swagger 需要嵌入到以下几个方面
*
controller 类的定义
*
controller 方法的定义, 包括接口描述, 入参, 返回值
*
model 已经 VO 里的类, 只要是会被作为返回值, 就需要用 swagger 的注释来解释.
ils-common/pom.xml
0 → 100644
浏览文件 @
e5ddec04
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<parent>
<artifactId>
ils-spring-example
</artifactId>
<groupId>
com.ilabservice
</groupId>
<version>
1.0-SNAPSHOT
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<artifactId>
ils-common
</artifactId>
<dependencies>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-lang3
</artifactId>
<version>
3.5
</version>
</dependency>
<dependency>
<groupId>
com.fasterxml.jackson.core
</groupId>
<artifactId>
jackson-databind
</artifactId>
</dependency>
<dependency>
<groupId>
com.fasterxml.jackson.core
</groupId>
<artifactId>
jackson-core
</artifactId>
</dependency>
<dependency>
<groupId>
com.fasterxml.jackson.core
</groupId>
<artifactId>
jackson-annotations
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
<version>
1.5.1.RELEASE
</version>
</dependency>
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
<version>
1.18.0
</version>
<scope>
provided
</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
ils-common/src/main/java/com/ilabservice/common/exceptions/BadRequestException.java
0 → 100644
浏览文件 @
e5ddec04
package
com
.
ilabservice
.
common
.
exceptions
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
@ResponseStatus
(
HttpStatus
.
BAD_REQUEST
)
public
class
BadRequestException
extends
RuntimeException
{
public
BadRequestException
(
String
msg
){
super
(
msg
);
}
}
ils-common/src/main/java/com/ilabservice/common/exceptions/ILabServiceLogicException.java
0 → 100644
浏览文件 @
e5ddec04
package
com
.
ilabservice
.
common
.
exceptions
;
import
lombok.Data
;
@Data
public
class
ILabServiceLogicException
extends
RuntimeException
{
Integer
code
;
Object
data
;
public
ILabServiceLogicException
(
Integer
code
,
String
msg
){
super
(
msg
);
this
.
code
=
code
;
data
=
null
;
}
public
ILabServiceLogicException
(
Integer
code
,
String
msg
,
Object
data
){
super
(
msg
);
this
.
code
=
code
;
this
.
data
=
data
;
}
}
ils-common/src/main/java/com/ilabservice/common/exceptions/NotFoundException.java
0 → 100644
浏览文件 @
e5ddec04
package
com
.
ilabservice
.
common
.
exceptions
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
@ResponseStatus
(
HttpStatus
.
NOT_FOUND
)
public
class
NotFoundException
extends
RuntimeException
{
public
NotFoundException
(
String
msg
){
super
(
msg
);
}
}
ils-common/src/main/java/com/ilabservice/common/exceptions/ServerInternalException.java
0 → 100644
浏览文件 @
e5ddec04
package
com
.
ilabservice
.
common
.
exceptions
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
@ResponseStatus
(
HttpStatus
.
INTERNAL_SERVER_ERROR
)
public
class
ServerInternalException
extends
RuntimeException
{
public
ServerInternalException
(
String
msg
){
super
(
msg
);
}
}
ils-common/src/main/java/com/ilabservice/common/result/CommonPageResult.java
0 → 100644
浏览文件 @
e5ddec04
package
com
.
ilabservice
.
common
.
result
;
import
lombok.Data
;
import
org.springframework.web.bind.annotation.ResponseBody
;
/**
* 分页查询返回结果
*
* @author zxy
* @since 1.0.1
* Created by zxy on 2019/8/26.
*/
@Data
@ResponseBody
public
class
CommonPageResult
<
T
>
extends
CommonResult
<
T
>
{
private
static
final
long
serialVersionUID
=
-
7316136625437068129L
;
/**
* 查询总数
*/
private
int
totalCount
;
/**
* 当前页
*/
private
int
pageNum
;
/**
* 分页数量
*/
private
int
pageSize
;
public
CommonPageResult
(
T
data
)
{
super
(
data
);
}
public
CommonPageResult
()
{
super
();
}
/**
* 异常构造函数
*
* @param code
* @param message
* @param data
*/
public
CommonPageResult
(
Integer
code
,
String
message
,
T
data
)
{
super
(
code
,
message
,
data
);
}
public
CommonPageResult
(
boolean
success
,
Integer
code
,
String
message
,
T
data
)
{
super
(
success
,
code
,
message
,
data
);
}
}
ils-common/src/main/java/com/ilabservice/common/result/CommonResult.java
0 → 100644
浏览文件 @
e5ddec04
package
com
.
ilabservice
.
common
.
result
;
import
lombok.Data
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
java.io.Serializable
;
/**
* 通用服务结果
*
* @author zxy
* @version $Id: CommonResult.java
*/
@Data
@ResponseBody
public
class
CommonResult
<
T
>
implements
Serializable
{
private
static
final
long
serialVersionUID
=
-
1848071965814004979L
;
/**
* 操作是否成功
*/
private
boolean
success
=
true
;
/**
* 结果码
*/
private
Integer
code
=
ResultErrorCode
.
SUCCESS
.
getCode
();
/**
* 结果描述
*/
private
String
message
=
ResultErrorCode
.
SUCCESS
.
getValue
();
/**
* 结果
*/
private
T
data
;
/**
* 默认构造函数
*/
public
CommonResult
()
{
super
();
}
public
CommonResult
(
boolean
success
,
Integer
code
,
String
message
,
T
data
)
{
this
.
success
=
success
;
this
.
code
=
code
;
this
.
message
=
message
;
this
.
data
=
data
;
}
/**
* 失败构造函数
*
* @param code
* @param message
* @param data
*/
public
CommonResult
(
Integer
code
,
String
message
,
T
data
)
{
this
.
success
=
false
;
this
.
code
=
code
;
this
.
message
=
message
;
this
.
data
=
data
;
}
public
CommonResult
(
T
data
)
{
this
.
data
=
data
;
}
public
boolean
isSuccess
()
{
return
success
;
}
public
void
setSuccess
(
boolean
success
)
{
this
.
success
=
success
;
}
public
T
getData
()
{
return
data
;
}
public
void
setData
(
T
data
)
{
this
.
data
=
data
;
}
}
ils-common/src/main/java/com/ilabservice/common/result/ResponseMessageCollection.java
0 → 100644
浏览文件 @
e5ddec04
package
com
.
ilabservice
.
common
.
result
;
import
java.util.Map
;
public
class
ResponseMessageCollection
{
public
static
final
String
DEFAULT_LANGUAGE
=
"chinese"
;
public
static
Map
<
String
,
String
>
englishErrorMessage
;
public
static
Map
<
String
,
String
>
chineseErrorMessage
;
public
static
Map
<
String
,
Map
<
String
,
String
>>
errorMessageByLanguage
;
}
ils-common/src/main/java/com/ilabservice/common/result/ResultErrorCode.java
0 → 100644
浏览文件 @
e5ddec04
package
com
.
ilabservice
.
common
.
result
;
public
enum
ResultErrorCode
{
ERROR_DATA
(
500
,
"{'code':500,'message':'系统错误'}"
),
/**
* 成功狀態碼
*/
SUCCESS
(
0
,
"Succeed"
),
/**
* HTTP常用状态码
*/
REQUEST_SUCCESS
(
200
,
"REQUEST_SUCCESS"
),
NO_CONTENT
(
204
,
"NO_CONTENT"
),
PARTIAL_CONTENT
(
206
,
"PARTIAL_CONTENT"
),
MOVED_PERMANENTLY
(
301
,
"MOVED_PERMANENTLY"
),
FOUND
(
302
,
"FOUND"
),
SEE_OTHER
(
303
,
"SEE_OTHER"
),
NOT_MODIFIED
(
304
,
"NOT_MODIFIED"
),
TEMPORARY_REDIRECT
(
307
,
"TEMPORARY_REDIRECT"
),
NOT_PERMISSION
(
401
,
"NOT_PERMISSION"
),
FORBIDDEN
(
403
,
"FORBIDDEN"
),
NOT_FOUND_PAGE_OR_URL_OR_MODEL
(
404
,
"NOT_FOUND_PAGE_OR_URL_OR_MODEL"
),
DELETE_INFO_ERROR
(
409
,
"DELETE_INFO_ERROR"
),
SERVER_ERROR
(
500
,
"SERVER_ERROR"
),
SERVICE_UNAVAILABLE
(
503
,
"SERVICE_UNAVAILABLE"
),
/**
* 数据库
*/
DB_ERROR
(
11
,
"DATABASE_ERROR"
),
DB_INSERT_ERROR
(
12
,
"DB_INSERT_ERROR"
),
DB_UPDATE_ERROR
(
13
,
"DB_INSERT_ERROR"
),
DB_DELETE_ERROR
(
14
,
"DB_DELETE_ERROR"
);
private
String
value
;
private
int
code
;
ResultErrorCode
(
int
code
,
String
value
)
{
this
.
code
=
code
;
this
.
value
=
value
;
}
public
String
getValue
()
{
return
value
;
}
public
int
getCode
()
{
return
code
;
}
public
static
ResultErrorCode
codeOf
(
int
code
)
{
for
(
ResultErrorCode
resultErrorCodeEnum
:
values
())
{
if
(
resultErrorCodeEnum
.
getCode
()
==
code
)
{
return
resultErrorCodeEnum
;
}
}
throw
new
RuntimeException
(
"没有找到对应枚举"
);
}
}
ils-service/pom.xml
0 → 100644
浏览文件 @
e5ddec04
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<parent>
<artifactId>
ils-spring-example
</artifactId>
<groupId>
com.ilabservice
</groupId>
<version>
1.0-SNAPSHOT
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<artifactId>
ils-service
</artifactId>
<properties>
<java.version>
1.8
</java.version>
<mybatis-plus.version>
3.1.0
</mybatis-plus.version>
</properties>
<dependencies>
<dependency>
<groupId>
com.ilabservice
</groupId>
<artifactId>
ils-common
</artifactId>
<version>
1.0-SNAPSHOT
</version>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
<version>
1.18.0
</version>
<scope>
provided
</scope>
</dependency>
<dependency>
<groupId>
io.springfox
</groupId>
<artifactId>
springfox-swagger2
</artifactId>
<version>
2.6.1
</version>
</dependency>
<dependency>
<groupId>
io.springfox
</groupId>
<artifactId>
springfox-swagger-ui
</artifactId>
<version>
2.6.1
</version>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<!-- mybatis pager -->
<dependency>
<groupId>
com.github.pagehelper
</groupId>
<artifactId>
pagehelper
</artifactId>
<version>
4.1.0
</version>
</dependency>
<dependency>
<groupId>
com.baomidou
</groupId>
<artifactId>
mybatis-plus-boot-starter
</artifactId>
<version>
${mybatis-plus.version}
</version>
</dependency>
<dependency>
<groupId>
mysql
</groupId>
<artifactId>
mysql-connector-java
</artifactId>
</dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
<version>
1.2.58
</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-surefire-plugin
</artifactId>
<version>
2.19
</version>
<configuration>
<includes>
<include>
**/*Test.java
</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-maven-plugin
</artifactId>
<configuration>
<outputDirectory>
../output
</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
ils-service/src/main/java/com/ilabservice/service/ExampleServiceApplication.java
0 → 100644
浏览文件 @
e5ddec04
package
com
.
ilabservice
.
service
;
import
com.alibaba.fastjson.JSON
;
import
com.ilabservice.common.result.ResponseMessageCollection
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
java.io.InputStream
;
import
java.nio.charset.Charset
;
import
java.util.HashMap
;
import
java.util.Map
;
@Slf4j
@SpringBootApplication
public
class
ExampleServiceApplication
{
public
static
void
loadTranslation
()
{
log
.
info
(
"loading translation"
);
try
{
InputStream
in
=
Thread
.
currentThread
().
getContextClassLoader
().
getResourceAsStream
(
"translation/en.json"
);
ResponseMessageCollection
.
englishErrorMessage
=
JSON
.
parseObject
(
in
,
Charset
.
forName
(
"UTF-8"
),
Map
.
class
);
}
catch
(
Exception
e
){
log
.
error
(
"failed to read en.json, cannot do translation for English. err: "
+
e
);
}
try
{
InputStream
in
=
Thread
.
currentThread
().
getContextClassLoader
().
getResourceAsStream
(
"translation/zh.json"
);
ResponseMessageCollection
.
chineseErrorMessage
=
JSON
.
parseObject
(
in
,
Charset
.
forName
(
"UTF-8"
),
Map
.
class
);
}
catch
(
Exception
e
){
log
.
error
(
"failed to read zh.json, cannot do translation for Chinese. err: "
+
e
);
}
ResponseMessageCollection
.
errorMessageByLanguage
=
new
HashMap
<>();
ResponseMessageCollection
.
errorMessageByLanguage
.
put
(
"chinese"
,
ResponseMessageCollection
.
chineseErrorMessage
);
ResponseMessageCollection
.
errorMessageByLanguage
.
put
(
"english"
,
ResponseMessageCollection
.
englishErrorMessage
);
}
public
static
void
main
(
String
[]
args
){
loadTranslation
();
SpringApplication
.
run
(
ExampleServiceApplication
.
class
,
args
);
}
}
ils-service/src/main/java/com/ilabservice/service/config/MybatisPlusConfig.java
0 → 100644
浏览文件 @
e5ddec04
package
com
.
ilabservice
.
service
.
config
;
import
com.baomidou.mybatisplus.core.injector.ISqlInjector
;
import
com.baomidou.mybatisplus.core.parser.ISqlParser
;
import
com.baomidou.mybatisplus.extension.injector.LogicSqlInjector
;
import
com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor
;
import
org.mybatis.spring.annotation.MapperScan
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
java.util.ArrayList
;
import
java.util.List
;
@Configuration
@MapperScan
({
"com.ilabservice.intelab.video.dao"
})
public
class
MybatisPlusConfig
{
/**
* 分页插件
*
* @return PaginationInterceptor
*/
@Bean
@ConditionalOnMissingBean
public
PaginationInterceptor
paginationInterceptor
()
{
PaginationInterceptor
paginationInterceptor
=
new
PaginationInterceptor
();
List
<
ISqlParser
>
sqlParserList
=
new
ArrayList
<>();
paginationInterceptor
.
setSqlParserList
(
sqlParserList
);
return
paginationInterceptor
;
}
/**
* 逻辑删除插件
*
* @return LogicSqlInjector
*/
@Bean
@ConditionalOnMissingBean
public
ISqlInjector
sqlInjector
()
{
return
new
LogicSqlInjector
();
}
}
ils-service/src/main/java/com/ilabservice/service/config/SwaggerConfig.java
0 → 100644
浏览文件 @
e5ddec04
package
com
.
ilabservice
.
service
.
config
;
import
io.swagger.annotations.ApiOperation
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
springfox.documentation.builders.ApiInfoBuilder
;
import
springfox.documentation.builders.RequestHandlerSelectors
;
import
springfox.documentation.service.ApiInfo
;
import
springfox.documentation.spi.DocumentationType
;
import
springfox.documentation.spring.web.plugins.Docket
;
import
springfox.documentation.swagger2.annotations.EnableSwagger2
;
@Configuration
@EnableSwagger2
public
class
SwaggerConfig
{
@Bean
public
Docket
productApi
()
{
return
new
Docket
(
DocumentationType
.
SWAGGER_2
)
.
apiInfo
(
apiInfo
()).
select
()
.
apis
(
RequestHandlerSelectors
.
withMethodAnnotation
(
ApiOperation
.
class
)).
build
();
}
private
ApiInfo
apiInfo
()
{
return
new
ApiInfoBuilder
()
.
title
(
"ilabservice example service"
)
.
description
(
"example service to manage device"
)
.
version
(
"1.0"
)
.
build
();
}
}
ils-service/src/main/java/com/ilabservice/service/controller/DeviceController.java
0 → 100644
浏览文件 @
e5ddec04
package
com
.
ilabservice
.
service
.
controller
;
import
com.ilabservice.common.exceptions.BadRequestException
;
import
com.ilabservice.common.exceptions.NotFoundException
;
import
com.ilabservice.common.exceptions.ServerInternalException
;
import
com.ilabservice.common.result.CommonPageResult
;
import
com.ilabservice.common.result.CommonResult
;
import
com.ilabservice.common.result.ResultErrorCode
;
import
com.ilabservice.service.model.Device
;
import
com.ilabservice.service.vo.DeviceItem
;
import
io.swagger.annotations.*
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.bind.annotation.*
;
import
java.util.List
;
@Slf4j
@RestController
@RequestMapping
(
"/device"
)
@Api
(
value
=
"device"
,
description
=
"设备相关接口"
,
tags
=
{
"device"
})
public
class
DeviceController
{
@ApiOperation
(
value
=
"get device list"
,
notes
=
"使用 optional 的过滤器来查询设备, 支持模糊查询和翻页"
)
@ApiResponses
({
@ApiResponse
(
code
=
400
,
message
=
"参数非法"
),
@ApiResponse
(
code
=
404
,
message
=
"设备不存在"
),
@ApiResponse
(
code
=
500
,
message
=
"服务器错误"
),
@ApiResponse
(
code
=
200
,
message
=
"设备列表 with 简化设备项"
)
})
@ApiImplicitParams
({
@ApiImplicitParam
(
name
=
"name"
,
value
=
"设备名, 模糊查询"
,
dataType
=
"string"
,
example
=
"dev"
,
paramType
=
"query"
),
@ApiImplicitParam
(
name
=
"model"
,
value
=
"型号, 模糊查询"
,
dataType
=
"string"
,
example
=
"XYZ001"
,
paramType
=
"query"
)
})
@GetMapping
(
""
)
CommonPageResult
<
List
<
DeviceItem
>>
getDeviceList
(
@RequestParam
(
required
=
false
,
value
=
"name"
)
String
name
,
@RequestParam
(
required
=
false
,
value
=
"model"
)
String
mode
,
@RequestParam
(
required
=
false
,
value
=
"offset"
)
Integer
offset
,
@RequestParam
(
required
=
false
,
value
=
"limit"
)
Integer
limit
)
{
// TODO: add logic here, following is fake logic
try
{
if
(
name
==
"db not found"
)
{
log
.
error
(
"device not found"
);
throw
new
NotFoundException
(
ResultErrorCode
.
DB_ERROR
.
getValue
());
}
else
if
(
name
==
""
)
{
log
.
error
(
"device name illegal"
);
throw
new
BadRequestException
(
ResultErrorCode
.
DB_ERROR
.
getValue
());
}
}
catch
(
Exception
e
){
log
.
error
(
"unexpected error: "
+
e
.
toString
());
throw
new
ServerInternalException
(
e
.
toString
());
}
return
new
CommonPageResult
<>();
}
@ApiOperation
(
value
=
"get device by id"
,
notes
=
"查询指定id的设备"
)
@ApiResponses
({
@ApiResponse
(
code
=
404
,
message
=
"设备不存在"
),
@ApiResponse
(
code
=
500
,
message
=
"服务器错误"
),
@ApiResponse
(
code
=
200
,
message
=
"设备详细信息"
)
})
@ApiImplicitParams
({
@ApiImplicitParam
(
name
=
"id"
,
value
=
"设备id"
,
dataType
=
"int"
,
example
=
"1"
,
paramType
=
"path"
)
})
@GetMapping
(
"/{id}"
)
CommonResult
<
Device
>
getDevice
(
@PathVariable
(
required
=
true
,
value
=
"id"
)
Integer
id
)
{
// TODO: add logic here
return
new
CommonResult
<>();
}
}
ils-service/src/main/java/com/ilabservice/service/exceptions/GlobalExceptionHandler.java
0 → 100644
浏览文件 @
e5ddec04
package
com
.
ilabservice
.
service
.
exceptions
;
import
com.ilabservice.common.exceptions.BadRequestException
;
import
com.ilabservice.common.exceptions.ILabServiceLogicException
;
import
com.ilabservice.common.exceptions.NotFoundException
;
import
com.ilabservice.common.exceptions.ServerInternalException
;
import
com.ilabservice.common.result.CommonResult
;
import
com.ilabservice.common.result.ResponseMessageCollection
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
import
javax.servlet.http.HttpServletRequest
;
@ControllerAdvice
public
class
GlobalExceptionHandler
{
@Value
(
"${runtime.debug:#{false}}"
)
Boolean
runtimeDebug
;
private
String
getTranslatedMessage
(
String
headerLang
,
String
message
){
if
(
headerLang
==
null
)
{
headerLang
=
ResponseMessageCollection
.
DEFAULT_LANGUAGE
;
}
if
(
ResponseMessageCollection
.
errorMessageByLanguage
.
get
(
headerLang
.
toLowerCase
())
!=
null
&&
ResponseMessageCollection
.
errorMessageByLanguage
.
get
(
headerLang
.
toLowerCase
()).
containsKey
(
message
))
{
return
ResponseMessageCollection
.
errorMessageByLanguage
.
get
(
headerLang
.
toLowerCase
()).
get
(
message
);
}
else
{
return
message
;
}
}
/**
* 逻辑错误http 返回码为 200, 实际错误码在 entity 里的 code 字段
* @param request
* @param err
* @return
*/
@ExceptionHandler
(
ILabServiceLogicException
.
class
)
@ResponseBody
public
CommonResult
<
Object
>
handleLogicErrorException
(
HttpServletRequest
request
,
ILabServiceLogicException
err
)
{
CommonResult
<
Object
>
errorInfo
=
new
CommonResult
<>();
errorInfo
.
setSuccess
(
false
);
errorInfo
.
setCode
(
err
.
getCode
());
errorInfo
.
setMessage
(
getTranslatedMessage
(
request
.
getHeader
(
"x-language"
),
err
.
getMessage
()));
errorInfo
.
setData
(
null
);
return
errorInfo
;
}
@ResponseStatus
(
HttpStatus
.
BAD_REQUEST
)
@ExceptionHandler
(
BadRequestException
.
class
)
@ResponseBody
public
CommonResult
<
Object
>
handleBadRequestException
(
HttpServletRequest
request
,
BadRequestException
err
)
{
CommonResult
<
Object
>
errorInfo
=
new
CommonResult
<>();
errorInfo
.
setSuccess
(
false
);
errorInfo
.
setMessage
(
getTranslatedMessage
(
request
.
getHeader
(
"x-language"
),
err
.
getMessage
()));
errorInfo
.
setData
(
null
);
errorInfo
.
setCode
(
400
);
return
errorInfo
;
}
@ResponseStatus
(
HttpStatus
.
NOT_FOUND
)
@ExceptionHandler
(
NotFoundException
.
class
)
@ResponseBody
public
CommonResult
<
Object
>
handleNotFoundException
(
HttpServletRequest
request
,
NotFoundException
err
)
{
CommonResult
<
Object
>
errorInfo
=
new
CommonResult
<>();
errorInfo
.
setSuccess
(
false
);
errorInfo
.
setMessage
(
getTranslatedMessage
(
request
.
getHeader
(
"x-language"
),
err
.
getMessage
()));
errorInfo
.
setData
(
null
);
errorInfo
.
setCode
(
404
);
return
errorInfo
;
}
@ResponseStatus
(
HttpStatus
.
INTERNAL_SERVER_ERROR
)
@ExceptionHandler
(
ServerInternalException
.
class
)
@ResponseBody
public
CommonResult
<
Object
>
handleServerInternalException
(
HttpServletRequest
request
,
ServerInternalException
err
)
{
CommonResult
<
Object
>
errorInfo
=
new
CommonResult
<>();
errorInfo
.
setSuccess
(
false
);
if
(
runtimeDebug
)
{
errorInfo
.
setMessage
(
getTranslatedMessage
(
request
.
getHeader
(
"x-language"
),
err
.
getMessage
()));
}
else
{
errorInfo
.
setMessage
(
getTranslatedMessage
(
request
.
getHeader
(
"x-language"
),
"INTERNAL_SERVER_ERROR"
));
}
errorInfo
.
setData
(
null
);
errorInfo
.
setCode
(
500
);
return
errorInfo
;
}
}
ils-service/src/main/java/com/ilabservice/service/model/Device.java
0 → 100644
浏览文件 @
e5ddec04
package
com
.
ilabservice
.
service
.
model
;
import
com.baomidou.mybatisplus.annotation.TableField
;
import
com.baomidou.mybatisplus.annotation.TableId
;
import
com.baomidou.mybatisplus.annotation.TableName
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
@Data
@ApiModel
(
value
=
"device"
)
@TableName
(
"device"
)
public
class
Device
{
@ApiModelProperty
(
value
=
"device id"
)
@TableId
(
value
=
"id"
)
private
Integer
id
;
/**
* 名称
*/
@ApiModelProperty
(
value
=
"device name"
,
example
=
"dev-001"
)
@TableField
(
value
=
"name"
)
private
String
name
;
/**
* 商标
*/
@ApiModelProperty
(
value
=
"manufacture brand"
,
example
=
"thermofisher"
)
@TableField
(
value
=
"brand"
)
private
String
brand
;
/**
* 型号
*/
@ApiModelProperty
(
value
=
"device model"
,
example
=
"YX001"
)
@TableField
(
value
=
"model"
)
private
String
model
;
/**
* 重量
*/
@ApiModelProperty
(
value
=
"device weight in kg"
,
example
=
"3.22"
)
@TableField
(
value
=
"weight"
)
private
Double
weight
;
/**
* 长度
*/
@ApiModelProperty
(
value
=
"device length in cm"
,
example
=
"100"
)
@TableField
(
value
=
"length"
)
private
Integer
length
;
/**
* 高度
*/
@ApiModelProperty
(
value
=
"device height in cm"
,
example
=
"50"
)
@TableField
(
value
=
"height"
)
private
Integer
height
;
}
ils-service/src/main/java/com/ilabservice/service/model/Measurement.java
0 → 100644
浏览文件 @
e5ddec04
package
com
.
ilabservice
.
service
.
model
;
import
com.baomidou.mybatisplus.annotation.TableField
;
import
com.baomidou.mybatisplus.annotation.TableId
;
import
com.baomidou.mybatisplus.annotation.TableName
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
@Data
@ApiModel
(
value
=
"measurement"
)
@TableName
(
"measurement"
)
public
class
Measurement
{
@ApiModelProperty
(
value
=
""
)
@TableId
(
value
=
"id"
)
private
Integer
id
;
/**
* 名称
*/
@ApiModelProperty
(
value
=
""
)
@TableField
(
value
=
"name"
)
private
String
name
;
/**
* 单位
*/
@ApiModelProperty
(
value
=
""
)
@TableField
(
value
=
"unit"
)
private
String
unit
;
/**
* 别名
*/
@ApiModelProperty
(
value
=
""
)
@TableField
(
value
=
"alias"
)
private
String
alias
;
}
ils-service/src/main/java/com/ilabservice/service/vo/DeviceItem.java
0 → 100644
浏览文件 @
e5ddec04
package
com
.
ilabservice
.
service
.
vo
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
@Data
@ApiModel
(
value
=
"设备列表中的设备项"
)
public
class
DeviceItem
{
@ApiModelProperty
(
value
=
"设备 id"
,
example
=
"11"
,
dataType
=
"int"
)
Integer
id
;
@ApiModelProperty
(
value
=
"设备名"
,
example
=
"dev-001"
)
String
name
;
@ApiModelProperty
(
value
=
"设备型号"
,
example
=
"YX001"
)
String
model
;
}
ils-service/src/main/resources/application-test.yml
0 → 100644
浏览文件 @
e5ddec04
server
:
port
:
9999
mybatis-plus
:
mapper-locations
:
classpath*:mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage
:
com.ilabservice.intelab.video.model
#typeEnumsPackage: com.ilabservice.intelab.model.enums
global-config
:
# 自动刷新Mapper对应的XML文件
refresh
:
true
# 关闭MP3.0自带的banner
banner
:
false
db-config
:
# 主键类型 0:数据库ID自增 1.未定义 2.用户输入 3 id_worker 4.uuid 5.id_worker字符串表示
id-type
:
AUTO
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
field-strategy
:
NOT_EMPTY
# 默认数据库表下划线命名
table-underline
:
true
# 逻辑删除配置
# 逻辑删除全局值(1表示已删除,这也是Mybatis Plus的默认配置)
# sqlInjector: com.baomidou.mybatisplus.mapper.LogicSqlInjector
#logic-delete-value: 1
# 逻辑未删除全局值(0表示未删除,这也是Mybatis Plus的默认配置)
logic-not-delete-value
:
0
configuration
:
map-underscore-to-camel-case
:
true
cache-enabled
:
false
spring
:
main
:
allow-bean-definition-overriding
:
true
datasource
:
name
:
ilsexample
url
:
jdbc:mysql://localhost:3306/ilsexample?useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8
username
:
root
password
:
root1234
driver-class-name
:
com.mysql.jdbc.Driver
filters
:
stat
maxActive
:
20
initialSize
:
1
maxWait
:
60000
minIdle
:
1
timeBetweenEvictionRunsMillis
:
60000
minEvictableIdleTimeMillis
:
300000
validationQuery
:
select 'x'
testWhileIdle
:
true
testOnBorrow
:
false
testOnReturn
:
false
poolPreparedStatements
:
true
maxOpenPreparedStatements
:
20
initialize
:
false
jpa
:
hibernate
:
show-sql
:
true
database-platform
:
org.hibernate.dialect.MySQLDialect
application
:
name
:
ils-example-service
runtime
:
debug
:
true
\ No newline at end of file
ils-service/src/main/resources/application.yml
0 → 100644
浏览文件 @
e5ddec04
spring
:
profiles
:
active
:
test
\ No newline at end of file
ils-service/src/main/resources/db_migration/bootstrap/bootstrap.sql
0 → 100644
浏览文件 @
e5ddec04
-- MySQL Workbench Forward Engineering
SET
@
OLD_UNIQUE_CHECKS
=@@
UNIQUE_CHECKS
,
UNIQUE_CHECKS
=
0
;
SET
@
OLD_FOREIGN_KEY_CHECKS
=@@
FOREIGN_KEY_CHECKS
,
FOREIGN_KEY_CHECKS
=
0
;
SET
@
OLD_SQL_MODE
=@@
SQL_MODE
,
SQL_MODE
=
'TRADITIONAL,ALLOW_INVALID_DATES'
;
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema ilsexample
-- -----------------------------------------------------
DROP
SCHEMA
IF
EXISTS
`ilsexample`
;
-- -----------------------------------------------------
-- Schema ilsexample
-- -----------------------------------------------------
CREATE
SCHEMA
IF
NOT
EXISTS
`ilsexample`
DEFAULT
CHARACTER
SET
latin1
;
USE
`ilsexample`
;
-- -----------------------------------------------------
-- Table `ilsexample`.`device`
-- -----------------------------------------------------
DROP
TABLE
IF
EXISTS
`ilsexample`
.
`device`
;
CREATE
TABLE
IF
NOT
EXISTS
`ilsexample`
.
`device`
(
`id`
INT
(
11
)
NOT
NULL
AUTO_INCREMENT
,
`name`
VARCHAR
(
45
)
NULL
DEFAULT
NULL
COMMENT
'设备名'
,
`brand`
VARCHAR
(
45
)
NULL
DEFAULT
NULL
COMMENT
'品牌'
,
`model`
VARCHAR
(
45
)
NULL
DEFAULT
NULL
COMMENT
'模型'
,
`enabled`
TINYINT
(
4
)
NULL
DEFAULT
NULL
COMMENT
'激活'
,
`weight`
DOUBLE
NULL
DEFAULT
NULL
COMMENT
'重量
\n
'
,
`length`
INT
(
11
)
NULL
DEFAULT
NULL
COMMENT
'长度 cm'
,
`height`
INT
(
11
)
NULL
DEFAULT
NULL
COMMENT
'高度 cm'
,
PRIMARY
KEY
(
`id`
))
ENGINE
=
InnoDB
DEFAULT
CHARACTER
SET
=
latin1
;
-- -----------------------------------------------------
-- Table `ilsexample`.`measurement`
-- -----------------------------------------------------
DROP
TABLE
IF
EXISTS
`ilsexample`
.
`measurement`
;
CREATE
TABLE
IF
NOT
EXISTS
`ilsexample`
.
`measurement`
(
`id`
INT
(
11
)
NOT
NULL
,
`name`
VARCHAR
(
45
)
NULL
DEFAULT
NULL
COMMENT
'监控参数标准名-英文'
,
`unit`
VARCHAR
(
45
)
NULL
DEFAULT
NULL
COMMENT
'单位'
,
`alias`
VARCHAR
(
45
)
NULL
DEFAULT
NULL
COMMENT
'其他名字'
,
PRIMARY
KEY
(
`id`
))
ENGINE
=
InnoDB
DEFAULT
CHARACTER
SET
=
latin1
;
SET
SQL_MODE
=@
OLD_SQL_MODE
;
SET
FOREIGN_KEY_CHECKS
=@
OLD_FOREIGN_KEY_CHECKS
;
SET
UNIQUE_CHECKS
=@
OLD_UNIQUE_CHECKS
;
ils-service/src/main/resources/translation/en.json
0 → 100644
浏览文件 @
e5ddec04
{
"DATABASE_ERROR"
:
"database error"
,
"DB_INSERT_ERROR"
:
"database insert error"
,
"DB_UPDATE_ERROR"
:
"database update error"
,
"DB_DELETE_ERROR"
:
"database delete error"
,
"INTERNAL_SERVER_ERROR"
:
"server failure"
}
\ No newline at end of file
ils-service/src/main/resources/translation/zh.json
0 → 100644
浏览文件 @
e5ddec04
{
"DATABASE_ERROR"
:
"数据库失败"
,
"DB_INSERT_ERROR"
:
"数据库添加操作失败"
,
"DB_UPDATE_ERROR"
:
"数据库修改操作失败"
,
"DB_DELETE_ERROR"
:
"数据库删除操作失败"
,
"INTERNAL_SERVER_ERROR"
:
"服务器故障"
}
\ No newline at end of file
pom.xml
0 → 100644
浏览文件 @
e5ddec04
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.ilabservice
</groupId>
<artifactId>
ils-spring-example
</artifactId>
<packaging>
pom
</packaging>
<version>
1.0-SNAPSHOT
</version>
<properties>
<java.version>
1.8
</java.version>
</properties>
<parent>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-parent
</artifactId>
<version>
1.5.3.RELEASE
</version>
</parent>
<modules>
<module>
ils-common
</module>
<module>
ils-service
</module>
</modules>
</project>
\ No newline at end of file
src/main/java/com/ilabservice/example/ExampleApplication.java
0 → 100644
浏览文件 @
e5ddec04
package
com
.
ilabservice
.
example
;
public
class
ExampleApplication
{
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论