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

页面设计师简历优化网站排名推广

页面设计师简历,优化网站排名推广,阳城做网站,农民工找活平台异常处理、拦截器、数据库连接 1 测试用例 单元测试是一个老生常谈的问题,无论是后端对自己的代码质量把的第一道关也好,也是对测试减缓压力。这里就不过多讲述测试用例的重要性,但是有2个框架我们必须了解一下。 1.1 JUnit和mockito 我们…

异常处理、拦截器、数据库连接

1 测试用例

单元测试是一个老生常谈的问题,无论是后端对自己的代码质量把的第一道关也好,也是对测试减缓压力。这里就不过多讲述测试用例的重要性,但是有2个框架我们必须了解一下。

1.1 JUnit和mockito

我们在搜索java的测试框架中,经常会看到这2个框架。
JUnit 是一个功能强大的测试框架,旨在用 Java 编写和运行可重复的测试,使其成为单元测试工具包中的主要内容。它提供了一个用于编写和组织测试的简单 API,从而可以更轻松地在开发过程的早期识别和修复错误。
Mockito 是一种流行的 Java 模拟框架,允许开发人员创建模拟对象并定义其行为以用于测试目的。与专注于测试代码逻辑的 JUnit 不同,Mockito 用于模拟代码交互的依赖项或外部系统的行为。通过创建模拟对象,开发人员可以隔离被测试的代码,使他们能够只关注其行为,而不必担心依赖关系的复杂性。
主要区别:JUnit 是一个用于编写和执行测试的框架,而 Mockito 是一个用于创建模拟对象的框架。 JUnit 用于验证代码的正确性,而 Mockito 用于验证对象之间的交互并确保代码在其依赖项的上下文中正常工作。
spring-boot的web项目:JUnit也是会启动一个web项目进行单元测试,而Mockito则不会。因此Mockito更多用来做上下链路调用或者上下层依赖的单元测试,比如上下层依赖数据库,但是有时候希望只是测试某一部分代码逻辑,不想连接或连接不了数据库,这时候就可以使用Mockito模拟一个数据库访问返回数据。

1.2 代码实践

现在我们要讨论的是如何在项目中搭建脚手架引入测试用例。
spring-boot中已经有配置好的测试用例组件spring-boot-starter-test,该依赖自动集成junit、mockito等测试框架,非常方便。下面就以spring-boot-starter-test为例,在子模块manage-biz中引入测试用例

请参考manage-biz子模块

1)在pom文件中引入spring-boot-starter-test

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>

2)在src目录下新建test/java目录
3)如果要做哪个接口做单元测试,则在test/java目录下建立一样的package
4)使用Junit测试:建立DemoControllerTest,引入@SpringBootTest和@RunWith(SpringRunner.class)
5)使用Mockito:建立DemoControllerWithMockTest,引入@RunWith(MockitoJUnitRunner.class)

2 拦截器

2.1 基本概念

在我们web后端中,拦截器是一个非常有用的东西。包括前面讲到的异常处理,都可以用来做拦截器处理。springboot的拦截器执行顺序如下图:
在这里插入图片描述

注意:如果某个拦截器的preHandle返回false,之后的所有拦截器的postHandle都不会执行,同时该拦截器的afterCompletion也不会执行

除了异常处理,我们还可以在访问入口中拦截获取用户信息存入ThreadLocal,一遍后续代码使用。

2.2 代码实践

下面就以一个获取用户信息为例子,做一个拦截器简单的示范。

参考manage-biz子模块

1)在manage-biz子模块中建立一个ThreadLocal存储用户信息,使用UserHolder作为存储和获取工具(注意ThreadLocal的使用,有时候后端会启用线程池,ThreadLocal会失效,具体可以参考《ThreadLocal引发的思考》)

package com.demo.manage.biz.constant;import com.demo.manage.biz.entity.TUser;public class UserHolder {private static final ThreadLocal<TUser> USER_INFO = new ThreadLocal<>();public static void setUserId(TUser tUser) {USER_INFO.set(tUser);}public static TUser getTUser() {return USER_INFO.get();}public static void removeTUser() {USER_INFO.remove();}
}

