前提
「Spring 解体新書」という書籍で学習しています。
メッセージを解決できない例外でエラーが出ています。
ソースコードに問題はなさそうです。
getMessageでmessages.propertiesの値を取れるはずですがどこに問題があるかが不明です。
実現したいこと
ユーザー登録画面を表示できるようにしたいです。
発生している問題・エラーメッセージ
There was an unexpected error (type=Internal Server Error, status=500). No message found under code 'male' for locale 'ja'. org.springframework.context.NoSuchMessageException: No message found under code 'male' for locale 'ja'. at org.springframework.context.support.DelegatingMessageSource.getMessage(DelegatingMessageSource.java:76) at com.example.application.service.UserApplicationService.getGenderMap(UserApplicationService.java:20) at com.example.controller.SignupController.getSignup(SignupController.java:44) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141)
該当のソースコード
java
1package com.example.application.service; 2 3import java.util.LinkedHashMap; 4import java.util.Locale; 5import java.util.Map; 6 7import org.springframework.beans.factory.annotation.Autowired; 8import org.springframework.context.MessageSource; 9import org.springframework.stereotype.Service; 10 11@Service 12public class UserApplicationService { 13 14 @Autowired 15 private MessageSource messageSource; 16 17// 性別のMAPを生成する 18 public Map<String,Integer>getGenderMap(Locale locale){ 19 Map<String,Integer>genderMap = new LinkedHashMap<>(); 20 String male = messageSource.getMessage("male",null,locale); 21 String female = messageSource.getMessage("female", null,locale); 22 genderMap.put(male, 1); 23 genderMap.put(female, 2); 24 return genderMap; 25 } 26} 27
java
1package com.example.controller; 2 3import java.util.Locale; 4import java.util.Map; 5 6import org.modelmapper.ModelMapper; 7import org.springframework.beans.factory.annotation.Autowired; 8import org.springframework.stereotype.Controller; 9import org.springframework.ui.Model; 10import org.springframework.validation.BindingResult; 11import org.springframework.validation.annotation.Validated; 12import org.springframework.web.bind.annotation.GetMapping; 13import org.springframework.web.bind.annotation.ModelAttribute; 14import org.springframework.web.bind.annotation.PostMapping; 15import org.springframework.web.bind.annotation.RequestMapping; 16 17import com.example.application.service.UserApplicationService; 18import com.example.domain.user.model.MUser; 19import com.example.domain.user.service.UserService; 20import com.example.form.GroupOrder; 21import com.example.form.SignupForm; 22 23import lombok.extern.slf4j.Slf4j; 24 25@Controller 26@RequestMapping("/user") 27@Slf4j 28public class SignupController { 29 30 @Autowired 31 private UserApplicationService userApplicationService; 32 33 @Autowired 34 private UserService userService; 35 36 @Autowired 37 private ModelMapper modelMapper; 38 39// ユーザー登録画面を表示 40 @GetMapping("/signup") 41 public String getSignup(Model model, Locale locale, @ModelAttribute SignupForm form) { 42 43 //性別を取得 44 Map<String,Integer> genderMap = userApplicationService.getGenderMap(locale); 45 model.addAttribute("genderMap", genderMap); 46 47 //ユーザー登録画面に遷移 48 return "user/signup"; 49 } 50 51 //ユーザー登録処理 52 @PostMapping("/signup") 53 public String postSignup(Model model, Locale locale, @ModelAttribute @Validated(GroupOrder.class) SignupForm form, BindingResult bindingResult) { 54 55 //入力チェック結果 56 if(bindingResult.hasErrors()) { 57 //NG:ユーザー登録画面に戻ります。 58 return getSignup(model, locale, form); 59 } 60 61 log.info(form.toString()); 62 63 //formをMUserクラスに変更 64 MUser user = modelMapper.map(form, MUser.class); 65 66 //ユーザー登録 67 userService.signup(user); 68 69 //ログイン画面にリダイレクト 70 return "redirect:/login"; 71 } 72}
java
1package com.example.form; 2 3import java.util.Date; 4 5import javax.validation.constraints.Email; 6import javax.validation.constraints.Max; 7import javax.validation.constraints.Min; 8import javax.validation.constraints.NotBlank; 9import javax.validation.constraints.NotNull; 10import javax.validation.constraints.Pattern; 11 12import org.hibernate.validator.constraints.Length; 13import org.springframework.format.annotation.DateTimeFormat; 14 15import lombok.Data; 16 17@Data 18public class SignupForm{ 19 20 @NotBlank(groups = ValidGroup1.class) 21 @Email(groups = ValidGroup2.class) 22 private String userId; 23 24 @NotBlank(groups = ValidGroup1.class) 25 @Length(min = 4, max = 100, groups = ValidGroup2.class) 26 @Pattern(regexp = "^[a-zA-Z0-9]+$", groups = ValidGroup2.class) 27 private String password; 28 29 @NotBlank(groups = ValidGroup1.class) 30 private String userName; 31 32 @DateTimeFormat(pattern = "yyyy/MM/dd") 33 @NotNull(groups = ValidGroup1.class) 34 private Date birthday; 35 36 @Min(value = 20, groups = ValidGroup2.class) 37 @Max(value = 100, groups = ValidGroup2.class) 38 private Integer age; 39 40 @NotNull(groups = ValidGroup1.class) 41 private Integer gender; 42}
messages.properties
試したこと
下記の確認
- ソースコード差分
- getMessageメソッドの使用方法
- propertiesファイルのファイル名
補足情報(FW/ツールのバージョンなど)
Spring Boot 2.4.1