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

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

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

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

Q&A

解決済

2回答

4650閲覧

SpringBoot 1回の処理が2回実行される

chocolate_pie

総合スコア26

Spring Boot

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

0グッド

0クリップ

投稿2020/02/13 01:52

編集2020/02/13 03:32

前提・実現したいこと

SpringBootでurlをベタ打ちして処理を実行すると本来は1回処理するメソッドが2回処理されてしまいます。
HTMLからformを通じでメソッドを処理すると1回の実行で済みます。

urlをベタ打ちした場合でも1回の処理にしたいです。

エラーメッセージを解決すれば正常に処理がされると思うのですが、解決方法が分かりません。
設定ファイル等々がおかしいような気がするので、application.propatiesとpom.xmlも載せましたがほかに設定ファイルがありましたらご教授いただきたいです。

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

エラーメッセージ(form遷移、ベタ打ち双方) 2020-02-13 10:32:30.267 WARN 1384 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "favicon.ico"]

該当のソースコード

表示用GETメソッド

Java

1 @RequestMapping(value = "/memberlist/{id}") 2 public String memberList(@PathVariable Integer id, Model model, Principal principal) { 3 4 Authentication auth = (Authentication) principal; 5 UserMaster loginuser = (UserMaster) auth.getPrincipal(); 6 Integer loginid = loginuser.getId(); 7 model.addAttribute("loginid",loginid); 8 9 if (id != loginid) { 10 return "redirect:/home"; 11 } 12 13 List<ReviewCks> userlists = reviewcksService.findByOkEmpId(id); 14 model.addAttribute("userlists", userlists); 15 16 List<Meisai> userStatuses = meisaiservice.findStatus(id); 17 model.addAttribute("userstatuses", userStatuses); 18 19 return "users/memberlist"; 20 }

memberlist.html

HTML

1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org" 3 xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity"> 4<head th:replace="layout :: layout('HOME',~{::link})"> 5<meta charset="UTF-8"> 6<title>Insert title here</title> 7</head> 8<body> 9 <div th:replace="layout :: header"></div> 10 <div class="container contmarge"> 11 <div class="row"> 12 <div class="col-md-10 col-md-offset-1"> 13 <div class="panel panel-default"> 14 <div class="panel-heading"> 15 <h3>メンバー一覧</h3> 16 </div> 17 <div class="panel-body">メンバーの目標確認状況を確認します</div> 18 <form method="get" th:action="@{/memberlist/{id}/unsubmit(id=${loginid})}"> 19 20 <input type="submit" class="btn btn-success " value="未提出者LINE通知実行" /> 21 </form> 22 <table class="table"> 23 <thead> 24 <tr> 25 <th>名前</th> 26 <th>期初レビューチェック</th> 27 <th>期中レビューチェック</th> 28 <th>期末レビューチェック</th> 29 <th></th> 30 </tr> 31 </thead> 32 <tbody> 33 <tr th:each="userlist: ${userlists}" th:object="${userlist}"> 34 <td th:text="*{rvwuserid.fullname}"></td> 35 <td th:text="*{firstcomp==true}? '済' : '未'"></td> 36 <td th:text="*{midcomp==true}? '済' : '未'"></td> 37 <td th:text="*{endcomp==true}? '済' : '未'"></td> 38 <td><form method="post" 39 th:action="@{/memberdetail/{id}(id=*{rvwuserid.id})}"> 40 <input type="submit" class="btn btn-primary center-block" 41 value="詳細" /> 42 </form></td> 43 </tr> 44 </tbody> 45 </table> 46 47 <div class="panel-footer"> 48 <button class="btn btn-default" 49 onclick="location.href='/home'">戻る</button> 50 </div> 51 </div> 52 </div> 53 </div> 54 </div> 55 <div th:replace="layout :: footer"></div> 56</body> 57</html>

処理用GETメソッド

Java

