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

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

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

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

Spring

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

MyBatis

MyBatisはJavaや.NET Frameworkでなどで使用できる、SQL文や、ストアドプロシージャをオブジェクトと紐付けるO/Rマッピングフレームワークです。

Q&A

解決済

1回答

3636閲覧

【MyBatis-Spring】サーバからデータを取得しようとすると500エラーが返ってきてしまう

S.niranira

総合スコア19

Java

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

Spring

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

MyBatis

MyBatisはJavaや.NET Frameworkでなどで使用できる、SQL文や、ストアドプロシージャをオブジェクトと紐付けるO/Rマッピングフレームワークです。

0グッド

0クリップ

投稿2018/04/18 07:42

前提・実現したいこと

お世話になっております。
今回、データベースからデータを取得するようプログラムを組んでいるのですが、
ブラウザで確認を行おうとすると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) 以下略

該当のソースコード

コントローラー

java

1package jp.sample.billing.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.PathVariable; 9import org.springframework.web.bind.annotation.RequestMapping; 10import org.springframework.web.bind.annotation.RequestMethod; 11 12import jp.sample.billing.dto.TestDto; 13import jp.sample.billing.service.TestService; 14 15@Controller 16public class TestController { 17 18 @Autowired 19 private TestService testSv; 20 21 @RequestMapping(value="/test/{id}", method = RequestMethod.GET) 22 public String test(Model model, @PathVariable int id){ 23 24 TestDto test = testSv.getTest(id); 25 model.addAttribute("message", "MyBatisのサンプルです。"); 26 model.addAttribute("test", test); 27 28 return "test"; 29 30 } 31 32 @RequestMapping(value = "/test/", method = RequestMethod.GET) 33 public String testAll(Model model) { 34 List<TestDto> tests = testSv.getTestAll(); 35 model.addAttribute("message", "MyBatisの全件取得サンプルです"); 36 model.addAttribute("tests", tests); 37 return "testAll"; 38 } 39 40}

Dto

java

1package jp.sample.billing.dto; 2 3import lombok.Data; 4 5@Data 6 7public class TestDto { 8 9 private Integer id; 10 11 private String name; 12 13 public Integer getId() { 14 return id; 15 } 16 17 public void setId(Integer id) { 18 this.id = id; 19 } 20 21 public String getName() { 22 return name; 23 } 24 25 public void setName(String name) { 26 this.name = name; 27 } 28 29}

エンティティ

java

1package jp.sample.billing.entity; 2 3import java.io.Serializable; 4import javax.persistence.*; 5 6 7/** 8 * The persistent class for the test database table. 9 * 10 */ 11@Entity 12@NamedQuery(name="Test.findAll", query="SELECT t FROM Test t") 13public class Test implements Serializable { 14 private static final long serialVersionUID = 1L; 15 16 @Id 17 private int id; 18 19 private String name; 20 21 public Test() { 22 } 23 24 public int getId() { 25 return this.id; 26 } 27 28 public void setId(int id) { 29 this.id = id; 30 } 31 32 public String getName() { 33 return this.name; 34 } 35 36 public void setName(String name) { 37 this.name = name; 38 } 39 40}

フォーム

java

1package jp.sample.billing.form; 2 3import lombok.Data; 4 5@Data 6public class TestForm { 7 8 private int id; 9 10 private String name; 11 12 public int getId() { 13 return id; 14 } 15 16 public void setId(int id) { 17 this.id = id; 18 } 19 20 public String getName() { 21 return name; 22 } 23 24 public void setName(String name) { 25 this.name = name; 26 } 27 28} 29

マッパー

java

1package jp.sample.billing.mapper; 2 3import java.util.List; 4 5import jp.sample.billing.entity.Test; 6 7public interface TestMapper { 8 Test getTest(int id); 9 10 List<Test> getTestAll(); 11 12} 13

xml

1<?xml version="1.0" encoding="UTF-8" ?> 2<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 5<mapper namespace="jp.sample.billing.mapper.TestMapper"> 6 <resultMap id="TestResult" type="jp.sample.billing.entity.Test"> 7 <result column="id" property="id" /> 8 <result column="name" property="name" /> 9 </resultMap> 10 11 <!-- SELECT by id --> 12 <select id="getTest" resultMap="TestResult" parameterType="int"> 13 select id, name 14 from test 15 where id = #{id} 16 </select> 17 18 <!-- SELECT by All --> 19 <select id="getTestAll" resultMap="TestResult"> 20 select id, name 21 from test; 22 </select> 23 24</mapper>

サービス

java

1package jp.sample.billing.service; 2 3import java.util.LinkedList; 4import java.util.List; 5 6import org.springframework.beans.BeanUtils; 7import org.springframework.beans.factory.annotation.Autowired; 8import org.springframework.stereotype.Service; 9 10import jp.sample.billing.dto.TestDto; 11import jp.sample.billing.entity.Test; 12import jp.sample.billing.mapper.TestMapper; 13 14@Service 15public class TestService { 16 17 @Autowired 18 private TestMapper testMap; 19 20 public TestDto getTest(Integer id){ 21 TestDto dto = new TestDto(); 22 Test entity = testMap.getTest(id); 23 BeanUtils.copyProperties(entity, dto); 24 25 return dto; 26 } 27 28 public List<TestDto> getTestAll() { 29 List<Test> testList = testMap.getTestAll(); 30 List<TestDto> resultList = convertToDto(testList); 31 return resultList; 32 } 33 34 private List<TestDto> convertToDto(List<Test> testList) { 35 List<TestDto> resultList = new LinkedList<>(); 36 for (Test entity : testList) { 37 TestDto dto = new TestDto(); 38 BeanUtils.copyProperties(entity, dto); 39 resultList.add(dto); 40 } 41 return resultList; 42 } 43}

不足分のソースは別途記載いたします。
長くなってしまいましたが、よろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

自己解決

コントローラーからサービスファイルがインポートできていなかったことが原因でした。
@Controller を付与しているファイルを以下のように変更したところ、解決しました。

変更前

java

1@Controller 2public class TestController {

変更後

java

1@Controller 2@Import(TestService.class) 3public class TestController {

投稿2018/04/18 07:55

S.niranira

総合スコア19

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問