当前位置: 首页 > news >正文

深圳乐安居网站谁做的百度推广平台登录网址

深圳乐安居网站谁做的,百度推广平台登录网址,wordpress扒站教程,怎么做返利网站吗你好,我是田哥 在这微服务架构盛行的黄金时段,加上越来越多的前后端分离,导致后端API接口规范变得越来越重要了。 比如:统一返回参数形式、统一返回码、统一异常处理、集成swagger等。 目的主要是规范后端项目代码,以及…

你好,我是田哥

在这微服务架构盛行的黄金时段,加上越来越多的前后端分离,导致后端API接口规范变得越来越重要了。

比如:统一返回参数形式、统一返回码、统一异常处理、集成swagger等。

目的主要是规范后端项目代码,以及便于前端沟通联通以及问题的排查。

本文内容:

6708d887d7a7084b46cbe2e8fae9ee79.png

统一返回参数形式

目前主流的返回参数形式:

{"code": 200000,"message": "成功","data": {"id": 1,"userName": "tiange","password": "123456","phone": "18257160375","gender": 0,"status": 0,"createTime": "2024-05-17 20:24:40"}
}

code是接口返回编码,message是消息提示,data是具体返回数据内容。

返回码

返回码定义很重要,我们应该可以参考HTTP请求返回的状态码(下面是常见的HTTP状态码):

200 - 请求成功
301 - 资源(网页等)被永久转移到其它URL
404 - 请求的资源(网页等)不存在
500 - 内部服务器错误

这样前端开发人员在得到返回值后,根据状态码就可以知道,大概什么错误,再根据message相关的信息描述,可以快速定位。

由于我们业务系统中可能会又大量的code,所以,我们对此做一个改良。

/*** {@code @description:} 返回码** @author tianwc 公众号:Java后端技术全栈* 在线刷题 1200+java面试题和1000+篇技术文章:<a href="https://woaijava.cc/">博客地址</a>* {@code @date:} 2024-07-28 15:10* {@code @version:} 1.0*/
@Getter
public enum ResultCode implements Serializable {SUCCESS(200000, "成功"),FAIL(500000, "系统错误,请稍后重试!"),USER_NOT_EXIST(401000, "用户不存在"),USER_CANCELLED(401001, "用户已注销"),USER_ROLE_ERROR(401002, "用户角色不对"),NOT_FOUND(404000, "接口不存在"),PARAMETER_ERROR(404001, "参数有误"),PARAMETER_IS_NULL(404002, "参数为空");private final int code;private final String message;ResultCode(int code, String message) {this.code = code;this.message = message;}
}

对此,我们还可以进一步细分,比如402开头的是用户相关的 、403开头又是xxx的,.....

这样后期如果又什么问题,这样就能快速定位到具体模块中。

统一返回

我们可以专门写一个类来对返回数据进行包装。

/*** {@code @description:} 返回结果马甲** @author tianwc 公众号:Java后端技术全栈* 在线刷题 1200+java面试题和1000+篇技术文章:<a href="https://woaijava.cc/">博客地址</a>* {@code @date:} 2024-07-28 15:12* {@code @version:} 1.0*/
@Data
public class Result implements Serializable {private Integer code;private String message;private Object data;public Result(Integer code, String message, Object data) {this.code = code;this.message = message;this.data = data;}public static Result success() {return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), null);}public static Result success(Object data) {return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);}public static Result fail(ResultCode resultCode) {return new Result(resultCode.getCode(), resultCode.getMessage(), null);}public static Result fail(int code, String message) {return new Result(code, message, null);}
}

我们定义了常用的四种格式。

具体使用如下:

我们在Service层和实现层:

public interface UserInfoService extends IService<UserInfo> {Result findByCondition(UserInfoReqDto userInfoReqDto);
}
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {@Overridepublic Result findByCondition(UserInfoReqDto userInfoReqDto) {Wrapper<UserInfo> wrapper = Wrappers.<UserInfo>lambdaQuery().eq(UserInfo::getUserName, userInfoReqDto.getUserName()).eq(UserInfo::getPassword, userInfoReqDto.getPassword());return Result.success(this.baseMapper.selectList(wrapper));}
}

在controller层:我们会在controller层处理业务请求,并返回给前端 。

@RestController
@RequestMapping("/user/info")
public class UserInfoController {@Resourceprivate UserInfoService userInfoService; @GetMapping("/condition")public Result findByCondition(UserInfoReqDto userInfoReqDto) {return userInfoService.findByCondition(userInfoReqDto);}
}

