-
Notifications
You must be signed in to change notification settings - Fork 31
javayh common starter 说明
杨海吉 edited this page Apr 14, 2020
·
4 revisions
javayh-common-starter是本项目的核心依赖
这里以Demo案例为编写条件
<?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>javayh-platform</artifactId>
<groupId>com.javayh</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion
<dependencies>
<dependency>
<groupId>com.javayh</groupId>
<artifactId>javayh-common-starter</artifactId>
</dependency>
</dependencies>
</project>
依赖引入后,即可计入开发阶段
这里与以往的SpringBoot项目有些差别,启动注解不在是SpringBootApplication而是@JavayhBootApplication, 注:由于本项目开启了国际化语言切换,所以您必须添加@EnableAutoInternationalization来开启国际化处理
案例如下:
@EnableAutoInternationalization
@JavayhBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class,args);
}
}
这里使用 @JavayhBootApplication ,会将服务自动注册到注册中心,还会SpringUtils
包含了全局异常,Feign服务异常处理
- 开启全局异常处理 在启动类添加@EnableAutoException
@EnableAutoException
@EnableAutoInternationalization
@JavayhBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class,args);
}
}
- 全局异常提供了相对的异常处理类:
1.BaseException 基础异常
2.ServiceException 服务处异常
3.DataAccessException 数据库异常
4.ControllerException 控制层异常
5.BusinessException 参数异常
- Service中使用自定义的异常处理为例:
@Service
public class DemoService {
public String getInstance(Integer num) {
if (num > 5){
throw new BaseException("服务次数超限");
}else if (num == 0){
throw new ServiceException("服务次数已用完");
}
return "欢迎使用!";
}
}
- 返回前端
{
"code": 0,
"msg": "服务次数超限",
"data": null
}
- 日志记录
这里采用的时log输出的方式,并未做入库处理,待完善
-
BusinessException 使用注意事项
需要配合 @RequestBody @Valid 使用
@PostMapping(value = "test")
public R test(@RequestBody @Valid User user){
return R.ok(demoService.getInstance(0));
}
@Data
public class User {
@NotBlank(message = "name is null")
private String name;
@NotBlank(message = "adder is null")
private String adder;
}
- 返回前端
{
"code": 0,
"msg": "name is null",
"data": null
}
如:
- IPUtils
- RequestUtils
- SpringUtils
- ResultData
- encrypt相关
- ......
......未完待续......
关注 Java有货领取更多资料
联系小编。微信:372787553,带您进群互相学习
左侧小编微信,右侧获取免费资料
- SpringCloud 自定义封装架构https://github.com/Dylan-haiji/javayh-platform
- Java 设计模式学习代码 https://github.com/Dylan-haiji/design-pattern
- SpringCloud学习代码: https://github.com/Dylan-haiji/javayh-cloud
- AlibabaCloud学习代码:https://github.com/Dylan-haiji/javayh-cloud-nacos
- SpringBoot+Mybatis 多数据源切换:https://github.com/Dylan-haiji/javayh-boot-data-soure
- Redis、Mongo、Rabbitmq、Kafka学习代码: https://github.com/Dylan-haiji/javayh-middleware
- SpringBoot+SpringSecurity实现自定义登录学习代码:https://github.com/Dylan-haiji/javayh-distribution