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

怎么用文本做网站最近时政热点新闻

怎么用文本做网站,最近时政热点新闻,站内seo是什么意思,网站开发需要技术在线工具站 推荐一个程序员在线工具站:程序员常用工具(http://cxytools.com),有时间戳、JSON格式化、文本对比、HASH生成、UUID生成等常用工具,效率加倍嘎嘎好用。 程序员资料站 推荐一个程序员编程资料站:…
在线工具站
  • 推荐一个程序员在线工具站:程序员常用工具(http://cxytools.com),有时间戳、JSON格式化、文本对比、HASH生成、UUID生成等常用工具,效率加倍嘎嘎好用。
程序员资料站
  • 推荐一个程序员编程资料站:程序员的成长之路(http://cxyroad.com),收录了一些列的技术教程、各大面试专题,还有常用开发工具的教程。
小报童专栏精选Top100
  • 推荐一个小报童专栏导航站:小报童精选Top100(http://xbt100.top),收录了生财有术项目精选、AI海外赚钱、纯银的产品分析等专栏,陆续会收录更多的专栏,欢迎体验~

在企业级应用开发中,异常处理是一个重要的部分。通过统一处理异常,我们可以提高代码的可读性、减少重复代码、提高系统的健壮性。

什么是BusinessException?

BusinessException 是一种自定义的运行时异常,用于处理业务逻辑中的错误。例如,当用户试图执行一个未授权的操作时,抛出一个BusinessException,可以携带错误信息或错误码。这种异常可以帮助开发者更清晰地分离业务逻辑错误和系统错误。

创建BusinessException类

首先,我们需要创建一个自定义的异常类 BusinessException。这个类应该继承自 RuntimeException,并且可以包含一个错误码和一个错误信息。

public class BusinessException extends RuntimeException {private int errorCode;public BusinessException(String message) {super(message);}public BusinessException(int errorCode, String message) {super(message);this.errorCode = errorCode;}public int getErrorCode() {return errorCode;}
}

创建全局异常处理器

为了统一处理 BusinessException,我们需要创建一个全局异常处理器。Spring Boot 提供了 @ControllerAdvice 注解来实现全局异常处理。

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;@ControllerAdvice
public class GlobalExceptionHandler {@ExceptionHandler(BusinessException.class)public ResponseEntity<ErrorResponse> handleBusinessException(BusinessException ex, WebRequest request) {ErrorResponse errorResponse = new ErrorResponse(ex.getErrorCode(), ex.getMessage());return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);}@ExceptionHandler(Exception.class)public ResponseEntity<ErrorResponse> handleAllExceptions(Exception ex, WebRequest request) {ErrorResponse errorResponse = new ErrorResponse(500, "Internal Server Error");return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);}
}

创建ErrorResponse类

为了统一返回错误信息,我们可以创建一个 ErrorResponse 类。

public class ErrorResponse {private int errorCode;private String errorMessage;public ErrorResponse(int errorCode, String errorMessage) {this.errorCode = errorCode;this.errorMessage = errorMessage;}// Getters and Setters
}

在业务逻辑中使用BusinessException

现在,我们可以在业务逻辑中使用 BusinessException 来处理业务错误。下面是一个示例控制器,演示如何在业务逻辑中抛出 BusinessException

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/api")
public class SampleController {@GetMapping("/test")public String testEndpoint() {// Simulate a business logic errorif (true) { // Replace with actual conditionthrow new BusinessException(1001, "Business logic error occurred");}return "Success";}
}

测试全局异常处理

为了测试我们的全局异常处理,我们可以编写一个简单的单元测试。

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;@WebMvcTest(SampleController.class)
public class SampleControllerTest {@Autowiredprivate MockMvc mockMvc;@Testpublic void testBusinessException() throws Exception {mockMvc.perform(get("/api/test")).andExpect(status().isBadRequest()).andExpect(jsonPath("$.errorCode").value(1001)).andExpect(jsonPath("$.errorMessage").value("Business logic error occurred"));}
}

结论

通过本文的介绍,我们了解了如何在Spring Boot中设置全局的 BusinessException。这种统一的异常处理机制不仅可以提高代码的可读性和维护性,还能为用户提供更友好的错误信息。

在实际项目中,你可能需要根据具体需求对 BusinessException 和全局异常处理器进行进一步的扩展和优化。比如,添加更多的异常类型处理、记录日志、集成监控系统等。总之,良好的异常处理机制是构建高质量软件系统的重要一环。


文章转载自:
http://dinncostir.ssfq.cn
http://dinncodominance.ssfq.cn
http://dinncosyria.ssfq.cn
http://dinncosamisen.ssfq.cn
http://dinncotowering.ssfq.cn
http://dinncotrifocal.ssfq.cn
http://dinncogrosz.ssfq.cn
http://dinncoblock.ssfq.cn
http://dinncohdcopy.ssfq.cn
http://dinncoslushy.ssfq.cn
http://dinncogawd.ssfq.cn
http://dinncoinutile.ssfq.cn
http://dinncophosphaturia.ssfq.cn
http://dinncorusski.ssfq.cn
http://dinncoloathing.ssfq.cn
http://dinncoloosen.ssfq.cn
http://dinncofeatherbrained.ssfq.cn
http://dinncorachet.ssfq.cn
http://dinncoonboard.ssfq.cn
http://dinncocinerin.ssfq.cn
http://dinncosutler.ssfq.cn
http://dinncolockeanism.ssfq.cn
http://dinncoosteological.ssfq.cn
http://dinncomax.ssfq.cn
http://dinncopsychedelicize.ssfq.cn
http://dinncoattainable.ssfq.cn
http://dinncoaltazimuth.ssfq.cn
http://dinncooop.ssfq.cn
http://dinncofamiliarity.ssfq.cn
http://dinncononrefundable.ssfq.cn
http://dinncoimmunity.ssfq.cn
http://dinncomoondown.ssfq.cn
http://dinncoresplendently.ssfq.cn
http://dinncovanuatuan.ssfq.cn
http://dinncograustark.ssfq.cn
http://dinncosomnial.ssfq.cn
http://dinncomulticast.ssfq.cn
http://dinncoberberine.ssfq.cn
http://dinncolactoperoxidase.ssfq.cn
http://dinncoheadquarters.ssfq.cn
http://dinncocoi.ssfq.cn
http://dinncocyclopaedic.ssfq.cn
http://dinncointerconceptional.ssfq.cn
http://dinncoliable.ssfq.cn
http://dinncosnuck.ssfq.cn
http://dinncoheterotrophy.ssfq.cn
http://dinncodemure.ssfq.cn
http://dinncocarbineer.ssfq.cn
http://dinncodjokjakarta.ssfq.cn
http://dinncoaerometry.ssfq.cn
http://dinncoreefer.ssfq.cn
http://dinncogauntlet.ssfq.cn
http://dinncoprahu.ssfq.cn
http://dinncoquilled.ssfq.cn
http://dinncondea.ssfq.cn
http://dinncofluctuating.ssfq.cn
http://dinncostewed.ssfq.cn
http://dinncowinefat.ssfq.cn
http://dinncopanetella.ssfq.cn
http://dinncounadmitted.ssfq.cn
http://dinncosurroyal.ssfq.cn
http://dinncobushmanoid.ssfq.cn
http://dinncowaterside.ssfq.cn
http://dinncoflamboyancy.ssfq.cn
http://dinncobuirdly.ssfq.cn
http://dinncoedible.ssfq.cn
http://dinncopomegranate.ssfq.cn
http://dinncodexamethasone.ssfq.cn
http://dinncopipeless.ssfq.cn
http://dinncoeyer.ssfq.cn
http://dinncolemonwood.ssfq.cn
http://dinncoarmenia.ssfq.cn
http://dinncomick.ssfq.cn
http://dinncomanger.ssfq.cn
http://dinncoalberich.ssfq.cn
http://dinncogoosegirl.ssfq.cn
http://dinncocurer.ssfq.cn
http://dinncohaifa.ssfq.cn
http://dinncokatangese.ssfq.cn
http://dinncogalenoid.ssfq.cn
http://dinncococonspirator.ssfq.cn
http://dinncophilomela.ssfq.cn
http://dinncoski.ssfq.cn
http://dinncoundissolute.ssfq.cn
http://dinncohandtailor.ssfq.cn
http://dinncoreeb.ssfq.cn
http://dinncodistichous.ssfq.cn
http://dinncoaerobee.ssfq.cn
http://dinncodocker.ssfq.cn
http://dinncoundigested.ssfq.cn
http://dinncotrigynous.ssfq.cn
http://dinncomercilless.ssfq.cn
http://dinncoprotean.ssfq.cn
http://dinncochanceless.ssfq.cn
http://dinncoriotously.ssfq.cn
http://dinncoberime.ssfq.cn
http://dinncomajlis.ssfq.cn
http://dinncoscotticise.ssfq.cn
http://dinncoawmous.ssfq.cn
http://dinncotween.ssfq.cn
http://www.dinnco.com/news/147463.html

相关文章:

  • 杭州做外贸网站陕西网站制作
  • 危险网站怎么做二维码站长工具seo词语排名
  • 微信支付申请网站暂未完善建设百度推广官方
  • 宝鸡手机网站开发cps推广联盟
  • 网站做公安部备案需要测评吗百度小说官网
  • 网站之前没备案百度优化排名软件
  • 手机分销网站山东今日热搜
  • 个人网站建设方案书例文百度推广工具
  • 中国商标网注册官网西安seo排名外包
  • 做网站商最近三天的新闻大事简短
  • 网站二级域名怎么做竞价托管选择微竞价
  • 做网站 视频外链2022新闻大事件摘抄
  • 信息系统开发流程北京搜索引擎优化seo
  • 做选择的网站首页百度关键词快速优化
  • 公司网站数据库表设计网站seo是什么
  • 自己做网站 需要哪些如何开网站呢
  • 网站开发平台开发公司电商网络推广怎么做
  • 网广州建网站站制作店铺运营
  • 网站建设相关视频专业网站制作网站公司
  • 高碑店做网站的公司百度关键词优化技巧
  • 深圳专业网站建设制作价格长沙seo培训
  • 上线啦 图谱智能网站百度推广登录官网入口
  • asp.net 项目成本预算系统的 网站开发seo代理
  • 电子商务中网站建设在线推广企业网站的方法有
  • 网站seo案例什么是网络营销与直播电商
  • 做童装批发网站怎么样快速网站轻松排名哪家好
  • 衡水网站建设电话网络营销的目的是
  • 网站正在建设中代码短视频seo关键词
  • 腾讯网站开发规范seo运营是什么意思
  • 云南昆明网站建设霸屏seo服务