SpringBootで掲示板を作りたいです。
タイトルとコンテンツだけをPostしたいですが、実装中に以下のエラーメッセージが発生しました。
↓は画面側のエラーです。Controllerまでいかないです。
html
1Whitelabel Error Page 2This application has no explicit mapping for /error, so you are seeing this as a fallback. 3 4Thu Oct 03 21:52:42 JST 2019 5There was an unexpected error (type=Bad Request, status=400). 6Failed to convert value of type 'java.lang.String' to required type 'com.cms.video.yoocms.Entity.Contents'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'this is contents'; nested exception is java.lang.NumberFormatException: For input string: "thisiscontents" 7org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'com.cms.video.yoocms.Entity.Contents'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'this is contents'; nested exception is java.lang.NumberFormatException: For input string: "thisiscontents" 8 at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:79) 9 at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:53) 10 at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:693) 11 at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttributeFromRequestValue(ServletModelAttributeMethodProcessor.java:141) 12 at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:77) 13 at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:139) 14 at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:127) 15 at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167) 16 at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134) 17 at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) 18 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892) 19 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) 20 21...省略... 22 23Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'this is contents'; nested exception is java.lang.NumberFormatException: For input string: "thisiscontents" 24 at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47) 25 at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191) 26 at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174) 27 at org.springframework.data.repository.support.DomainClassConverter$ToEntityConverter.convert(DomainClassConverter.java:168) 28 at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41) 29 at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191) 30 at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:129) 31 at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:73) 32 ... 94 more 33Caused by: java.lang.NumberFormatException: For input string: "thisiscontents" 34 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 35 at java.lang.Long.parseLong(Long.java:589) 36 at java.lang.Long.valueOf(Long.java:803) 37 at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:214) 38 at org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber.convert(StringToNumberConverterFactory.java:62) 39 at org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber.convert(StringToNumberConverterFactory.java:49) 40 at org.springframework.core.convert.support.GenericConversionService$ConverterFactoryAdapter.convert(GenericConversionService.java:436) 41 at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41) 42 ... 101 more
該当のソースコード
java
1package com.cms.video.yoocms.Entity; 2 3import lombok.Getter; 4import lombok.Setter; 5import org.hibernate.annotations.CreationTimestamp; 6import org.hibernate.annotations.UpdateTimestamp; 7 8import javax.persistence.*; 9import java.sql.Timestamp; 10 11@Entity 12@Getter 13@Setter 14public class Contents { 15 16 @Id 17 @GeneratedValue 18 private Long id; 19 20 @Column 21 private String title; 22 23 @Column 24 private String contents; 25 26 @Column 27 @CreationTimestamp 28 private Timestamp createTime; 29 30 @Column 31 @UpdateTimestamp 32 private Timestamp updateTime; 33} 34
java
1package com.cms.video.yoocms.Controller; 2 3import com.cms.video.yoocms.Entity.Contents; 4import com.cms.video.yoocms.Service.ContentsService; 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Controller; 7import org.springframework.ui.Model; 8import org.springframework.validation.BindingResult; 9import org.springframework.web.bind.annotation.*; 10 11import javax.validation.Valid; 12 13 14@Controller 15@RequestMapping(value = "/contents/**") 16public class ContentsController { 17 18 @Autowired 19 ContentsService contentsService; 20 21 @GetMapping("/create") 22 public String createContents(Model model){ 23 model.addAttribute("contents",new Contents()); 24 return "contents_create"; 25 } 26 27 @PostMapping("/create") 28 public String createContents(@Valid @ModelAttribute Contents contents, BindingResult bindingResult){ 29 //contentsService.createContents(contents); 30 return "redirect:/contents/list"; 31 } 32 33 @GetMapping("/list") 34 public String list(){ 35 return "contents_list"; 36 } 37 38} 39
html
1<!doctype html> 2<html lang="en" xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" 6 content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> 7 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 8 <title>Document</title> 9</head> 10<body> 11<form action="#" th:action="@{/contents/create}" method="post"> 12 13 <input type="text" name="title" value="this is title"> 14 <input type="text" name="contents" value="this is contents"> 15 <input type="submit" value="OK"> 16</form> 17</body> 18</html>
試したこと
contentsフィールドのタイプがStringなのに、submitするとLongタイプに変換しているようです。
画面側で文字列を入力したら上記エラーが発生しますが、
数字を入力したらPostでcreateContentsメソッドに届きます。
@RequestParam String title, @RequestParam String contents で受けると正常ですが、@ModelAttributeで上記エラーになる原因が知りたいです。
補足情報(FW/ツールのバージョンなど)
springboot 2.1.8.RELEASE
java se 8
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。