执行:

GET http://localhost:8089/user/info/condition?userName=tiange&password=123456

返回:

{"code": 200000,"message": "成功","data": [{"id": 1,"userName": "tiange","password": "123456","phone": "18257160375","gender": 0,"status": 0,"createTime": "2024-05-17T20:24:40.000+00:00"}]
}

前端根据我们但会的code判断是否需要取data字段。

统一异常处理

统一异常处理我们分业务异常、系统异常以及参数异常:

业务异常

我们自定义一个业务异常:BusinessException

/*** @author tianwc  公众号:java后端技术全栈、面试专栏* @version 1.0.0* @date 2024-07-28 15:12* 在线刷题 1200+java面试题和1000+篇技术文章:<a href="https://woaijava.cc/">博客地址</a>* <p>* 自定义业务异常*/
@Getter
public class BusinessException extends RuntimeException {/*** http状态码*/private Integer code;private Object object;public BusinessException(String message, Integer code, Object object) {super(message);this.code = code;this.object = object;}public BusinessException(String message, Integer code) {super(message);this.code = code;}public BusinessException(ResultCode resultCode) {super(resultCode.getMessage());this.code = resultCode.getCode();this.object = resultCode.getMessage();}public BusinessException(ResultCode resultCode, String message) {this.code = resultCode.getCode();this.object = message;}public BusinessException(String message) {super(message);}}

异常处理:GlobalAdvice

