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

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

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

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

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

1回答

3978閲覧

Java Spring Boot エラーの解決

granddaifuku

総合スコア6

Java

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

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

1クリップ

投稿2019/07/25 13:57

前提・実現したいこと

Spring Boot を用いて ToDo アプリケーションを作成しています.
save を実行しようとすると以下のエラーが出てしまい, save ができない状態です.
今回 save をしようとした文字は a です.
どこが間違っているのかご指摘いただけるとありがたいです. よろしくお願いします.
また, YouTube の https://www.youtube.com/watch?v=qZs0885Mdm0 を参考にアプリケーションを作成しています.
文字数の関係でソースコードの一部を抜粋しています.

発生している問題・エラーメッセージ

This application has no explicit mapping for /error, so you are seeing this as a fallback. Thu Jul 25 22:41:29 JST 2019 There was an unexpected error (type=Bad Request, status=400). Failed to convert value of type 'java.lang.String' to required type 'com.example.ToDoList.model.ToDo'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'a'; nested exception is java.lang.NumberFormatException: For input string: "a" org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'com.example.ToDoList.model.ToDo'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'a'; nested exception is java.lang.NumberFormatException: For input string: "a"

該当のソースコード

Service Implements

java

1package com.example.ToDoList.service.impl; 2 3import java.util.Date; 4import java.util.List; 5import java.util.Optional; 6 7import org.springframework.beans.factory.annotation.Autowired; 8import org.springframework.stereotype.Service; 9import org.springframework.transaction.annotation.Transactional; 10 11import com.example.ToDoList.model.ToDo; 12import com.example.ToDoList.repository.ToDoRepository; 13import com.example.ToDoList.service.ToDoService; 14 15@Service 16@Transactional 17public class ToDoServiceImpl implements ToDoService{ 18 19 @Autowired private ToDoRepository toDoRepository; 20 21 @Override 22 public List<ToDo> getAllToDos() { 23 return (List<ToDo>) toDoRepository.findAll(); 24 } 25 26 @Override 27 public ToDo save(ToDo toDo) { 28 toDo.setCreatedDate(new Date()); 29 return toDoRepository.save(toDo); 30 } 31 32 @Override 33 public ToDo findById(Long id) { 34 Optional<ToDo> toDo = toDoRepository.findById(id); 35 if (toDo.isPresent()) { 36 return toDo.get(); 37 } else { 38 return null; 39 } 40 } 41 42} 43

Model

java

1package com.example.ToDoList.model; 2 3import java.io.Serializable; 4import java.util.Date; 5 6import javax.persistence.Column; 7import javax.persistence.Entity; 8import javax.persistence.GeneratedValue; 9import javax.persistence.GenerationType; 10import javax.persistence.Id; 11import javax.persistence.Table; 12 13@Entity 14@Table(name="toDo") 15public class ToDo implements Serializable { 16 17 /** 18 * 19 */ 20 private static final long serialVersionUID = -269687929538344451L; 21 @Id 22 @GeneratedValue(strategy=GenerationType.AUTO) 23 @Column(name="id") 24 private Long id; 25 @Column(name="to_do") 26 private String toDo; 27 @Column(name="dead_line") 28 private Date deadLine; 29 @Column(name="created_date") 30 private Date createdDate; 31 @Column(name="updated_date") 32 private Date updatedDate; 33 34 public Long getId() { 35 return id; 36 } 37 38 public void setId(Long id) { 39 this.id = id; 40 } 41 42 public String getToDo() { 43 return toDo; 44 } 45 46 public void setToDo(String toDo) { 47 this.toDo = toDo; 48 } 49 50 public Date getCreatedDate() { 51 return createdDate; 52 } 53 54 public void setCreatedDate(Date createdDate) { 55 this.createdDate = createdDate; 56 } 57 58} 59

Repository

java

1package com.example.ToDoList.repository; 2 3import org.springframework.data.repository.CrudRepository; 4import org.springframework.stereotype.Repository; 5 6import com.example.ToDoList.model.ToDo; 7 8@Repository 9public interface ToDoRepository extends CrudRepository<ToDo, Long>{ 10 11} 12

Controller

java

