HoRain云小助手:个人主页
个人专栏: 《Linux 系列教程》《c语言教程》
⛺️生活的理想,就是为了理想的生活!
前些天发现了一个超棒的服务器购买网站,性价比超高,大内存超划算!忍不住分享一下给大家。点击跳转到网站。
专栏名称 |
专栏介绍 |
《C语言》 |
本专栏主要撰写C干货内容和编程技巧,让大家从底层了解C,把更多的知识由抽象到简单通俗易懂。 |
《网络协议》 |
本专栏主要是注重从底层来给大家一步步剖析网络协议的奥秘,一起解密网络协议在运行中协议的基本运行机制! |
《docker容器精解篇》 |
全面深入解析 docker 容器,从基础到进阶,涵盖原理、操作、实践案例,助您精通 docker。 |
《linux系列》 |
本专栏主要撰写Linux干货内容,从基础到进阶,知识由抽象到简单通俗易懂,帮你从新手小白到扫地僧。 |
《python 系列》 |
本专栏着重撰写Python相关的干货内容与编程技巧,助力大家从底层去认识Python,将更多复杂的知识由抽象转化为简单易懂的内容。 |
《试题库》 |
本专栏主要是发布一些考试和练习题库(涵盖软考、HCIE、HRCE、CCNA等) |
目录
⛳️ 推荐
专栏介绍
一、快速定位问题
1. 查看详细错误日志
2. 启用DEBUG日志
二、常见原因及解决方案
1. 代码逻辑错误
2. 依赖组件未正确注入
3. 数据库或外部服务问题
4. 请求/响应序列化问题
5. 安全配置冲突
三、高级调试技巧
1. 断点调试
2. 网络请求抓包
3. 隔离测试环境
四、代码示例:修复常见问题
场景:控制器返回未处理的 Mono.error
修复方案:添加全局异常处理
测试代码验证:
五、总结步骤
[185c31bb]
找到完整的异常堆栈信息。185c31bb
,定位到具体的错误位置。Caused by:
后的根本原因(如 NullPointerException
、DatabaseConnectionException
等)。application.properties
或 application-test.properties
中增加: logging.level.root=DEBUG
logging.level.org.springframework.web=DEBUG
logging.level.your.package=TRACE
@RestController
)中未处理的异常。null
或未校验数据合法性。@ExceptionHandler
或全局异常处理(@ControllerAdvice
)。webTestClient.get().uri("/api/endpoint")
.exchange()
.expectStatus().isOk()
.expectBody(String.class).consumeWith(System.out::println);
@MockBean
但未定义Mock行为。@SpringBootTest
或 @WebFluxTest
注解。@MockBean
private UserService userService;
@BeforeEach
void setup() {
Mockito.when(userService.findById(any())).thenReturn(Mono.just(new User()));
}
@Test
void testEndpoint() {
// 初始化测试数据
databaseClient.insert().into(User.class).using(new User("test")).then().block();
// 执行测试
webTestClient.get().uri("/api/users/test")
.exchange()
.expectStatus().isOk();
}
@RequestBody
)或路径变量类型不匹配。webTestClient.post().uri("/api/create")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue("{ \"name\": \"test\" }")
.exchange()
.expectStatus().isOk();
@TestPropertySource(properties = {
"spring.security.enabled=false"
})
webTestClient.get().uri("/api/secured")
.headers(headers -> headers.setBearerAuth("mock-token"))
.exchange()
.expectStatus().isOk();
Wireshark
或 tcpdump
抓取测试请求的原始数据。./mvnw test -Dtest=YourTestClass#testSpecificMethod
Mono.error
@GetMapping("/error-prone")
public Mono errorProneEndpoint() {
return Mono.error(new RuntimeException("Simulated error"));
}
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(RuntimeException.class)
public ResponseEntity handleRuntimeException(RuntimeException ex) {
return ResponseEntity.status(500).body("Error: " + ex.getMessage());
}
}
webTestClient.get().uri("/error-prone")
.exchange()
.expectStatus().is5xxServerError()
.expectBody().jsonPath("$.error").isEqualTo("Simulated error");
[185c31bb]
的详细错误。
❤️❤️❤️本人水平有限,如有纰漏,欢迎各位大佬评论批评指正!
如果觉得这篇文对你有帮助的话,也请给个点赞、收藏下吧,非常感谢!
Stay Hungry Stay Foolish 道阻且长,行则将至,让我们一起加油吧!