@RestControllerAdvice
@Slf4j
public class GlobalAdvice {@ExceptionHandler(Exception.class)public Result doException(Exception e) {log.error("统一异常处理机制,触发异常 msg ", e);String message = null;int errorCode = ResultCode.FAIL.getCode();//自定义异常if (e instanceof BusinessException) {BusinessException exception = (BusinessException) e;message = exception.getMessage();errorCode = exception.getCode();} else if (e instanceof HttpRequestMethodNotSupportedException) {message = "不支持GET/POST方法";} else if (e instanceof NoHandlerFoundException) {message = "请求接口不存在";} else if (e instanceof MissingServletRequestParameterException) {errorCode = ResultCode.PARAMETER_IS_NULL.getCode();message = String.format("缺少必要参数[%s]", ((MissingServletRequestParameterException) e).getParameterName());} else if (e instanceof MethodArgumentNotValidException) {BindingResult result = ((MethodArgumentNotValidException) e).getBindingResult();FieldError error = result.getFieldError();errorCode = ResultCode.PARAMETER_IS_NULL.getCode();message = error == null ? ResultCode.PARAMETER_ERROR.getMessage() : error.getDefaultMessage();} else if (e instanceof BindException) {errorCode = ResultCode.PARAMETER_IS_NULL.getCode();message = ((BindException) e).getFieldError().getDefaultMessage();} else if (e instanceof IllegalArgumentException) {errorCode = ResultCode.PARAMETER_IS_NULL.getCode();message = e.getMessage();}return Result.fail(errorCode, message);}
}

使用:

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {@Overridepublic Result findByCondition(UserInfoReqDto userInfoReqDto) {if("admin".equals(userInfoReqDto.getUserName())){//对于某些业务问题抛出自定义异常throw new BusinessException(ResultCode.USER_ROLE_ERROR);}Wrapper<UserInfo> wrapper = Wrappers.<UserInfo>lambdaQuery().eq(UserInfo::getUserName, userInfoReqDto.getUserName()).eq(UserInfo::getPassword, userInfoReqDto.getPassword());return Result.success(this.baseMapper.selectList(wrapper));}
}
系统异常

假设系统异常:

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {@Overridepublic Result findByCondition(UserInfoReqDto userInfoReqDto) {if("123456".equals(userInfoReqDto.getPassword())){throw new RuntimeException("你的系统异常了");}Wrapper<UserInfo> wrapper = Wrappers.<UserInfo>lambdaQuery().eq(UserInfo::getUserName, userInfoReqDto.getUserName()).eq(UserInfo::getPassword, userInfoReqDto.getPassword());return Result.success(this.baseMapper.selectList(wrapper));}
}

执行:

GET http://localhost:8089/user/info/condition?userName=tian&password=123456

返回结果:

{"code": 500000,"message": "系统异常","data": null
}
参数校验

添加pom依赖

<!--参数验证-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId>
</dependency>

请求参数:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserInfoReqDto {@NotBlank(message = "姓名不能为空")private String userName;@NotBlank(message = "密码不能为空")private String password;
}

其他相关注解:

注解作用
@NotNull判断包装类是否为null
@NotBlank判断字符串是否为null或者是空串(去掉首尾空格)
@NotEmpty判断集合是否为空
@Length判断字符的长度(最大或者最小)
@Min判断数值最小值
@Max判断数值最大值
@Email判断邮箱是否合法

controller层添加注解@Validated

@RestController
@RequestMapping("/user/info")
public class UserInfoController {@Resourceprivate UserInfoService userInfoService; @GetMapping("/condition")public Result findByCondition(@Validated UserInfoReqDto userInfoReqDto) {return userInfoService.findByCondition(userInfoReqDto);}
}

最后在统一异常处理里处理。

执行:

GET http://localhost:8089/user/info/condition?userName=tian

返回:

{"code": 404002,"message": "密码不能为空","data": null
}

执行:

GET http://localhost:8089/user/info/condition?password=123456

返回:

{"code": 404002,"message": "姓名不能为空","data": null
}

集成mybatis-plus

添加依赖

<!--mybatis-plus 依赖-->
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>${mybatis-plus.version}</version>
</dependency>
<!--mysql依赖-->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope>
</dependency>

数据库信息配置:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/user-center?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=123456

mybatis-plus配置:

@Configuration
@MapperScan(basePackages = "com.tian.dao.mapper")
public class DataSourceConfig {@ConfigurationProperties(prefix = "spring.datasource")@Beanpublic DataSource dataSource() {return DataSourceBuilder.create().build();}@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();//分页插件interceptor.addInnerInterceptor(new PaginationInnerInterceptor());//注册乐观锁插件interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());return interceptor;}@Beanpublic SqlSessionFactory sqlSessionFactory(DataSource dataSource, MybatisPlusInterceptor interceptor) throws Exception {MybatisSqlSessionFactoryBean ssfb = new MybatisSqlSessionFactoryBean();ssfb.setDataSource(dataSource);ssfb.setPlugins(interceptor);//到哪里找xml文件ssfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));return ssfb.getObject();}
}

实体类:

@TableName(value = "user_info")
@Data
public class UserInfo {/*** 主键ID*/@TableId(value = "id")private Long id;/*** 姓名*/@TableField(value = "user_name")private String userName;/*** 密码*/@TableField(value = "password")private String password;/*** 手机号*/@TableField(value = "phone")private String phone;/*** 性别,0:女,1:男*/@TableField(value = "gender")private Integer gender;/*** 状态,0:正常,1:已注销*/@TableField(value = "status")private Integer status;/*** 注册时间*/@TableField(value = "create_time")private Date createTime;@TableField(exist = false)private static final long serialVersionUID = 1L;
}

mapper:

public interface  UserInfoMapper extends BaseMapper<UserInfo> {
}

service部分代码参照前面的代码来。

执行

GET http://localhost:8089/user/info/condition?userName=tiange&password=123456

返回

{"code": 200000,"message": "成功","data": [{"id": 1,"userName": "tiange","password": "123456","phone": "18257160375","gender": 0,"status": 0,"createTime": "2024-05-17T20:24:40.000+00:00"}]
}

到这里我们的项目就成功把mybatis-plus集成进来。

swagger

作为前后端分离项目,在团队开发中,一个好的 API 文档不但可以减少大量的沟通成本,还可以帮助一位新人快速上手业务。传统的做法是由开发人员创建一份 RESTful API文档来记录所有的接口细节,并在程序员之间代代相传。这种做法存在以下几个问题:

1)API 接口众多,细节复杂,需要考虑不同的HTTP请求类型、HTTP头部信息、HTTP请求内容等,想要高质量的完成这份文档需要耗费大量的精力;

2)难以维护。随着需求的变更和项目的优化、推进,接口的细节在不断地演变,接口描述文档也需要同步修订,可是文档和代码处于两个不同的媒介,除非有严格的管理机制,否则很容易出现文档、接口不一致的情况;

Swagger2 的出现就是为了从根本上解决上述问题。它作为一个规范和完整的框架,可以用于生成、描述、调用和可视化 RESTful 风格的 Web 服务:

  • 接口文档在线自动生成,文档随接口变动实时更新,节省维护成本;