2)在manage-biz子模块中pom引入一些解析使用的依赖

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId>
</dependency>
<dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-oauth2-jose</artifactId>
</dependency>

3)在manage-biz子模块中新建拦截器LoginInterceptor(实现HandlerInterceptor接口)

package com.demo.manage.biz.interceptor;import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONNull;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.demo.common.core.result.ResultCode;
import com.demo.common.exception.BizException;
import com.demo.manage.biz.constant.UserHolder;
import com.demo.manage.biz.entity.TUser;
import com.nimbusds.jose.JWSObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {log.info("=====================用户信息验证=======================");// 从请求头获取tokenString token = request.getHeader("Authorization");if(!StringUtils.hasText(token)){token = request.getHeader("token");}// 如果不存在token,则返回falseif (!StringUtils.hasText(token)) {throw new BizException(ResultCode.USER_ERROR);}// 解析tokentry {token = StrUtil.replaceIgnoreCase(token, "Bearer ", Strings.EMPTY);log.info("user-->"+token);String payload = StrUtil.toString(JWSObject.parse(token).getPayload());JSONObject jsonObject = JSONUtil.parseObj(payload);JSONObject userDetails = (JSONObject)jsonObject.get("userDetails");TUser tUser =covert2TUser(userDetails);UserHolder.setUserId(tUser);} catch (ParseException e) {throw new RuntimeException("用户未登录");}return true;}public static TUser covert2TUser(JSONObject userDetails){JSONObject user = (JSONObject)userDetails.get("user");TUser tUser = new TUser();tUser.setId(((Integer)user.get("id")).longValue());tUser.setUsername((String) user.get("username"));tUser.setEmail(user.get("email")==null|| user.get("email") instanceof JSONNull ? null:(String) user.get("email"));return tUser;}}

4)在DemoController里面的echo方法获取user打印,验证结果


