postgresql 创建在文件中定义了名称为“countryController”的Bean时出错

3z6pesqy  于 2023-03-01  发布在  PostgreSQL
关注(0)|答案(2)|浏览(129)

我试图使地址的国家有许多国家和和和国家有许多地区和地区有许多市。
我的国家/地区

package com.part.firstProject.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Component
public class CountryDTO {
    private int id;
    private String name;
    private List<StatesDTO> states;
}

我的国家实体:

package com.part.firstProject.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.*;
import java.util.List;
import java.util.Set;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
@Table(name="Country")
public class Country {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="country_id")
    private int id;
    @Column(name="country_name")
    private String name;
    @ManyToOne
    private States states;
}

我的国家控制器:

package com.part.firstProject.controller;

import com.part.firstProject.dto.CountryDTO;
import com.part.firstProject.entity.Country;
import com.part.firstProject.service.CountryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api")
public class CountryController {
    private  CountryService countryService;

    @Autowired
    public CountryController(CountryService countryService){
        this.countryService=countryService;
    }
    @GetMapping("/getCountry")
    public ResponseEntity<?>findAll(){
        List<CountryDTO> countries=countryService.findAll();
        return new ResponseEntity<>(countries,countries.size()>0? HttpStatus.OK:HttpStatus.NOT_FOUND);
    }
}

我的国家/地区服务实施:

package com.part.firstProject.service;

import com.part.firstProject.dto.CountryDTO;
import com.part.firstProject.entity.Country;
import com.part.firstProject.repository.CountryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CountryServiceImpl implements CountryService {
    CountryRepository countryRepository;
    @Autowired
    CountryServiceImpl(CountryRepository countryRepository){
        this.countryRepository=countryRepository;
    }
    @Override
    public List<CountryDTO> findAll() {
     List<CountryDTO> countryDTOS= countryRepository.findAll();
        if(countryDTOS.size()>0){
            return countryDTOS;
        }else{
            return new ArrayList<CountryDTO>();
        }
    }

    @Override
    public CountryDTO findById(int id) {
        return null;
    }

    @Override
    public void save(CountryDTO countryList) {
            countryRepository.save(countryList);
    }
}

我的国家存储库:

package com.part.firstProject.repository;
import com.part.firstProject.dto.CountryDTO;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CountryRepository extends JpaRepository<CountryDTO, Integer> {

}

在我的主应用程序中:

package com.part.firstProject;

import com.part.firstProject.service.AddressService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.io.*;
import java.util.Objects;

@SpringBootApplication
public class FirstProjectApplication {

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

    @Bean
    CommandLineRunner runner(AddressService addressService) {
        return args -> {
            // read json and write to db
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(
                        Objects.requireNonNull(this.getClass().
                                getResourceAsStream("/nepal-data.json"))));
                System.out.println("Users Saved!");
            } catch (Exception e){
                System.out.println("Unable to save users: " + e.getMessage());
            }
        };
    }

}

我希望这将顺利运行,但我遇到以下错误
创建在文件中定义了名称为"countryController"的Bean时出错
创建在文件中定义了名称为"countryServiceImpl"的Bean时出错
创建在com. part. firstProject. repository中定义的名为"countryRepository"的Bean时出错
不是托管类型:类别组成部分第一个项目日期国家日期

rryofs0p

rryofs0p1#

如果您看到正在尝试从countrydto的db中获取数据
List<CountryDTO> countries=countryService.findAll();
然而,
错误是说CountryDTO is not a managed class表示它应该是实体。
要么需要它的类型为@Entity以触发spring data jpa查询,要么进行重构以仅调用实体上的存储库方法。

wtlkbnrh

wtlkbnrh2#

在Web应用程序的主函数中使用@ComponentScan(“basePackage name),请尝试以下操作。

相关问题