Problem Details(RFC 7807)
標準化されたエラーレスポンスを返します
「Problem Details(RFC 7807)」はCoddyKit上の無料Spring Boot 4 Microservices & REST APIsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSpring Boot 4 Microservices & REST APIs学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
A Standard for API Errors
Every team inventing its own error JSON leads to chaos for API consumers. RFC 7807 defines Problem Details — a standard, machine-readable error format with a media type of application/problem+json.
The Problem Details Fields
The standard defines a small set of fields:
type— a URI identifying the problem kindtitle— short, human-readable summarystatus— the HTTP status codedetail— specifics for this occurrenceinstance— URI of the specific occurrence
Spring’s ProblemDetail
Spring Framework 6 / Boot 3 ship a built-in ProblemDetail class that models RFC 7807 directly. You construct it with a status and customize the standard fields.
ProblemDetail pd = ProblemDetail.forStatusAndDetail(
HttpStatus.NOT_FOUND, "Order 42 was not found");
pd.setTitle("Order Not Found");
pd.setType(URI.create("https://errors.example.com/order-not-found"));Returning ProblemDetail
Return a ProblemDetail from a controller or exception handler. Spring serializes it as application/problem+json with the right status.
@ExceptionHandler(OrderNotFoundException.class)
public ProblemDetail handle(OrderNotFoundException ex) {
ProblemDetail pd = ProblemDetail.forStatusAndDetail(
HttpStatus.NOT_FOUND, ex.getMessage());
pd.setTitle("Order Not Found");
return pd;
}Enabling Built-in Problem Details
Spring Boot can convert its own framework exceptions into Problem Details automatically when you enable the property, giving consistent errors even for built-in failures.
spring:
mvc:
problemdetails:
enabled: trueExtending with Custom Properties
RFC 7807 permits extension members. Add domain-specific data — an error code, a correlation id, or field errors — via setProperty.
pd.setProperty("errorCode", "ORDER_NOT_FOUND");
pd.setProperty("correlationId", correlationId);
pd.setProperty("timestamp", Instant.now());ResponseEntityExceptionHandler
Extend ResponseEntityExceptionHandler in your @ControllerAdvice to inherit handlers for Spring’s standard exceptions, then override the hooks to shape them as Problem Details.
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
// override protected handle... methods to customize
}Problem Details for Validation
Override the validation hook to express field errors as a Problem Details body with an extension array, giving clients structured, standard validation feedback.
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex, HttpHeaders h,
HttpStatusCode status, WebRequest req) {
ProblemDetail pd = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST);
pd.setTitle("Validation Failed");
pd.setProperty("errors", ex.getBindingResult().getFieldErrors().stream()
.map(e -> Map.of("field", e.getField(), "message", e.getDefaultMessage()))
.toList());
return ResponseEntity.badRequest().body(pd);
}Why the type URI Matters
The type URI gives each problem a stable identity clients can branch on — more robust than parsing human text. Point it at documentation describing the error and how to recover.
Content Negotiation
Problem Details responses carry the application/problem+json content type. Clients can detect this media type to distinguish structured errors from normal success payloads.
HTTP/1.1 404 Not Found
Content-Type: application/problem+json
{ "type": "...", "title": "Order Not Found", "status": 404, "detail": "..." }Adopting the Standard
Using Problem Details makes your API errors predictable and interoperable:
- One shape across all endpoints and frameworks
- Machine-readable
typefor client logic - Extensible for domain-specific detail
Quick Check
Test your understanding of RFC 7807.
Recap
Problem Details standardize API errors.
- RFC 7807 defines
type,title,status,detail,instance - Spring’s
ProblemDetailmodels it; served asapplication/problem+json - Enable built-in conversion via
spring.mvc.problemdetails.enabled - Add extension members with
setProperty - Extend
ResponseEntityExceptionHandlerto shape framework errors
よくある質問
「Problem Details(RFC 7807)」レッスンは無料ですか?
はい。「Problem Details(RFC 7807)」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Boot 4 Microservices & REST APIsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。
「Problem Details(RFC 7807)」で何を学びますか?
標準化されたエラーレスポンスを返します ブラウザで直接実行するハンズオンコードでSpring Boot 4 Microservices & REST APIsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Spring Boot 4 Microservices & REST APIsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSpring Boot 4 Microservices & REST APIsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「Problem Details(RFC 7807)」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSpring Boot 4 Microservices & REST APIsレッスンでコードを書いて実行できますか?
はい。すべてのSpring Boot 4 Microservices & REST APIsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- @ValidによるBean Validation
- カスタムバリデーター
- @ControllerAdviceと@ExceptionHandler
- Problem Details(RFC 7807)