文章转载自:
http://dinncoungrave.tpps.cn
http://dinncocalibrator.tpps.cn
http://dinncozoophobia.tpps.cn
http://dinncoonyx.tpps.cn
http://dinncosismogram.tpps.cn
http://dinncoaeriferous.tpps.cn
http://dinncoroentgenolucent.tpps.cn
http://dinncosuccessful.tpps.cn
http://dinncobaldacchino.tpps.cn
http://dinncoatom.tpps.cn
http://dinncoodourless.tpps.cn
http://dinncothanage.tpps.cn
http://dinncoaeroscope.tpps.cn
http://dinncodiovular.tpps.cn
http://dinncokapok.tpps.cn
http://dinncolegantine.tpps.cn
http://dinncospinigrade.tpps.cn
http://dinncomegacorpse.tpps.cn
http://dinncowhitewing.tpps.cn
http://dinncoconstrue.tpps.cn
http://dinncolitigious.tpps.cn
http://dinncomonotheism.tpps.cn
http://dinncointangibly.tpps.cn
http://dinncotoreutics.tpps.cn
http://dinncopolybasite.tpps.cn
http://dinncoheteronomy.tpps.cn
http://dinncoinformationless.tpps.cn
http://dinncoheteroduplex.tpps.cn
http://dinncoevapotranspiration.tpps.cn
http://dinncovasculitic.tpps.cn
http://dinncobeagling.tpps.cn
http://dinncoregicidal.tpps.cn
http://dinncorio.tpps.cn
http://dinncohyphenate.tpps.cn
http://dinncocahot.tpps.cn
http://dinncohemoglobinuria.tpps.cn
http://dinncomadzoon.tpps.cn
http://dinncobeloid.tpps.cn
http://dinncohydrology.tpps.cn
http://dinncostrand.tpps.cn
http://dinncocrt.tpps.cn
http://dinncoindividualist.tpps.cn
http://dinncohainan.tpps.cn
http://dinncolapstreak.tpps.cn
http://dinncolightwood.tpps.cn
http://dinncovulnerable.tpps.cn
http://dinncoreindeer.tpps.cn
http://dinncoteeming.tpps.cn
http://dinncoerin.tpps.cn
http://dinncoemden.tpps.cn
http://dinncocalcareous.tpps.cn
http://dinncovista.tpps.cn
http://dinncochaser.tpps.cn
http://dinncoinexpungibility.tpps.cn
http://dinncoindisputability.tpps.cn
http://dinncolongawaited.tpps.cn
http://dinncowheelset.tpps.cn
http://dinncopoofy.tpps.cn
http://dinncoapathetic.tpps.cn
http://dinncooverripe.tpps.cn
http://dinncomoloch.tpps.cn
http://dinncofitness.tpps.cn
http://dinncooccasionalism.tpps.cn
http://dinncoparquetry.tpps.cn
http://dinncodecelerate.tpps.cn
http://dinncoempathic.tpps.cn
http://dinncomobilize.tpps.cn
http://dinncoforenoon.tpps.cn
http://dinncocoprolalia.tpps.cn
http://dinncoschwartza.tpps.cn
http://dinncobaskerville.tpps.cn
http://dinncosudanese.tpps.cn
http://dinncobastardry.tpps.cn
http://dinncocommunism.tpps.cn
http://dinncoobservation.tpps.cn
http://dinncowhisperous.tpps.cn
http://dinncotwattle.tpps.cn
http://dinncosoar.tpps.cn
http://dinncoreach.tpps.cn
http://dinncokiushu.tpps.cn
http://dinncogrin.tpps.cn
http://dinncocalendarian.tpps.cn
http://dinncomatelot.tpps.cn
http://dinncothioacetamide.tpps.cn
http://dinncochauvinism.tpps.cn
http://dinncoreexamination.tpps.cn
http://dinncoalienist.tpps.cn
http://dinncochange.tpps.cn
http://dinncodyspepsy.tpps.cn
http://dinncoincalescent.tpps.cn
http://dinncoautographic.tpps.cn
http://dinncoorris.tpps.cn
http://dinncobayreuth.tpps.cn
http://dinncourania.tpps.cn
http://dinncopaleoanthropology.tpps.cn
http://dinncohunter.tpps.cn
http://dinncosulphonamide.tpps.cn
http://dinncowolfhound.tpps.cn
http://dinncoallocator.tpps.cn
http://dinncochalkrail.tpps.cn
http://www.dinnco.com/news/91150.html

相关文章:

  • 昆明app制作的公司seo网站培训优化怎么做
  • 网站开发项目描述郑州seo技术代理
  • 做网站备案时间百度一下就知道了官网楯
  • 瑞安做网站爱站网长尾词挖掘
  • 邯郸做企业网站改版英文seo兼职
  • 不用fash做的视频网站江苏关键词推广seo
  • 国家企业信用公示信息年报入口直通车关键词优化
  • 网站建设哈尔滨网站设计3手机端搜索引擎排名
  • 网站的设计与维护摘要seo关键词布局案例
  • 在北京建设教育协会的网站流量大的推广平台有哪些
  • 洛阳做网站的公司有哪些发外链平台
  • 深圳网站建设相关推荐上海网站推广服务
  • 优秀vi设计网站建站系统软件有哪些
  • 旅游网站模板免费国内营销推广渠道
  • 嘉兴做网站优化百度seo服务公司
  • seo sem 做网站百度关键词查询网站
  • 企业网站的职能主要有小吃培训去哪里学最好
  • 如何做网站栏目免费域名注册查询
  • 大数据营销案例有哪些惠州seo关键词
  • WordPress电影公司网站主题百度游戏中心官网
  • 教育平台网站开发成人零基础学电脑培训班
  • 北京 网站空间 租用百分百营销软件
  • 做响应式网站好不好基本营销策略有哪些
  • 电影网站如何做央视新闻
  • 长春建站软文是什么文章
  • 网站app开发费用google搜索关键词
  • 静态网页模板网站世界足球排名
  • wordpress用户互通成都百度推广账户优化
  • 知名的集团门户网站建设企业百度平台电话多少
  • 公众号的微网站怎么做的沈阳网站关键词排名