ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
| この記事の要点 |
|
エラー画面
Spring Boot アプリにアクセスして 404 / 500 等のエラーが発生したとき、以下のような無味乾燥なページが表示されます:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat May 15 12:34:56 JST 2024
There was an unexpected error (type=Not Found, status=404).
原因
Spring Boot はデフォルトのフォールバックエラーページとして「Whitelabel Error Page」を表示します。これは:
- 独自の
/errorコントローラが定義されていない - テンプレートエンジンの
error.htmlも無い - 結果として Spring Boot 内蔵の素っ気ない HTML が表示される
本番では UX もブランディングも崩れるので独自エラーページに置き換えます。
対処1: error.html を配置(Thymeleaf)
もっとも簡単。src/main/resources/templates/error.html を作成するだけで Spring Boot が自動でマッピング:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="${status} + ' - ' + ${error}">エラー</title>
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<div class="error-container">
<h1 th:text="${status}">500</h1>
<h2 th:text="${error}">Internal Server Error</h2>
<p th:if="${message}" th:text="${message}"></p>
<div th:if="${trace}" class="trace">
<pre th:text="${trace}"></pre>
</div>
<a href="/">トップに戻る</a>
</div>
</body>
</html>
ステータスコードごとに別ファイルを置くと自動で振り分けられます:
src/main/resources/templates/error/
├── 404.html ← 404 専用
├── 500.html ← 500 専用
└── ...
src/main/resources/templates/
└── error.html ← その他すべて
対処2: ErrorController を実装
テンプレートを使わず、コントローラで JSON 等を返す:
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
@RestController
public class CustomErrorController implements ErrorController {
@RequestMapping("/error")
public Map<String, Object> handleError(HttpServletRequest request) {
Integer status = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
String message = (String) request.getAttribute(RequestDispatcher.ERROR_MESSAGE);
String path = (String) request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI);
Map<String, Object> response = new HashMap<>();
response.put("status", status);
response.put("error", message);
response.put("path", path);
response.put("timestamp", new Date());
return response;
}
}
対処3: @ControllerAdvice で例外を catch
例外型ごとに細かく分岐したい場合(推奨):
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(NotFoundException.class)
public ResponseEntity<ErrorResponse> handleNotFound(NotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new ErrorResponse("NOT_FOUND", ex.getMessage()));
}
@ExceptionHandler(AccessDeniedException.class)
public ResponseEntity<ErrorResponse> handleForbidden(AccessDeniedException ex) {
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.body(new ErrorResponse("FORBIDDEN", ex.getMessage()));
}
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleAll(Exception ex) {
log.error("Unexpected error", ex);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new ErrorResponse("INTERNAL", "サーバエラーが発生しました"));
}
public record ErrorResponse(String code, String message) {}
}
対処4: Whitelabel を無効化
独自のエラーハンドリングだけ使い、Spring の Whitelabel を完全に切る:
# application.properties
server.error.whitelabel.enabled=false
# エラー詳細表示の制御
server.error.include-stacktrace=on_param # never / always / on_param / on_trace_param
server.error.include-message=on_param # never / always / on_param
server.error.include-binding-errors=on_param
server.error.include-exception=false
# /error パスを変更
server.error.path=/api-error
本番 vs 開発でのエラー詳細制御
# application-dev.properties (開発)
server.error.include-stacktrace=always
server.error.include-message=always
server.error.include-exception=true
# application-prod.properties (本番)
server.error.include-stacktrace=never # 機密漏洩防止
server.error.include-message=never
server.error.include-exception=false
404 だけを別ハンドラに
// 404 専用ハンドラ
@ControllerAdvice
public class NotFoundHandler {
@ExceptionHandler(NoHandlerFoundException.class)
public ModelAndView handle404(NoHandlerFoundException ex) {
ModelAndView mav = new ModelAndView("error/404");
mav.setStatus(HttpStatus.NOT_FOUND);
return mav;
}
}
// application.properties で 404 例外を投げるように
// spring.mvc.throw-exception-if-no-handler-found=true
// spring.web.resources.add-mappings=false
JSON / HTML を自動切替
同じ /error でも Accept ヘッダで HTML / JSON を切り替えるパターン:
@Controller
public class CustomErrorController implements ErrorController {
@RequestMapping(value = "/error", produces = MediaType.TEXT_HTML_VALUE)
public String html(HttpServletRequest request, Model model) {
// HTML テンプレートで返す
Integer status = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
model.addAttribute("status", status);
return "error";
}
@RequestMapping(value = "/error")
@ResponseBody
public Map<String, Object> json(HttpServletRequest request) {
// JSON で返す(API リクエスト用)
// ...
return Map.of("status", status, "error", "...");
}
}
関連
- HandlerInterceptor でエラー処理前にロギング・通知を挟める
- Spring Security の AccessDeniedHandler: 403 の独自処理
- RestControllerAdvice: API 専用の @ControllerAdvice
- ProblemDetail (RFC 9457): Spring 6 / Boot 3 で標準化されたエラーレスポンス形式
- Actuator: 健全性チェック / エラー統計の収集
ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
子ページ
子ページはありません
同階層のページ
- java.lang.IllegalStateException: CGLIB is required to process @Configuration classes
- Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator
- No mapping found for HTTP request with URI ... in DispatcherServlet with name ...
- An internal error occurred during: "Building UI model". com/google/common/base/Function
- No identifier specified for entity : ...
- org.hibernate.hql.internal.ast.QuerySyntaxException: table_name is not mapped
- No compiler is provided in this environment
- java.sql.SQLException: The server time zone value ' ... ' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone
- Caused by: java.lang.RuntimeException: Executing an update/delete query
- Not supported for DML operations
- Field ... required a bean of type ... that could not be found.
- Annotation-specified bean name 'xxxController' for bean class [com.xxx.controller.xxxController] conflicts with existing, non-compatible bean definition of same name and class [com.xxx.xxxController]
- Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.
- Exception in thread "main" java.lang.UnsupportedClassVersionError
人気ページ
- 1 Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法
- 2 tomcat の起動 / 停止ログと catalina.log・catalina.out の違い
- 3 JavaScript で base URL を取得する方法|window.location.origin
- 4 YouTube Data API v3 エラー一覧|403・400・404 の原因と対処
- 5 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 6 Spring Frameworkのアノテーション一覧
- 7 3Dグラフィックスとは|モデリング/レンダリング/主要ソフトウェア (Blender / Maya)
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- PHPでのファイルアップロード方法|multipart/form-dataと$_FILES 2026-08-07 17:15:57
- PHP $_SERVERとは|DOCUMENT_ROOT・PHP_SELFなど主な要素 2026-08-07 17:15:57
- Scratchのアカウントの作り方|メールアドレスとユーザー名の登録手順 2026-08-07 17:15:57
- djangoにおけるMVCアプリケーション実装例 2026-08-07 17:15:57
- Bluetoothとは|仕組み・BLEとClassicの違い・用途 2026-08-07 17:15:57
- Spring Bootのテスト入門|@SpringBootTestとJUnitでの単体・結合テスト 2026-08-07 17:15:57
- Java とは?言語仕様・JVM・主要フレームワーク一覧 2026-08-07 17:15:56
- インストール方法(Windows版) 2026-08-07 17:15:56
- APIキー取得方法 2026-08-07 17:15:56
- 【PHPフレームワーク】Laravelの使い方 2026-08-07 17:15:56
- Scratch 作品の公開方法 2026-08-07 17:15:56
- Google OAuth 2.0 認証の実装方法 2026-08-07 17:15:56
- データベースとは?RDB / NoSQL / SQL の基礎 2026-08-07 17:15:56
- インストール(eclipseプラグイン) 2026-08-07 17:15:56
- Spring Frameworkの使い方 2026-08-07 17:15:56
コメントを削除してもよろしいでしょうか?