[AI-8th]Add ProblemDetail (RFC 7807) support for standardized SOFABoot REST error responses#1418
[AI-8th]Add ProblemDetail (RFC 7807) support for standardized SOFABoot REST error responses#1418pmupkin wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Adds RFC 7807 ProblemDetail-based error rendering for SOFA RPC REST (Spring MVC + JAX-RS) and introduces Jakarta Bean Validation-based fail-fast validation for several SOFABoot @ConfigurationProperties.
Changes:
- Introduce
SofaProblemDetailAutoConfiguration+ MVC@RestControllerAdvice+ JAX-RSExceptionMappers backed by a sharedSofaProblemDetailFactory. - Add
sofa.boot.problem-detail.*configuration properties (type base URI, stack trace/service info toggles, etc.) and tests for MVC/JAX-RS behavior and i18n. - Add Bean Validation annotations + validators and related tests to fail fast on invalid SOFABoot configuration properties; add
spring-boot-starter-validationdependencies.
Reviewed changes
Copilot reviewed 27 out of 27 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
| sofa-boot-project/sofaboot-dependencies/pom.xml | Adds dependency management entry for spring-boot-starter-validation. |
| sofa-boot-project/sofa-boot-autoconfigure/pom.xml | Adds spring-boot-starter-validation dependency to enable property validation. |
| sofa-boot-project/sofa-boot-actuator-autoconfigure/pom.xml | Adds spring-boot-starter-validation dependency for actuator property validation. |
| sofa-boot-project/sofa-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports | Registers new SofaProblemDetailAutoConfiguration for auto-import. |
| sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/problem/SofaProblemDetailAutoConfiguration.java | Auto-configures ProblemDetail factory + MVC advice + JAX-RS mappers/provider registration. |
| sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/problem/SofaProblemDetailProperties.java | Adds sofa.boot.problem-detail.* properties (enabled, defaultType, typeBaseUri, etc.). |
| sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/problem/SofaProblemDetailFactory.java | Centralizes exception-to-ProblemDetail mapping, rendering, and i18n resolution. |
| sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/problem/SofaProblemDetailExceptionHandler.java | Spring MVC @RestControllerAdvice returning application/problem+json. |
| sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/problem/AbstractSofaProblemDetailExceptionMapper.java | Base JAX-RS mapper to build problem+json responses using the shared factory. |
| sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/problem/SofaRpcExceptionProblemDetailExceptionMapper.java | JAX-RS mapper for SofaRpcException. |
| sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/problem/SofaRpcRuntimeExceptionProblemDetailExceptionMapper.java | JAX-RS mapper for SofaRpcRuntimeException. |
| sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/problem/SofaBootRpcRuntimeExceptionProblemDetailExceptionMapper.java | JAX-RS mapper for SofaBootRpcRuntimeException. |
| sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/tracer/SofaTracerProperties.java | Adds Bean Validation constraints and @Validated to tracer properties. |
| sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/runtime/SofaRuntimeProperties.java | Adds @Validated and cross-field/assertion validation for executor sizing. |
| sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/isle/SofaModuleProperties.java | Adds @Validated and positive constraints for parallel refresh settings. |
| sofa-boot-project/sofa-boot-actuator-autoconfigure/src/main/java/com/alipay/sofa/boot/actuator/autoconfigure/health/HealthProperties.java | Adds @Validated and positive constraints for health timeout settings. |
| sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaBootRpcProperties.java | Enables @Validated and class-level constraint for RPC properties. |
| sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/ValidSofaBootRpcProperties.java | Introduces class-level constraint annotation for RPC properties. |
| sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaBootRpcPropertiesValidator.java | Implements the class-level validator for RPC property combinations/ranges. |
| sofa-boot-project/sofa-boot-autoconfigure/src/test/java/com/alipay/sofa/boot/autoconfigure/problem/SofaProblemDetailAutoConfigurationTests.java | Verifies auto-config backoff, JAX-RS provider registration, and stack trace toggle. |
| sofa-boot-project/sofa-boot-autoconfigure/src/test/java/com/alipay/sofa/boot/autoconfigure/problem/SofaProblemDetailMvcAutoConfigurationTests.java | Verifies MVC problem+json output, i18n via MessageSource, ordering, stack trace toggle. |
| sofa-boot-project/sofa-boot-autoconfigure/src/test/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaBootRpcPropertiesValidatorTests.java | Unit tests for RPC properties validator behavior and edge cases. |
| sofa-boot-project/sofa-boot-autoconfigure/src/test/java/com/alipay/sofa/boot/autoconfigure/tracer/SofaTracerAutoConfigurationTests.java | Adds fail-fast test for invalid tracer sampling percentage. |
| sofa-boot-project/sofa-boot-autoconfigure/src/test/java/com/alipay/sofa/boot/autoconfigure/runtime/SofaRuntimeAutoConfigurationTests.java | Adds fail-fast test for invalid async init executor sizing. |
| sofa-boot-project/sofa-boot-autoconfigure/src/test/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaRpcAutoConfigurationTests.java | Adds fail-fast tests for invalid RPC properties. |
| sofa-boot-project/sofa-boot-autoconfigure/src/test/java/com/alipay/sofa/boot/autoconfigure/isle/SofaModuleAutoConfigurationTests.java | Adds fail-fast test for invalid module parallel refresh factor. |
| sofa-boot-project/sofa-boot-actuator-autoconfigure/src/test/java/com/alipay/sofa/boot/actuator/autoconfigure/health/ReadinessAutoConfigurationTests.java | Adds fail-fast test for invalid actuator health timeout configuration. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| * | ||
| * @author OpenAI |
There was a problem hiding this comment.
New classes in this module typically use a real contributor name/email in Javadoc @author tags (e.g., SofaRpcAutoConfiguration). Using OpenAI here is inconsistent with existing conventions; please replace with the actual contributor (or remove the tag if authorship isn’t tracked).
| * | |
| * @author OpenAI |
| * | ||
| * @author OpenAI |
There was a problem hiding this comment.
New classes in this module typically use a real contributor name/email in Javadoc @author tags. Using OpenAI here is inconsistent with existing conventions; please replace with the actual contributor (or remove the tag).
| * | |
| * @author OpenAI |
| import javax.ws.rs.ext.Provider; | ||
|
|
||
| /** | ||
| * JAX-RS mapper for {@link SofaRpcException}. | ||
| * | ||
| * @author OpenAI | ||
| */ | ||
| @Provider |
There was a problem hiding this comment.
JAX-RS providers in this codebase appear to be registered explicitly via JAXRSProviderManager.registerCustomProviderInstance(...) (see RestFilterConfiguration). Keeping @Provider here is redundant and risks double-registration if a JAX-RS runtime also performs classpath scanning. Consider removing @Provider and relying on the explicit registration mechanism.
Also, @author OpenAI is inconsistent with existing conventions in this repository; please use the actual contributor name/email (or omit the tag).
| import javax.ws.rs.ext.Provider; | |
| /** | |
| * JAX-RS mapper for {@link SofaRpcException}. | |
| * | |
| * @author OpenAI | |
| */ | |
| @Provider | |
| /** | |
| * JAX-RS mapper for {@link SofaRpcException}. | |
| */ |
| * @author OpenAI | ||
| */ | ||
| @AutoConfiguration(after = SofaRpcAutoConfiguration.class) | ||
| @ConditionalOnClass(ProblemDetail.class) |
There was a problem hiding this comment.
SofaProblemDetailAutoConfiguration is only conditional on ProblemDetail, but it always creates SofaProblemDetailFactory, which has hard references to SOFA RPC exception classes. In apps that include sofa-boot-autoconfigure but do not have rpc-sofa-boot / SOFA RPC on the classpath, this can trigger NoClassDefFoundError when the factory class is loaded. Consider adding an additional @ConditionalOnClass (or splitting configs) that gates this auto-configuration on the presence of the relevant SOFA RPC exception types (e.g., SofaRpcException / SofaRpcRuntimeException / SofaBootRpcRuntimeException).
| @ConditionalOnClass(ProblemDetail.class) | |
| @ConditionalOnClass(name = { "org.springframework.http.ProblemDetail", | |
| "com.alipay.sofa.rpc.core.exception.SofaRpcException", | |
| "com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException", | |
| "com.alipay.sofa.boot.rpc.exception.SofaBootRpcRuntimeException" }) |
| * | ||
| * @author OpenAI |
There was a problem hiding this comment.
New classes in this module typically use a real contributor name/email in Javadoc @author tags (see other auto-configurations in this project). Using OpenAI here is inconsistent with existing conventions; please replace with the actual contributor (or remove the tag).
| * | |
| * @author OpenAI |
| * | ||
| * @author OpenAI |
There was a problem hiding this comment.
New classes in this module typically use a real contributor name/email in Javadoc @author tags (see other auto-configurations in this project). Using OpenAI here is inconsistent with existing conventions; please replace with the actual contributor (or remove the tag).
| * | |
| * @author OpenAI |
| * | ||
| * @author OpenAI |
There was a problem hiding this comment.
New classes in this module typically use a real contributor name/email in Javadoc @author tags. Using OpenAI here is inconsistent with existing conventions; please replace with the actual contributor (or remove the tag).
| * | |
| * @author OpenAI |
| import javax.ws.rs.ext.Provider; | ||
|
|
||
| /** | ||
| * JAX-RS mapper for {@link SofaRpcRuntimeException}. | ||
| * | ||
| * @author OpenAI | ||
| */ | ||
| @Provider |
There was a problem hiding this comment.
JAX-RS providers in this codebase appear to be registered explicitly via JAXRSProviderManager.registerCustomProviderInstance(...) (see RestFilterConfiguration). Keeping @Provider here is redundant and risks double-registration if a JAX-RS runtime also performs classpath scanning. Consider removing @Provider and relying on the explicit registration mechanism.
Also, @author OpenAI is inconsistent with existing conventions in this repository; please use the actual contributor name/email (or omit the tag).
| import javax.ws.rs.ext.Provider; | |
| /** | |
| * JAX-RS mapper for {@link SofaRpcRuntimeException}. | |
| * | |
| * @author OpenAI | |
| */ | |
| @Provider | |
| /** | |
| * JAX-RS mapper for {@link SofaRpcRuntimeException}. | |
| */ |
| import javax.ws.rs.ext.Provider; | ||
|
|
||
| /** | ||
| * JAX-RS mapper for {@link SofaBootRpcRuntimeException}. | ||
| * | ||
| * @author OpenAI | ||
| */ | ||
| @Provider |
There was a problem hiding this comment.
JAX-RS providers in this codebase appear to be registered explicitly via JAXRSProviderManager.registerCustomProviderInstance(...) (see RestFilterConfiguration). Keeping @Provider here is redundant and risks double-registration if a JAX-RS runtime also performs classpath scanning. Consider removing @Provider and relying on the explicit registration mechanism.
Also, @author OpenAI is inconsistent with existing conventions in this repository; please use the actual contributor name/email (or omit the tag).
| import javax.ws.rs.ext.Provider; | |
| /** | |
| * JAX-RS mapper for {@link SofaBootRpcRuntimeException}. | |
| * | |
| * @author OpenAI | |
| */ | |
| @Provider | |
| /** | |
| * JAX-RS mapper for {@link SofaBootRpcRuntimeException}. | |
| */ |
| } | ||
|
|
||
| private void appendCommonProperties(ProblemDetail problemDetail, Throwable throwable) { | ||
| problemDetail.setProperty("exception", throwable.getClass().getName()); |
There was a problem hiding this comment.
appendCommonProperties always adds the server-side exception class name into the RFC7807 response (exception). This can leak internal implementation details to clients even when include-stack-trace is disabled. Consider removing this field by default or gating it behind a dedicated opt-in property (similar to includeStackTrace).
| problemDetail.setProperty("exception", throwable.getClass().getName()); | |
| // Intentionally left blank to avoid leaking internal exception class names | |
| // in client-facing RFC7807 responses. |
This PR adds ProblemDetail support in sofa-boot-autoconfigure to standardize REST and SOFA RPC REST error responses based on RFC 7807.Related issue #1403
It includes:
Integrating with MessageSource so title and detail can be localized