【MyBatis-Spring】サーバからデータを取得しようとすると500エラーが返ってきてしまう
解決済
回答 1
投稿
- 評価
- クリップ 0
- VIEW 1,711
前提・実現したいこと
お世話になっております。
今回、データベースからデータを取得するようプログラムを組んでいるのですが、
ブラウザで確認を行おうとすると500エラーが返ってきてしまいます。
エラーの内容を確認したのですが、@Autowiredがおかしいくらいまでしか理解できず、
そこより深い部分での原因がわからないため、相談させてください。
発生している問題・エラーメッセージ
【例外】
javax.servlet.ServletException: サーブレット [dispatcherServlet] のServlet.init()が例外を投げました
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
以下略
【原因】
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'testSv'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'jp.sample.billing.service.TestService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1268)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:171)
以下略
該当のソースコード
コントローラー
package jp.sample.billing.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import jp.sample.billing.dto.TestDto;
import jp.sample.billing.service.TestService;
@Controller
public class TestController {
@Autowired
private TestService testSv;
@RequestMapping(value="/test/{id}", method = RequestMethod.GET)
public String test(Model model, @PathVariable int id){
TestDto test = testSv.getTest(id);
model.addAttribute("message", "MyBatisのサンプルです。");
model.addAttribute("test", test);
return "test";
}
@RequestMapping(value = "/test/", method = RequestMethod.GET)
public String testAll(Model model) {
List<TestDto> tests = testSv.getTestAll();
model.addAttribute("message", "MyBatisの全件取得サンプルです");
model.addAttribute("tests", tests);
return "testAll";
}
}
Dto
package jp.sample.billing.dto;
import lombok.Data;
@Data
public class TestDto {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
エンティティ
package jp.sample.billing.entity;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the test database table.
*
*/
@Entity
@NamedQuery(name="Test.findAll", query="SELECT t FROM Test t")
public class Test implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private int id;
private String name;
public Test() {
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
フォーム
package jp.sample.billing.form;
import lombok.Data;
@Data
public class TestForm {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
マッパー
package jp.sample.billing.mapper;
import java.util.List;
import jp.sample.billing.entity.Test;
public interface TestMapper {
Test getTest(int id);
List<Test> getTestAll();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="jp.sample.billing.mapper.TestMapper">
<resultMap id="TestResult" type="jp.sample.billing.entity.Test">
<result column="id" property="id" />
<result column="name" property="name" />
</resultMap>
<!-- SELECT by id -->
<select id="getTest" resultMap="TestResult" parameterType="int">
select id, name
from test
where id = #{id}
</select>
<!-- SELECT by All -->
<select id="getTestAll" resultMap="TestResult">
select id, name
from test;
</select>
</mapper>
サービス
package jp.sample.billing.service;
import java.util.LinkedList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import jp.sample.billing.dto.TestDto;
import jp.sample.billing.entity.Test;
import jp.sample.billing.mapper.TestMapper;
@Service
public class TestService {
@Autowired
private TestMapper testMap;
public TestDto getTest(Integer id){
TestDto dto = new TestDto();
Test entity = testMap.getTest(id);
BeanUtils.copyProperties(entity, dto);
return dto;
}
public List<TestDto> getTestAll() {
List<Test> testList = testMap.getTestAll();
List<TestDto> resultList = convertToDto(testList);
return resultList;
}
private List<TestDto> convertToDto(List<Test> testList) {
List<TestDto> resultList = new LinkedList<>();
for (Test entity : testList) {
TestDto dto = new TestDto();
BeanUtils.copyProperties(entity, dto);
resultList.add(dto);
}
return resultList;
}
}
不足分のソースは別途記載いたします。
長くなってしまいましたが、よろしくお願いいたします。
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
check解決した方法
+1
コントローラーからサービスファイルがインポートできていなかったことが原因でした。
@Controller を付与しているファイルを以下のように変更したところ、解決しました。
変更前
@Controller
public class TestController {
変更後
@Controller
@Import(TestService.class)
public class TestController {
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.32%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる