ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
| この記事の要点 |
|---|
|
エラーの状況
WARN org.springframework.web.servlet.PageNotFound -
No mapping found for HTTP request with URI [/api/users/123]
in DispatcherServlet with name 'dispatcherServlet'
Spring MVC の DispatcherServlet がリクエストを受け取ったものの、そのパスに対応する Controller のメソッドが登録されていない場合に発生します。クライアントには 404 が返ります。
主な原因と対処
原因 1: @RequestMapping が付いていない
// ダメな例
@Controller
public class UserController {
public User getUser(Long id) { // ← @GetMapping 等の指定なし
return userService.findById(id);
}
}
// 修正
@Controller
public class UserController {
@GetMapping("/api/users/{id}") // ← パスを指定
@ResponseBody
public User getUser(@PathVariable Long id) {
return userService.findById(id);
}
}
原因 2: @ComponentScan の範囲外
Controller が Spring の自動検出パッケージ外にあると Bean として登録されません。
// メインクラスのパッケージが com.example
@SpringBootApplication
public class MyApp { ... }
// 同じ com.example.controllers ならOK
@Controller
public class UserController { ... }
// com.other.controllers だと検出されない!
// 修正: スキャン範囲を広げる
@SpringBootApplication(scanBasePackages = {"com.example", "com.other"})
public class MyApp { ... }
原因 3: @Controller / @RestController が付いていない
// ダメな例 - 普通の Bean
@Component
public class UserController {
@GetMapping("/api/users/{id}")
public User get(...) { ... }
}
// 修正
@RestController // または @Controller
public class UserController { ... }
原因 4: コンテキストパスの考慮漏れ
Spring Boot で server.servlet.context-path を設定している場合、URL の先頭にそのパスが付きます:
# application.properties
server.servlet.context-path=/api/v1
// Controller のパスが /users なら、実際の URL は
// http://localhost:8080/api/v1/users
// http://localhost:8080/users ← これは 404
原因 5: URL のタイポ
クライアント側のリクエスト URL とコントローラのパスが一致しているか確認:
@GetMapping("/api/users/{id}") // 末尾スラッシュなし
// クライアント
fetch("/api/users/123") // OK
fetch("/api/users/123/") // ← 末尾スラッシュ。Spring の設定次第で 404
# Spring 5.3+ デフォルトでは / の有無を区別する
# 統一したい場合
spring.mvc.pathmatch.matching-strategy=ant-path-matcher # 古い動作
原因 6: HTTP メソッドの不一致
// GET メソッドだけマッピング
@GetMapping("/users")
public List list() { ... }
// クライアントが POST で来ると 405 Method Not Allowed
// (404 ではないが似た問題)
// 複数メソッド対応
@RequestMapping(value = "/users", method = {RequestMethod.GET, RequestMethod.POST})
登録済みマッピングの確認
① 起動ログを見る
Spring Boot 起動時にマッピングがログに出力されます:
INFO ... RequestMappingHandlerMapping -
s.w.s.m.m.a.RequestMappingHandlerMapping: Mapped
"{[/api/users/{id}],methods=[GET]}" onto
public User com.example.UserController.getUser(java.lang.Long)
② Spring Boot Actuator
actuator を有効にすると /actuator/mappings で全マッピング一覧が見られます:
# application.properties
management.endpoints.web.exposure.include=mappings,health,info
# 起動後
$ curl http://localhost:8080/actuator/mappings | jq .
# レスポンス例
{
"contexts": {
"application": {
"mappings": {
"dispatcherServlets": {
"dispatcherServlet": [
{
"predicate": "{GET /api/users/{id}}",
"handler": "com.example.UserController#getUser(Long)",
...
}
]
}
}
}
}
}
静的リソースが 404 になる場合
このエラーは静的ファイル(画像・CSS・JS)でも発生します:
// 配置場所
// src/main/resources/static/css/app.css
// → http://localhost:8080/css/app.css でアクセス可能
// templates/ や webapp/ も Spring Boot は自動認識
// 認識しないなら次の確認
// 1. ファイルパスが間違っていないか
// 2. spring.mvc.static-path-pattern が変えられていないか
// 3. WebMvcConfigurer の addResourceHandlers で別パスにしていないか
デバッグ Tips
① ログレベルを DEBUG に
# application.properties
logging.level.org.springframework.web=DEBUG
logging.level.org.springframework.web.servlet.PageNotFound=DEBUG
② エラーハンドリング
デフォルトの 404 ページをカスタマイズ:
@ControllerAdvice
public class NotFoundHandler {
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity> handle(NoHandlerFoundException ex) {
return ResponseEntity.status(404).body(
Map.of("error", "Not Found", "path", ex.getRequestURL())
);
}
}
# application.properties で NoHandlerFoundException を例外に
spring.mvc.throw-exception-if-no-handler-found=true
spring.web.resources.add-mappings=false
関連記事
ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子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 ... hat could not be found.
- Annotation-specified bean name ' ... ' for bean class [ ... ] conflicts with existing, non-compatible bean definition of same name and class [...]
- 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 と SSR/Node.js 対応
- 4 YouTube Data API v3 エラー一覧|403/400/404 の主要原因と切り分け
- 5 Spring Frameworkのアノテーション一覧
- 6 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 7 3Dグラフィックスとは|モデリング/レンダリング/主要ソフトウェア (Blender / Maya)
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- Laravel キャッシュクリア完全ガイド(cache:clear / config:clear / 2026-05-18 07:42:07
- プロジェクトの作成と削除 2026-05-18 07:42:07
- インストール直後にNetbeansが反応しない 2026-05-18 07:42:07
- 動画やチャンネルの検索 2026-05-18 07:42:07
- APIキー取得方法 2026-05-18 07:42:07
- チャンネル情報の取得 2026-05-18 07:42:07
- API 入門 — Web API(REST / GraphQL / gRPC / 2026-05-18 07:42:07
- インストール(eclipseプラグイン) 2026-05-18 07:42:07
- Laravel「Dotenv values containing spaces must be surrounded 2026-05-18 07:42:07
- エラー一覧 2026-05-18 07:42:07
- curl: (51) SSL: certificate subject name '~' does not match 2026-05-18 07:42:07
- インストール方法(Windows版) 2026-05-18 07:42:07
- JSONから配列に変換 2026-05-18 07:42:07
- 処理を一定時間待つ 2026-05-18 07:42:07
- A non well formed numeric value encountered 2026-05-18 07:42:07
コメントを削除してもよろしいでしょうか?