yudaocode/SpringBoot-Labs · error · ServiceException
1001002000
1001002000
Error message
用户不存在
What it means
ServiceException(USER_NOT_FOUND, code 1001002000, '用户不存在') thrown from GET /user/exception-02 in the WebFlux lab. It demonstrates that @ControllerAdvice + @ExceptionHandler works identically in Spring WebFlux for exceptions thrown synchronously from annotated controllers, converting the business exception into a CommonResult payload with code 1001002000.
Source
Thrown at lab-27/lab-27-webflux-02/src/main/java/cn/iocoder/springboot/lab27/springwebflux/controller/UserController.java:109
UserVO user = new UserVO().setId(id).setUsername("username:" + id);
// 返回
return CommonResult.success(user);
}
/**
* 测试抛出 NullPointerException 异常
*/
@GetMapping("/exception-01")
public UserVO exception01() {
throw new NullPointerException("没有粗面鱼丸");
}
/**
* 测试抛出 ServiceException 异常
*/
@GetMapping("/exception-02")
public UserVO exception02() {
throw new ServiceException(ServiceExceptionEnum.USER_NOT_FOUND);
}
// @PostMapping(value = "/add",
// // ↓ 增加 "application/xml"、"application/json" ,针对 Content-Type 请求头
// consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE},
// // ↓ 增加 "application/xml"、"application/json" ,针对 Accept 请求头
// produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}
// )
@PostMapping(value = "/add",
// ↓ 增加 "application/xml"、"application/json" ,针对 Content-Type 请求头
consumes = {MediaType.APPLICATION_XML_VALUE},
// ↓ 增加 "application/xml"、"application/json" ,针对 Accept 请求头
produces = {MediaType.APPLICATION_XML_VALUE}
)
// @PostMapping(value = "/add")
public Mono<UserVO> add(@RequestBody Mono<UserVO> user) {
return user;View on GitHub (pinned to 6c12efaed0)
Solutions
- Expect the CommonResult contract (code 1001002000) in clients; do not parse HTTP status alone.
- Confirm your @ControllerAdvice class is component-scanned by the WebFlux app (same base package as the Application class).
- When throwing ServiceException from reactive operators, make sure it is returned as Mono.error/Flux.error so the dispatcher sees it and the advice fires.
- Keep ServiceExceptionEnum in a shared module so MVC and WebFlux services expose the same codes.
Example fix
// before: business exception thrown inside a reactive lambda may be swallowed
userRepository.findById(id)
.doOnNext(u -> { if (u == null) throw new ServiceException(ServiceExceptionEnum.USER_NOT_FOUND); })
.map(this::toVO);
// after: propagate through the error channel
userRepository.findById(id)
.switchIfEmpty(Mono.error(new ServiceException(ServiceExceptionEnum.USER_NOT_FOUND)))
.map(this::toVO); Defensive patterns
Strategy: try-catch
Validate before calling
// For real lookups, switch on empty before the exception can be thrown:
return userRepository.findById(id)
.switchIfEmpty(Mono.error(new ServiceException(ServiceExceptionEnum.USER_NOT_FOUND))); Try / catch
// Server-side unified handling:
@ExceptionHandler(ServiceException.class)
CommonResult<?> serviceEx(ServiceException ex) {
return CommonResult.error(ex.getCode(), ex.getMessage());
} Prevention
- Always return errors via Mono.error/Flux.error in reactive chains so handlers see them.
- Share ServiceException(Enum) via a common module across MVC and WebFlux services.
- Test the error path with WebTestChain (WebTestClient) to lock the CommonResult shape.
When it happens
Trigger: Calling GET /user/exception-02 on the WebFlux app; the method throws unconditionally. Note the surrounding demo also has commented-out /add mappings showing consumes/produces negotiation — only the exception path matters here.
Common situations: Verifying the unified error contract ({code, message, data}) in a reactive service before writing gateway/client code. Developers also hit this shape when a reactive service layer throws ServiceException from inside flatMap: the handler still catches it, but only if the error actually propagates out of the chain (beware swallowed errors in doOnNext without error consumers).
Related errors
AI-assisted analysis of yudaocode/SpringBoot-Labs@6c12efaed0 (2026-08-14).
Data as JSON: /api/errors/37ed702ed7eee986.
Report an issue: GitHub.