1 @RequestMapping(value = "/memberlist/{id}/unsubmit", method = RequestMethod.GET) 2 public String noticeUnsubmitted(@PathVariable Integer id) { 3 List<Meisai> userStatuses = meisaiservice.findStatus(id); 4 List<String> users=new ArrayList<String>(); 5 6 for(Meisai userStatuse: userStatuses) { 7 Integer userId=userStatuse.getGoalSeq().getUserid().getId(); 8 ReviewCks rvw = reviewcksService.findByEmpId(userId); 9 UserMaster user=usermasterservice.findOne(userId); 10 String username=user.getFullname(); 11 Boolean first = rvw.getFirstcomp(); 12 Boolean mid = rvw.getMidcomp(); 13 Boolean end = rvw.getEndcomp(); 14 Integer timing = 0; 15 if (first == true && mid == false) { 16 timing = 1; 17 } 18 if (mid == true && end == false) { 19 timing = 2; 20 } 21 Integer firstStatus=userStatuse.getFirststatus(); 22 Integer midStatus=userStatuse.getMidstatus(); 23 Integer endStatus=userStatuse.getEndstatus(); 24 if(timing==0&&firstStatus==MeisaiService.STATUS_ENPTY||timing==1&&midStatus==MeisaiService.STATUS_ENPTY||timing==2&&endStatus==MeisaiService.STATUS_ENPTY) { 25 users.add(username); 26 } 27 28 } 29 List<String> userHashs= new ArrayList<String>(new HashSet<>(users)); 30 System.out.println(userHashs); 31 32 return "redirect:/memberlist/{id}"; 33 }

URLベタ打ちコンソールはの場合コンソールは以下のような感じでsysoutが2回実行されエラーが出てしまいます。

[テスト2(一般ユーザー)] [テスト2(一般ユーザー)] 2020-02-13 10:42:58.559 WARN 1384 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "favicon.ico"]

pom.xml

XML

1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.2.2.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>jp.co.itc.mbo</groupId> 12 <artifactId>mboapp</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>mboapp</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version> 20 <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version> 21 </properties> 22 23 <dependencies> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-data-jpa</artifactId> 27 </dependency> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-jdbc</artifactId> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-security</artifactId> 35 </dependency> 36 <dependency> 37 <groupId>org.thymeleaf.extras</groupId> 38 <artifactId>thymeleaf-extras-springsecurity5</artifactId> 39 </dependency> 40 <dependency> 41 <groupId>org.springframework.boot</groupId> 42 <artifactId>spring-boot-starter-thymeleaf</artifactId> 43 </dependency> 44 <dependency> 45 <groupId>org.springframework.boot</groupId> 46 <artifactId>spring-boot-starter-web</artifactId> 47 </dependency> 48 49 <dependency> 50 <groupId>org.springframework.boot</groupId> 51 <artifactId>spring-boot-devtools</artifactId> 52 <scope>runtime</scope> 53 <optional>true</optional> 54 </dependency> 55 <dependency> 56 <groupId>mysql</groupId> 57 <artifactId>mysql-connector-java</artifactId> 58 <scope>runtime</scope> 59 </dependency> 60 <dependency> 61 <groupId>org.springframework.boot</groupId> 62 <artifactId>spring-boot-starter-test</artifactId> 63 <scope>test</scope> 64 <exclusions> 65 <exclusion> 66 <groupId>org.junit.vintage</groupId> 67 <artifactId>junit-vintage-engine</artifactId> 68 </exclusion> 69 </exclusions> 70 </dependency> 71 <dependency> 72 <groupId>org.springframework.security</groupId> 73 <artifactId>spring-security-test</artifactId> 74 <scope>test</scope> 75 </dependency> 76 <dependency> 77<groupId>org.webjars</groupId> 78<artifactId>bootstrap</artifactId> 79<version>3.3.5</version> 80</dependency> 81<dependency> 82 <groupId>org.springframework.boot</groupId> 83 <artifactId>spring-boot-starter-mail</artifactId> 84 </dependency> 85 <dependency> 86 <groupId>org.webjars</groupId> 87 <artifactId>jquery</artifactId> 88 <version>3.4.1</version> 89 </dependency> 90 <dependency> 91 <groupId>org.hibernate</groupId> 92 <artifactId>hibernate-jpamodelgen</artifactId> 93 </dependency> 94 95 </dependencies> 96 97 <build> 98 <plugins> 99 <plugin> 100 <groupId>org.springframework.boot</groupId> 101 <artifactId>spring-boot-maven-plugin</artifactId> 102 </plugin> 103 </plugins> 104 </build> 105 106</project> 107

