Java Spring JPA, problem z Beanem.

Java Spring JPA, problem z Beanem.
K2
  • Rejestracja:około 9 lat
  • Ostatnio:prawie 7 lat
  • Postów:77
0

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.

Kopiuj
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

Kopiuj
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

Kopiuj
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();
    }
}
edytowany 2x, ostatnio: kucia2129
Burdzi0
Popraw kodowanie, aby podświetlało kolorki, niech życie nabierze trochę blasku
BA
  • Rejestracja:ponad 7 lat
  • Ostatnio:około 4 lata
  • Lokalizacja:Kraków
  • Postów:68
0

A dodałeś MapStrucktowy plugin do build toola, żeby ci wygenerował ten mapper?

OtoKamil
Z tym pluginem to w IntelliJ (w mvn, nie wiem czy gradle też) jest taki myk, że nie działa. Trzeba sobie dodać zależność zamiast plugina. Jeśli używasz javy8 to musisz się też upewnić że masz artifact mapstruct-jdk8, a nie mapstruct.
K2
  • Rejestracja:około 9 lat
  • Ostatnio:prawie 7 lat
  • Postów:77
0
Kopiuj
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>doctors-office</groupId>
    <artifactId>doctors-office</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>

    <dependencies>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>1.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.2.0.Final</version>

            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.mapstruct</groupId>
                    <artifactId>mapstruct</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>1.4.3.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
            </resource>
            <resource>
                <directory>${project.build.directory}/generated-resources</directory>
            </resource>
        </resources>


        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>1.2.0.Final</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

mam :( @bames

edytowany 1x, ostatnio: kucia2129
BA
  • Rejestracja:ponad 7 lat
  • Ostatnio:około 4 lata
  • Lokalizacja:Kraków
  • Postów:68
0

i na pewno generuje się ten kod?

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.