前提・実現したいこと
現在springとJPAの学習の為、某サイトに載っていたソースコードを参考にさせて頂いてました。
しかし、1系と2系(今2系です)の違いなのかどうもブラウザで表示ができません。
エラー内容としては、
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Sep 12 14:51:43 JST 2019
There was an unexpected error (type=Not Found, status=404).
No message available
になります。
また、起動時のログが以下の文章になります。(勉強不足のためマッピング情報がどれかわからなかったので文字数制限のため一部書かせて頂きました。)
2019-09-12 17:23:44.013 INFO 4246 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate Core {5.3.11.Final}
2019-09-12 17:23:44.025 INFO 4246 --- [ restartedMain] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2019-09-12 17:23:45.073 INFO 4246 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-09-12 17:23:45.477 INFO 4246 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-09-12 17:23:45.866 INFO 4246 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2019-09-12 17:23:45.963 INFO 4246 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2019-09-12 17:23:46.663 INFO 4246 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-09-12 17:23:46.776 INFO 4246 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2019-09-12 17:23:47.467 INFO 4246 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-12 17:23:47.584 WARN 4246 --- [ restartedMain] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2019-09-12 17:23:48.186 INFO 4246 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-12 17:23:48.198 INFO 4246 --- [ restartedMain] c.example.demo.PlayerSampleApplication : Started PlayerSampleApplication in 11.952 seconds (JVM running for 15.298)
2019-09-12 17:23:49.645 INFO 4246 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-09-12 17:23:49.650 INFO 4246 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-09-12 17:23:49.835 INFO 4246 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 185 ms
長文になってしまい申し訳ありません。
エラーが出るタイミングとしては起動後、ブラウザにてhttp://localhost:8080/playersと入力した時です。
おそらく最初のGetMapping("/players")の時点で処理の問題、もしくはhtmlでのtymeleafのエラーなのかもしれませんが間違いに気づくことができません。
JPAによる、自動でテーブルをcreate、レコードのinsert、selectができるかどうかを試しています。
Tomcatの起動も出来ている、ControllerのURLも間違っていないようですし、原因がわからず滞っています。
どうかお力を貸して頂きたいです。
よろしくお願いします。
java
1package com.example.demo; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6@SpringBootApplication 7public class PlayerSampleApplication { 8 9 public static void main(String[] args) { 10 SpringApplication.run(PlayerSampleApplication.class, args); 11 } 12 13} 14
java
1package com.example.baseball.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.DeleteMapping; 9import org.springframework.web.bind.annotation.GetMapping; 10import org.springframework.web.bind.annotation.ModelAttribute; 11import org.springframework.web.bind.annotation.PathVariable; 12import org.springframework.web.bind.annotation.PostMapping; 13import org.springframework.web.bind.annotation.PutMapping; 14 15import com.example.baseball.domain.Player; 16import com.example.baseball.service.PlayerService; 17 18@Controller 19public class PlayerController { 20 @Autowired 21 private PlayerService playerService; 22 23 @GetMapping("/players") 24 public String index(Model model) { 25 List<Player> players = playerService.findAll(); 26 model.addAttribute("players", players); 27 return "players/index"; 28 } 29 30 @GetMapping("new") 31 public String newPlayer(Model model) { 32 return "players/new"; 33 } 34 35 @GetMapping("{id}/edit") 36 public String edit(@PathVariable Long id, Model model) { 37 Player player = playerService.findOne(id); 38 model.addAttribute("player", player); 39 return "players/edit"; 40 } 41 42 @GetMapping("{id}") 43 public String show(@PathVariable Long id, Model model) { 44 Player player = playerService.findOne(id); 45 model.addAttribute("player", player); 46 return "players/show"; 47 } 48 49 @PostMapping 50 public String create(@ModelAttribute Player player) { 51 playerService.save(player); 52 return "redirect:/players"; 53 } 54 55 @PutMapping("{id}") 56 public String update(@PathVariable Long id, @ModelAttribute Player player) { 57 player.setId(id); 58 playerService.save(player); 59 return "redirect:/players"; 60 } 61 62 @DeleteMapping("{id}") 63 public String destroy(@PathVariable Long id) { 64 playerService.delete(id); 65 return "redirect:/players"; 66 } 67}
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3 4 <head> 5 <meta charset="utf-8" /> 6 <title>Listing Players - baseball</title> 7 <!-- ① --> 8 <link rel="stylesheet" href="/css/bootstrap.css" /> 9 <script src="/js/jquery.js"></script> 10 <script src="/js/bootstrap.js"></script> 11 </head> 12 <body> 13 <div class="container"> 14 <h1>Listing Players</h1> 15 <table class="table"> 16 <thead> 17 <tr> 18 <th>ID</th> 19 <th>名前</th> 20 <th>年齢</th> 21 <th>チーム名</th> 22 <th>守備位置</th> 23 <th></th> 24 <th></th> 25 <th></th> 26 </tr> 27 </thead> 28 <tbody> 29 <!-- ② --> 30 <tr th:each="player:${players}" th:object="${player}"> 31 <!-- ③ --> 32 <td th:text="*{id}"></td> 33 <td th:text="*{name}"></td> 34 <td th:text="*{age}"></td> 35 <td th:text="*{team}"></td> 36 <td th:text="*{position}"></td> 37 38 <td><a class="btn btn-default btn-xs" th:href="@{/players/{id}(id=*{id})}">参照</a></td> 39 <td><a class="btn btn-default btn-xs" th:href="@{/players/{id}/edit(id=*{id})}">編集</a></td> 40 <td> 41 42 <form th:action="@{/players/{id}(id=*{id})}" th:method="delete"> 43 <input class="btn btn-default btn-xs" type="submit" value="削除" /> 44 </form> 45 </td> 46 </tr> 47 </tbody> 48 </table> 49 <a class="btn btn-default" href="/players/new">新規作成</a> 50 </div> 51 </body> 52</html> 53
java
1package com.example.baseball.service; 2 3import java.util.List; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Service; 7 8import com.example.baseball.domain.Player; 9import com.example.baseball.repository.PlayerRepository; 10 11@Service 12public class PlayerService { 13 @Autowired 14 private PlayerRepository playerRepository; 15 16 public List<Player> findAll() { 17 return playerRepository.findAll(); 18 } 19 20 public Player findOne(Long id) { 21 22 return playerRepository.findById(id).orElse(null); 23 } 24 25 public Player save(Player player) { 26 return playerRepository.save(player); 27 } 28 29 public void delete(Long id) { 30 playerRepository.deleteById(id); 31 } 32}
java
1package com.example.baseball.repository; 2 3import org.springframework.data.jpa.repository.JpaRepository; 4import org.springframework.stereotype.Repository; 5 6import com.example.baseball.domain.Player; 7 8@Repository 9public interface PlayerRepository extends JpaRepository<Player, Long> { 10 11}
java
1package com.example.baseball.domain; 2 3import javax.persistence.Entity; 4import javax.persistence.GeneratedValue; 5import javax.persistence.GenerationType; 6import javax.persistence.Id; 7 8@Entity 9public class Player { 10 @Id 11 @GeneratedValue(strategy = GenerationType.IDENTITY) 12 private Long id; 13 private String name; 14 private Integer age; 15 private String team; 16 private String position; 17 18 public Long getId() { 19 return id; 20 } 21 public void setId(Long id) { 22 this.id = id; 23 } 24 public String getName() { 25 return name; 26 } 27 public void setName(String name) { 28 this.name = name; 29 } 30 public Integer getAge() { 31 return age; 32 } 33 public void setAge(Integer age) { 34 this.age = age; 35 } 36 public String getTeam() { 37 return team; 38 } 39 public void setTeam(String team) { 40 this.team = team; 41 } 42 public String getPosition() { 43 return position; 44 } 45 public void setPosition(String position) { 46 this.position = position; 47 } 48 49 @Override 50 public String toString() { 51 return "Player [id=" + id + ", name=" + name + ", age=" + age + ", team=" + team + ", position=" + position + "]"; 52 } 53}
回答1件
あなたの回答
tips
プレビュー