自定义类中使用Server等类

直接使用要出错的,叫做对非共享成员的引用,因为你自己定义的类并没有包含Server这样的类啊。所以:

Server.MapPath-----------System.Web.HttpContext.Current.Server.MapPath

类推。

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot提供了自定义异常处理的功能,可以根据不同的异常型返回不同的响应结果。下面是一个示例代码,演示了如何在Spring Boot中自定义异常处理: 1. 创建一个自定义异常,继承自Exception,并添加需要展示的信息等。例如,创建一个名为CustomException的: ```java public class CustomException extends Exception { private String message; public CustomException(String message) { this.message = message; } public String getMessage() { return message; } } ``` 2. 创建一个全局异常处理,用于处理所有的异常情况。例如,创建一个名为GlobalExceptionHandler的: ```java @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(CustomException.class) public ResponseEntity<String> handleCustomException(CustomException ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.getMessage()); } @ExceptionHandler(Exception.class) public ResponseEntity<String> handleException(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Internal Server Error"); } } ``` 在上述代码中,使用@ControllerAdvice注解标记全局异常处理,@ExceptionHandler注解用于指定处理特定异常的方法。handleCustomException方法用于处理CustomException异常,返回一个带有自定义异常信息的响应实体;handleException方法用于处理其他未处理的异常,返回一个带有"Internal Server Error"信息的响应实体。 3. 在控制器中抛出自定义异常。例如,在一个控制器方法中抛出CustomException异常: ```java @GetMapping("/custom-exception") public ResponseEntity<String> throwCustomException() throws CustomException { throw new CustomException("Custom Exception"); } ``` 在上述代码中,当访问"/custom-exception"路径时,将抛出CustomException异常。 通过以上步骤,你可以在Spring Boot中自定义异常处理,根据不同的异常型返回不同的响应结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值