skip to Main Content

I am trying To make Addresses where Country has many states and And in States there are many districts and in districts there are many Municipality.

My CountryDTO:

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;
}

MyCountryEntity:

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;
}

MyCountryController:

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);
    }
}

MYCountryServiceImpl:

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);
    }
}

MyCountryRepository:

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> {

}

In my MainApplication:

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());
            }
        };
    }

}

I was expecting this will run smoothly, but I am facing the following error

Error creating bean with name ‘countryController’ defined in file
Error creating bean with name ‘countryServiceImpl’ defined in file
Error creating bean with name ‘countryRepository’ defined in com.part.firstProject.repository
Not a managed type: class com.part.firstProject.dto.CountryDTO

2

Answers


  1. If you see
    you are trying to fetch data from db for countrydto

    List<CountryDTO> countries=countryService.findAll();

    however,

    the error is saying that CountryDTO is not a managed class meaning its expected to be entity.

    Either you need it to be of type @Entity to fire spring data jpa queries or refactor to only call repository methods on entity only.

    Login or Signup to reply.
  2. use @ComponentScan("basePackage name) in the main function of a web application, try this.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search