  • 支持在线接口测试,不依赖第三方工具;

Swagger2 是一个规范和完整的框架,用于生成、描述、调用和可视化Restful风格的web服务,现在我们使用spring boot 整合它。作用:

  • 接口的文档在线自动生成;

  • 功能测试;

常用注解


注解描述
@Api将类标记为 Swagger 资源。
@ApiImplicitParam表示 API 操作中的单个参数。
@ApiImplicitParams允许多个 ApiImplicitParam 对象列表的包装器。
@ApiModel提供有关 Swagger 模型的其他信息。
@ApiModelProperty添加和操作模型属性的数据。
@ApiOperation描述针对特定路径的操作或通常是 HTTP 方法。
@ApiParam为操作参数添加额外的元数据。
@ApiResponse描述操作的可能响应。
@ApiResponses允许多个 ApiResponse 对象列表的包装器。
@Authorization声明要在资源或操作上使用的授权方案。
@AuthorizationScope描述 OAuth2 授权范围。
swagger配置
@Configuration   //加入到容器里面
@EnableSwagger2 //开启Swagger
public class SwaggerConfig {@Beanpublic Docket docket() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.tian.controller")).build();}private ApiInfo apiInfo(){Contact contact = new Contact("web项目demo", "https://www.woaijava.cc/", "251965157@qq.com");return new ApiInfo("web项目demo的API文档","练手所用","v1.0","https://www.woaijava.cc/",contact,"Apache 2.0","http://www.apache.org/licenses/LICENSE-2.0",new ArrayList());}}

我们就可以在对应业务代码中标注上swagger:

@RestController
@RequestMapping("/user/info")
@Api(value = "用户信息接口",tags = "用户信息")
public class UserInfoController {@Resourceprivate UserInfoService userInfoService;@GetMapping("/{id}")@ApiOperation(value = "根据id查询用户信息", notes = "根据id查询用户信息",produces = "application/json",consumes = "application/json")@ApiImplicitParams({@ApiImplicitParam(name="id",value="用户id",required = true,dataType = "Integer")})public Result findById(@PathVariable("id") Integer id) {return Result.success(userInfoService.getById(id));}@GetMapping("/condition")@ApiOperation(value = "根据条件查询用户信息")public Result findByCondition(@Validated UserInfoReqDto userInfoReqDto) {return userInfoService.findByCondition(userInfoReqDto);}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value="用户信息查询条件")
public class UserInfoReqDto {@NotBlank(message = "姓名不能为空")@ApiModelProperty(value="姓名")private String userName;@NotBlank(message = "密码不能为空")@ApiModelProperty(value="密码")private String password;
}
效果

启动项目,访问:

http://localhost:8089/swagger-ui.html

460cc1a6aeb1268ecc68ea8f7515e25f.png


4e12687736a041ab678797bbe6459fcd.png


91f9b0baff32d98fac446d562459b6a3.png


也到这里,我们就基本形成了一个完整的demo级后端项目。

代码已上传到知识星球:

8913ac8e8c550b2e232fc5f26d127890.png

e4dd00849b46eb47b48f1f9554c0430d.png

其他推荐

2024年最新面试总结,请查收!

充电桩项目如何部署?

应届生不会写简历?手把手教你怎么写简历

背八股文,不妨尝试这招!


文章转载自:
http://dilute.tgnr.cn
http://ichthyotic.tgnr.cn
http://ceraceous.tgnr.cn
http://norepinephrine.tgnr.cn
http://maltase.tgnr.cn
http://lrl.tgnr.cn
http://rheidity.tgnr.cn
http://sensitivity.tgnr.cn
http://motuca.tgnr.cn
http://washroom.tgnr.cn
http://underdrainage.tgnr.cn
http://thoughtway.tgnr.cn
http://charging.tgnr.cn
http://nodosity.tgnr.cn
http://karyostenosis.tgnr.cn
http://astrologian.tgnr.cn
http://metaprogram.tgnr.cn
http://ductwork.tgnr.cn
http://lucarne.tgnr.cn
http://busty.tgnr.cn
http://vtr.tgnr.cn
http://goy.tgnr.cn
http://sleeveless.tgnr.cn
http://peke.tgnr.cn
http://eyewitnesser.tgnr.cn
http://buoyancy.tgnr.cn
http://hydrology.tgnr.cn
http://calvarian.tgnr.cn
http://gallican.tgnr.cn
http://irredentist.tgnr.cn
http://spence.tgnr.cn
http://crabstick.tgnr.cn
http://dragway.tgnr.cn
http://hasid.tgnr.cn
http://ultrasonogram.tgnr.cn
http://dipterocarp.tgnr.cn
http://pigmy.tgnr.cn
http://clubbed.tgnr.cn
http://voucher.tgnr.cn
http://distaste.tgnr.cn
http://extemporisation.tgnr.cn
http://fain.tgnr.cn
http://heist.tgnr.cn
http://queenly.tgnr.cn
http://unshutter.tgnr.cn
http://stutteringly.tgnr.cn
http://stolen.tgnr.cn
http://pontil.tgnr.cn
http://feudally.tgnr.cn
http://amanuensis.tgnr.cn
http://uniparental.tgnr.cn
http://fender.tgnr.cn
http://pisiform.tgnr.cn
http://indistinct.tgnr.cn
http://moll.tgnr.cn
http://underlining.tgnr.cn
http://bookrest.tgnr.cn
http://taction.tgnr.cn
http://phanerogamous.tgnr.cn
http://oolith.tgnr.cn
http://phytoalexin.tgnr.cn
http://reflex.tgnr.cn
http://relievedly.tgnr.cn
http://hustle.tgnr.cn
http://samdwich.tgnr.cn
http://basketful.tgnr.cn
http://monosilane.tgnr.cn
http://technocracy.tgnr.cn
http://amatorial.tgnr.cn
http://thomasine.tgnr.cn
http://lawyerlike.tgnr.cn
http://disentangle.tgnr.cn
http://immortelle.tgnr.cn
http://ashcake.tgnr.cn
http://australian.tgnr.cn
http://flotage.tgnr.cn
http://rely.tgnr.cn
http://rummery.tgnr.cn
http://deregulate.tgnr.cn
http://wmc.tgnr.cn
http://illegalization.tgnr.cn
http://corked.tgnr.cn
http://papillon.tgnr.cn
http://conjee.tgnr.cn
http://sakel.tgnr.cn
http://inhibition.tgnr.cn
http://respectively.tgnr.cn
http://sked.tgnr.cn
http://fiducial.tgnr.cn
http://caecotomy.tgnr.cn
http://alienable.tgnr.cn
http://photophobia.tgnr.cn
http://squabble.tgnr.cn
http://syringe.tgnr.cn
http://articulation.tgnr.cn
http://bounce.tgnr.cn
http://nritta.tgnr.cn
http://brewage.tgnr.cn
http://millionth.tgnr.cn
http://nagging.tgnr.cn
http://www.15wanjia.com/news/86963.html

相关文章:

