Tworzę projekt w Springu, napotkałem się na cos takiego. Jest to dopiero mój drugi projekt w Springu, z tego co wiem to gdzieś nie tworzę odpowiedniego Beana. Chodzi o klastę PatientMapper. Wspomagam się MapStructem.
Parameter 1 of constructor in doctorsoffice.patient.PatientServiceImpl required a bean of type 'doctorsoffice.patient.PatientMapper' that could not be found.
package doctorsoffice.patient;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import java.util.Collection;
import java.util.List;
@Mapper(componentModel = "spring")
public interface PatientMapper {
@Mappings({
@Mapping(target = "password", ignore = true)
})
PatientDTO toPatientDTO(Patient patient);
List<PatientDTO> toPatientDTO(Collection<Patient> patients);
}
Problem pojawia się w konstruktorze PatientServiceImpl
package doctorsoffice.patient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
@Service
@Transactional
public class PatientServiceImpl implements PatientService {
private PatientMapper patientMapper;
private PatientRepository patientRepository;
@Autowired
public PatientServiceImpl(PatientRepository patientRepository, PatientMapper patientMapper) {
this.patientMapper = patientMapper;
this.patientRepository = patientRepository;
}
@Override
public PatientDTO registerPatient(String name, String surname, int age, Long pesel, String email, String password) {
Patient patient = new Patient();
patient.setAge(age);
patient.setName(name);
patient.setSurname(surname);
patient.setPassword(password);
patient.setEmail(email);
patient.setPesel(pesel);
patient = patientRepository.save(patient);
return patientMapper.toPatientDTO(patient);
}
}
Klasa Application
package doctorsoffice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import static springfox.documentation.builders.PathSelectors.regex;
@SpringBootApplication
@RestController
@EntityScan(
basePackageClasses = {Application.class, Jsr310JpaConverters.class}
)
@EnableJpaRepositories
@EnableSwagger2
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public Docket newsApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("api")
.apiInfo(apiInfo())
.select()
.paths(regex("/api.*"))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring REST Sample with Swagger")
.description("Spring REST Sample with Swagger")
.termsOfServiceUrl("http://www-03.ibm.com/software/sla/sladb.nsf/sla/bm?Open")
.contact("Niklas Heidloff")
.license("Apache License Version 2.0")
.licenseUrl("https://github.com/IBM-Bluemix/news-aggregator/blob/master/LICENSE")
.version("2.0")
.build();
}
}