application.propaties

spring.thymeleaf.cache=false spring.datasource.url=jdbc:mysql://localhost:3306/mboapp?serverTimezone=JST spring.datasource.username=**** spring.datasource.password=**** spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.jpa.database=MYSQL spring.mail.host=smtp.gmail.com spring.mail.port=587 spring.mail.username=**** spring.mail.password=**** spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true

試したこと

・何回もデバックしました
・googleでエラーメッセージを検索しましたが分かりませんでした
【追記】
HTMLにformからgetで飛ぶボタンを実装したら処理は1回になりましたがエラーは直りませんでした

補足情報(FW/ツールのバージョンなど)

バージョン Spring Boot ::(v2.2.2.RELEASE)

初心者で至らない点があると思いますがよろしくお願いします。

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

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

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

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

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

A-pZ

2020/02/13 03:17

HTMLにformからgetで飛ぶ実装と、実際にエラーが2度?発生したのはどのように実行したのかを端折らずに合わせて記載すると、正しい回答が得られるかと思います。
chocolate_pie

2020/02/13 03:24

アドバイスありがとうございます。 再度詳細も含めて編集させていただきます。
guest

回答2

0

ベストアンサー

エラーの内容は favicon.ico がないので、警告(WARN)しているということだと思います。
動作に影響が出るようなものではないと思いますし、sysoutが2回実行とは関係がない気もします。

favicon.ico はブラウザがWebページを表示する際に自動でアクセスしにいくもので、検索するといろいろ出てきますが、ブラウザなどでアイコンを表示するためのものです。
たとえば http://www.example.com/ にブラウザでアクセスすると、ブラウザは http://www.example.com/favicon.ico にアイコンファイルを取りに行こうとします。

出力されている警告は、コントローラーに /favicon.ico がないためですが、 Spring Boot って本来はこの辺り自動で favicon が設定された気もします。
クラスの全体や、使用している Spring Boot のバージョン、 application.yml などの設定ファイルなどがないと原因は探しづらいです。

投稿2020/02/13 02:41

aaharu

総合スコア441

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

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

chocolate_pie

2020/02/13 03:05 編集

ご回答ありがとうございます。 原因はfaviconではないと教えていただきありがとうございます。 他のクラスでもメソッドが2回処理されることが起こっていたので、設定ファイルを確認してみます。
guest

0

こんにちは、Spring Bootについては使用経験がありませんので憶測での回答ですが、
@RequestMappingで指定したパスから変数を受け取りたい場合、

java

1@PathVariable("id") Integer id) { 2

としなければいけないのでは無いでしょうか?

以下のサイトを参考にさせていただきました。

Spring Boot解説第18回(基本編:Controllerとは)
Failed to convert value of type 'java.lang.String' to required type 'int';

投稿2020/02/13 02:37

NeoSavage

総合スコア8

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

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

chocolate_pie

2020/02/13 02:55

ご回答ありがとうございます。 public String noticeUnsubmitted(@PathVariable("id") Integer id) {} としてましたが様子が変わりませんでしたので問題点は別にありました。
NeoSavage

2020/02/13 03:07 編集

そうですか。 MethodArgumentTypeMismatchExceptionとあるので、URLのあたりで型エラーが起きてそうなのですが。 リクエストしているURLの{id}の部分は、正しくInteger型になっていますか?
chocolate_pie

2020/02/13 03:16

デバックしたところ{id}はIntegerで取れていて、その後の検索クエリも正常に実行されていました。他のメソッドでも同じエラーが出るので全体的な問題だと思われます????わざわざコメントありがとうございました…!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問