-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[AI-8th]Add ProblemDetail (RFC 7807) support for standardized SOFABoot REST error responses #1418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.alipay.sofa.boot.autoconfigure.problem; | ||
|
|
||
| import org.springframework.http.ProblemDetail; | ||
| import org.springframework.util.StringUtils; | ||
|
|
||
| import javax.ws.rs.core.Context; | ||
| import javax.ws.rs.core.HttpHeaders; | ||
| import javax.ws.rs.core.MediaType; | ||
| import javax.ws.rs.core.Response; | ||
| import javax.ws.rs.core.UriInfo; | ||
| import javax.ws.rs.ext.ExceptionMapper; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
|
|
||
| /** | ||
| * Base JAX-RS exception mapper for SOFA problem detail responses. | ||
| * | ||
| * @author OpenAI | ||
| */ | ||
| public abstract class AbstractSofaProblemDetailExceptionMapper<T extends Throwable> | ||
| implements | ||
| ExceptionMapper<T> { | ||
|
|
||
| private final SofaProblemDetailFactory factory; | ||
|
|
||
| @Context | ||
| private UriInfo uriInfo; | ||
|
|
||
| @Context | ||
| private HttpHeaders headers; | ||
|
|
||
| protected AbstractSofaProblemDetailExceptionMapper(SofaProblemDetailFactory factory) { | ||
| this.factory = factory; | ||
| } | ||
|
|
||
| @Override | ||
| public Response toResponse(T exception) { | ||
| ProblemDetail problemDetail = factory.create(exception, resolveInstancePath(), | ||
| resolveLocale()); | ||
| return Response.status(problemDetail.getStatus()) | ||
| .type(MediaType.valueOf("application/problem+json")) | ||
| .entity(factory.render(problemDetail)).build(); | ||
| } | ||
|
|
||
| private String resolveInstancePath() { | ||
| if (uriInfo == null || uriInfo.getRequestUri() == null) { | ||
| return null; | ||
| } | ||
| String path = uriInfo.getRequestUri().getPath(); | ||
| if (!StringUtils.hasText(path)) { | ||
| return null; | ||
| } | ||
| return path.startsWith("/") ? path : "/" + path; | ||
| } | ||
|
|
||
| private Locale resolveLocale() { | ||
| if (headers == null) { | ||
| return Locale.getDefault(); | ||
| } | ||
| List<Locale> acceptableLanguages = headers.getAcceptableLanguages(); | ||
| return acceptableLanguages.isEmpty() ? Locale.getDefault() : acceptableLanguages.get(0); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,36 @@ | ||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||||||||||||||||||||||||
| * contributor license agreements. See the NOTICE file distributed with | ||||||||||||||||||||||||
| * this work for additional information regarding copyright ownership. | ||||||||||||||||||||||||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||||||||||||||||||||||||
| * (the "License"); you may not use this file except in compliance with | ||||||||||||||||||||||||
| * the License. You may obtain a copy of the License at | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||
| * See the License for the specific language governing permissions and | ||||||||||||||||||||||||
| * limitations under the License. | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| package com.alipay.sofa.boot.autoconfigure.problem; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import javax.ws.rs.ext.Provider; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * JAX-RS mapper for {@link SofaBootRpcRuntimeException}. | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * @author OpenAI | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| @Provider | ||||||||||||||||||||||||
|
Comment on lines
+21
to
+28
|
||||||||||||||||||||||||
| import javax.ws.rs.ext.Provider; | |
| /** | |
| * JAX-RS mapper for {@link SofaBootRpcRuntimeException}. | |
| * | |
| * @author OpenAI | |
| */ | |
| @Provider | |
| /** | |
| * JAX-RS mapper for {@link SofaBootRpcRuntimeException}. | |
| */ |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,97 @@ | ||||||||||||
| /* | ||||||||||||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||||||||||||
| * contributor license agreements. See the NOTICE file distributed with | ||||||||||||
| * this work for additional information regarding copyright ownership. | ||||||||||||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||||||||||||
| * (the "License"); you may not use this file except in compliance with | ||||||||||||
| * the License. You may obtain a copy of the License at | ||||||||||||
| * | ||||||||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||
| * | ||||||||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||
| * See the License for the specific language governing permissions and | ||||||||||||
| * limitations under the License. | ||||||||||||
| */ | ||||||||||||
| package com.alipay.sofa.boot.autoconfigure.problem; | ||||||||||||
|
|
||||||||||||
| import com.alipay.sofa.boot.autoconfigure.rpc.SofaRpcAutoConfiguration; | ||||||||||||
| import com.alipay.sofa.rpc.config.JAXRSProviderManager; | ||||||||||||
| import org.springframework.beans.factory.ObjectProvider; | ||||||||||||
| import org.springframework.boot.autoconfigure.AutoConfiguration; | ||||||||||||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||||||||||||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||||||||||||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||||||||||||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; | ||||||||||||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||||||||||||
| import org.springframework.context.MessageSource; | ||||||||||||
| import org.springframework.context.annotation.Bean; | ||||||||||||
| import org.springframework.context.annotation.Configuration; | ||||||||||||
| import org.springframework.core.env.Environment; | ||||||||||||
| import org.springframework.http.ProblemDetail; | ||||||||||||
|
|
||||||||||||
| import javax.ws.rs.ext.ExceptionMapper; | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Auto-configuration for SOFABoot problem detail support. | ||||||||||||
| * | ||||||||||||
| * @author OpenAI | ||||||||||||
|
Comment on lines
+38
to
+39
|
||||||||||||
| * | |
| * @author OpenAI |
Copilot
AI
Apr 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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" }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New classes in this module typically use a real contributor name/email in Javadoc
@authortags. UsingOpenAIhere is inconsistent with existing conventions; please replace with the actual contributor (or remove the tag).