xkcoding/spring-boot-demo · info · JsonException
500
500
Error message
服务器出错啦
What it means
This is a demo/test endpoint (GET /json) that unconditionally throws a JsonException wrapping Status.UNKNOWN_ERROR (code 500, message '服务器出错啦'). It exists solely to demonstrate the global exception handler's JSON-response path. It will always fire when the endpoint is hit — it is not a runtime failure but a deliberate test throw.
Source
Thrown at demo-exception-handler/src/main/java/com/xkcoding/exception/handler/controller/TestController.java:26
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
/**
* <p>
* 测试Controller
* </p>
*
* @author yangkai.shen
* @date Created in 2018-10-02 20:49
*/
@Controller
public class TestController {
@GetMapping("/json")
@ResponseBody
public ApiResponse jsonException() {
throw new JsonException(Status.UNKNOWN_ERROR);
}
@GetMapping("/page")
public ModelAndView pageException() {
throw new PageException(Status.UNKNOWN_ERROR);
}
}
View on GitHub (pinned to 87a142f960)
Solutions
- This is expected behavior for the demo — no fix needed unless the endpoint is unwanted.
- If this appears in production logs, exclude or remove the TestController bean from production builds.
- Verify a @ControllerAdvice with an @ExceptionHandler(JsonException.class) is registered to produce the expected JSON response.
Defensive patterns
Strategy: try-catch
Try / catch
// In a @ControllerAdvice handler
@ExceptionHandler(JsonException.class)
@ResponseBody
public ApiResponse handleJsonException(JsonException e) {
log.warn("JsonException caught: code={}, msg={}", e.getCode(), e.getMessage());
return ApiResponse.ofMessage(e.getCode(), e.getMessage());
} Prevention
- Register a global @ControllerAdvice with an @ExceptionHandler for JsonException.
- Exclude TestController from production profiles using @Profile("dev").
- Do not expose demo endpoints in production deployments.
When it happens
Trigger: Sending a GET request to /json. The method body contains no conditional logic — it always throws. The JsonException is caught by a @ControllerAdvice handler (if configured) and returned as a JSON ApiResponse with code 500.
Common situations: Hitting the demo endpoint during local testing; integration tests that exercise the exception-handler flow; accidental exposure of the test controller in a non-demo deployment.
AI-assisted analysis of xkcoding/spring-boot-demo@87a142f960 (2026-08-14).
Data as JSON: /api/errors/eb50d5a636804e79.
Report an issue: GitHub.