質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

POST

POSTはHTTPプロトコルのリクエストメソッドです。ファイルをアップロードしたときや入力フォームが送信されたときなど、クライアントがデータをサーバに送る際に利用されます。

String

Stringは、ゼロ以上の文字から連続してできた文字の集合を扱うデータ型です。基本的にテキストを表すために使われます。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Q&A

1回答

9684閲覧

Sprintbootで@ModelAttributeでモデルを受け取れません。StringタイプのフィールドがLongタイプに強制変換されます。

chenilyoo

総合スコア4

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

POST

POSTはHTTPプロトコルのリクエストメソッドです。ファイルをアップロードしたときや入力フォームが送信されたときなど、クライアントがデータをサーバに送る際に利用されます。

String

Stringは、ゼロ以上の文字から連続してできた文字の集合を扱うデータ型です。基本的にテキストを表すために使われます。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

0グッド

0クリップ

投稿2019/10/03 13:11

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

Longに変換……というよりも
Contentsクラスの引数の名前が『contents』なので
HTML側のFormの中身一式ではなくname="contents"の中身の文字列が単独で反応して結果落ちてしまっているのではないでしょうか?
Long変換が発生してるのは先頭にあるフィールド「id」がLongだからそこに入れ込もうとしているのかも

投稿2019/10/05 13:33

編集2019/10/05 13:39
q_sane_q

総合スコア610

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問