1

I receive the error when attempting to run my Spring Boot app:

Field studentMapper in com.adam.rest.client.impl.StudentClientImpl required a bean of type 'com.adam.rest.mapper.StudentMapper' that could not be found.

Could you suggest what is going wrong? (See files below)

App.java

package com.adam.rest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

StudentClientImpl.java

package com.adam.rest.client.impl;

import com.adam.rest.mapper.StudentMapper;
import com.adam.rest.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Repository
public class StudentClientImpl {

    @Autowired
    private StudentMapper studentMapper;

    private static Map<Integer, Student> students;
         // DUMMY DATA GOES HERE

    Collection<Student> getStudents() { return this.students.values(); }

    Student getStudent(Integer id) { return this.students.get(id); }

    ...

StudentMapper.java

package com.adam.rest.mapper;

import com.adam.rest.model.Student;

import java.util.Collection;

public interface StudentMapper {

    Collection<Student> getStudents();

    Student getStudent(Integer id);

    Student getStudent(String name);

    void updateStudent(Student student);

    void removeStudent(Integer id);

    void removeStudent(String name);

    void createStudent(Student student);

}
tommybee
  • 2,409
  • 1
  • 20
  • 23
physicsboy
  • 5,656
  • 17
  • 70
  • 119

1 Answers1

2

Create an implementation class for your interface:

public class StudentMapperImpl implements StudentMapper {
 ...
}

Then add a @Bean method to your @SpringBootApplication class or another @Configuration class.

For example:

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Bean
    public StudentMapperImpl studentMapper() {
        return new StudentMapperImpl ();
    }
}
Michael Peacock
  • 2,011
  • 1
  • 11
  • 14