@yawashita14 (やま)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

Request method 'POST' not supported リクエストメソッド「POST」はサポートされていません

Request method 'POST' not supported リクエストメソッド「POST」はサポートされていません

スプリング解体新書(6章)を使って勉強をしているのですが、ユーザー登録画面の入力内容をログ出力したいのですがRequest method 'POST' not supportedというエラーが出てしまいます。

発生している問題・エラー

![スクリーンショット 2022-06-19 13.21.25.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2712741/7d3a286e-daf0-e8a3-4a8d-5bb7aad75ef5.png)

Request method 'POST' not supported

該当するソースコード

1. UserApplicationService.java
package com.example.demo.application.service;


import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

@Service
public class UserApplicationService {
	
	@Autowired
	private MessageSource messageSource;

    /** 性別のMapを生成する */
    public Map<String, Integer> getGenderMap(Locale locale) {
        Map<String, Integer> genderMap = new LinkedHashMap<>();
        String male = messageSource.getMessage("male", null, locale);
        String female = messageSource.getMessage("female", null, locale);
        genderMap.put(male, 1);
        genderMap.put(female, 2);
        return genderMap;
    }
}

2. LoginController.java
package com.example.demo.contoroller.java;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class LoginController {

    /** ログイン画面を表示 */
    @GetMapping("/login")
    public String getLogin() {
        return "login/login";
    }
}

3.SignupController.java
package com.example.demo.contoroller.java;

import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.demo.application.service.UserApplicationService;
import com.example.demo.form.SignupForm;

import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("/user")
@Slf4j
public class SignupController {

    @Autowired
    private UserApplicationService userApplicationService;

    /** ユーザー登録画面を表示 */
    @GetMapping("/signup")
    public String getSignup(Model model, Locale locale,
            @ModelAttribute SignupForm form) {
        // 性別を取得
        Map<String, Integer> genderMap = userApplicationService.getGenderMap(locale);
        model.addAttribute("genderMap", genderMap);

        // ユーザー登録画面に遷移
        return "user/signup";
    }

    /** ユーザー登録処理 */
    @PostMapping("/signup")
    public String postSignup(@ModelAttribute SignupForm form) {

        log.info(form.toString());

        // ログイン画面にリダイレクト
        return "redirect:/login";
    }
}
4. SignupForm.java
package com.example.demo.form;

import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;

import lombok.Data;

@Data
public class SignupForm {

    private String userId;

    private String password;

    private String userName;

    @DateTimeFormat(pattern = "yyyy/MM/dd")
    private Date birthday;

    private Integer age;

    private Integer gender;
}
5. signup.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8"></meta>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <!-- CSS読込 -->
  <link rel="stylesheet" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}">
  <link rel="stylesheet" th:href="@{/css/user/signup.css}">
  <!-- JS読込 -->
  <script th:src="@{/webjars/jquery/jquery.min.js}" defer></script>
  <script th:src="@{/webjars/bootstrap/js/bootstrap.min.js}" defer></script>
  <title th:text="#{user.signup.title}"></title>
</head>
<body class="bg-light">
  <form id="signup-form" method="post" action="/user/signup" class="form-signup" th:object="${signupForm}">
    <h1 class="text-center" th:text="#{user.signup.title}"></h1>
    <!-- ユーザーID -->
    <div class="form-group">
      <label for="userId" th:text="#{userId}"></label>
      <input type="text" class="form-control" th:field="*{userId}"/>
    </div>
    <!-- パスワード -->
    <div class="form-group">
      <label for="password" th:text="#{password}"></label>
      <input type="text" class="form-control" th:field="*{password}"/>
    </div>
    <!-- ユーザー名 -->
    <div class="form-group">
      <label for="userName" th:text="#{userName}"></label>
      <input type="text" class="form-control" th:field="*{userName}"/>
    </div>
    <!-- 誕生日 -->
    <div class="form-group">
      <label for="birthday" th:text="#{birthday}"></label>
      <input type="text" class="form-control" placeholder="yyyy/MM/dd" th:field="*{birthday}"/>
    </div>
    <!-- 年齢 -->
    <div class="form-group">
      <label for="age" th:text="#{age}"></label>
      <input type="text" class="form-control" th:field="*{age}"/>
    </div>
    <!-- 性別 -->
    <div class="form-group">
      <div th:each="item : ${genderMap}" class="form-check-inline">
        <input type="radio" class="form-check-input" th:value="${item.value}" th:field="*{gender}"/>
        <label class="form-check-label" th:text="${item.key}"></label>
      </div>
    </div>
    <!-- 登録ボタン -->
    <input type="submit" th:value="#{user.signup.btn}" class="btn btn-primary w-100 mt-3" />
  </form>
</body>
</html>

自分で試したこと

サンプル資料と見比べてもコードで間違ってるところが見当たりません。
色々検索して試してみましたが変わらずエラーが出てしまいます。
何が悪いのか教えて頂きたいです。

0 likes

No Answers yet.

Your answer might help someone💌