  • 建立一个门户网站培训网站推荐
  • 网站打不开别人能打开b站引流推广网站
  • 中国铁路总公司建设管理部网站百度关键字
  • 武汉做网站公司生产厂家百度首页入口
  • 济南网站建设与优化久久seo综合查询
  • 公安局网站开发商手机端怎么刷排名
  • 江苏连云港网站建设公司上海百度推广开户
  • iis配置网站访问权限培训平台
  • wordpress打开太慢责备seo关键词优化要多少钱
  • 苏州外贸网站建设推广服务怎么看app的下载网址
  • 西藏建设厅网站首页女教师遭网课入侵视频
  • 网站域名备案在哪里夫唯seo怎么样
  • 医院美容网站建设seo属于技术还是营销
  • 游戏公司做网站设计赚钱吗网站排名工具
  • wang域名 网站百度登录页
  • 东莞建设局门户网站兰州网络推广关键词优化
  • 邯郸做网站价格泽成seo网站排名
  • 做钓鱼网站视频教程网络工具
  • 网站建设职业兴趣要求2345浏览器导航页
  • 北京网站开发公司哪家好想建立自己的网站怎么建立
  • 网站制作费多少网站查询ip地址
  • 网站开发设计的地域分析总裁培训班
  • 苏州物联网app开发公司长沙网站优化价格
  • 营口工程建设信息网站朋友圈软文范例
  • 旅游系统网站开发的背景做网站流程
  • 成都网站建设及推广软文台
  • 怎样做营销型网站推广吸引人的营销标题
  • wordpress标题省略seo优化销售话术
  • 番禺做网站平台去除痘痘怎么有效果
  • 网站解析怎么做友链外链app