1package com.example.ToDoList.controller; 2 3import java.util.List; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Controller; 7import org.springframework.ui.Model; 8import org.springframework.web.bind.annotation.GetMapping; 9import org.springframework.web.bind.annotation.ModelAttribute; 10import org.springframework.web.bind.annotation.PathVariable; 11import org.springframework.web.bind.annotation.PostMapping; 12import org.springframework.web.servlet.mvc.support.RedirectAttributes; 13 14import com.example.ToDoList.model.ToDo; 15import com.example.ToDoList.service.ToDoService; 16 17@Controller 18public class ToDoController { 19 @Autowired private ToDoService toDoService; 20 21 @GetMapping("/") 22 public String toDos(Model model) { 23 List<ToDo> toDos = toDoService.getAllToDos(); 24 model.addAttribute("toDos", toDos); 25 model.addAttribute("toDo", new ToDo()); 26 model.addAttribute("title", "ToDos"); 27 model.addAttribute("isAdd", true); 28 return "view/toDos"; 29 } 30 31 @PostMapping(value="/save") 32 public String save(@ModelAttribute ToDo toDo, RedirectAttributes redirectAttributes, Model model) { 33 ToDo dbToDo = toDoService.save(toDo); 34 if (dbToDo!=null) { 35 redirectAttributes.addFlashAttribute("successmessage", "ToDo is saved succesfully"); 36 return "redirect:/"; 37 } else { 38 model.addAttribute("errormessage", "ToDo is not saved, Please try again"); 39 model.addAttribute("todo", toDo); 40 return "view/toDos"; 41 } 42 } 43 44 @GetMapping(value="/getToDo/{id}") 45 public String getToDo(@PathVariable Long id, Model model) { 46 ToDo toDo = toDoService.findById(id); 47 List<ToDo> toDos = toDoService.getAllToDos(); 48 model.addAttribute("toDos", toDos); 49 model.addAttribute("toDo", toDo); 50 model.addAttribute("title", "Edit ToDos"); 51 model.addAttribute("isAdd", false); 52 return "view/toDos"; 53 } 54 55 @PostMapping(value="/update") 56 public String update(@ModelAttribute ToDo toDo, RedirectAttributes redirectAttributes, Model model) { 57 ToDo dbToDo = toDoService.update(toDo); 58 if (dbToDo != null) { 59 redirectAttributes.addFlashAttribute("successmessage", "ToDo is updated succesfully"); 60 return "redirect:/"; 61 } else { 62 model.addAttribute("errormessage", "ToDo is not updated, Please try again"); 63 model.addAttribute("toDo", toDo); 64 return "view/toDos"; 65 } 66 } 67} 68

HTML

html

1 2<body> 3<div class="container-fluid"> 4<div class="row"> 5<div class="col-md-12"> 6<h2>ToDo Form</h2> 7<div class="alert alert-success" th:if="${successmessage}"> 8 <a href="#" class="close" data-dismiss="alert" arial-label="close">&times;</a> 9 <strong th:text=${successmessage}></strong> 10</div> 11<div class="alert alert-warning" th:if="${errormessage}"> 12 <a href="#" class="close" data-dismiss="alert" arial-label="close">&times;</a> 13 <strong th:text=${errormessage}></strong> 14</div> 15<form action="#" th:action="@{${isAdd}?'/save':'/update'}" th:object="${toDo}" method="post"> 16<div class="form-group"> 17<input type="text" class="form-control" id="toDo" placeholder="ToDo" th:field="*{toDo}"> 18</div> 19<div class="form-group"> 20<input type="text" class="form-control" id="deadLine" placeholder="Dead Line" th:field="*{deadLine}"> 21</div> 22<input type="hidden" class="form-control" th:field="*{id}"> 23<button type="submit" class="btn btn-primary" th:text="${isAdd}?'Save':'Update'">Submit</button> 24</form> 25</div> 26 27<div class="col-md-12"> 28<h2>All ToDos</h2> 29<div style="margin-top:10px"> 30<table id="todotable" class="table table-striped table-bordered" style="width:100%"> 31<thead> 32<tr> 33 <th>ToDo</th> 34 <th>Action</th> 35</tr> 36</thead> 37<tbody> 38<tr th:each="toDo: ${toDos}"> 39<td th:text="${toDo.toDoName}"></td> 40<td> 41 <a th:href="@{'/getToDo/' + ${toDo.id}}"><i class="fa fa-edit" style="font-size: 21px;"></i></a> 42 <a href="javascript:void(0)" class="confirm-delete" th:attr="data-id=${toDo.id}, data-name=${toDo.toDoName}"> 43 <i class="fa fa-trash" style="font-size: 21px;color:red;"></i></a> 44</td> 45 46</tr> 47</tbody> 48</table> 49</div> 50</div> 51 52</div> 53</div> 54 55<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 56<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script> 57<script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script> 58<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script> 59<script type="text/javascript"> 60$("#toDotable").dataTable(); 61$(".confirm-delete").on('click', function(e){ 62 e,preventDefault(); 63 var id = $(this),data('id'); 64 var name = $(this).data('name'); 65 $("#modal-name").html(name); 66 $('#idModalLink').attr('href', '/deleteToDo/'+id); 67 $('#toDoModal').modal('show'); 68}) 69</script> 70 71</body> 72</html>

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

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

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

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

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

moootoko

2019/08/01 02:13 編集

テンプレートのHTML内で <tr th:each="toDo: ${toDos}"> <td th:text="${toDo.toDoName}"></td> とありますが、 th:eachのtoDoにはList<ToDo>リストから取り出したToDoオブジェクトが入っていると思います。 しかしToDoオブジェクトにはtoDoNameが定義されていませんが、それは大丈夫なのでしょうか。
guest

回答1

0

java

1 @Column(name="to_do") 2 private String toDo;

ToDoクラスのプロパティとしてtodoがあるので、上手くパースできてないのかもしれません。
カラム名も変更になりますが無難にtitleなどに変更してみるのはどうでしょう?

投稿2020/01/29 20:25

ShunTakeuchi

総合スコア10

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問