0

When I do not use @CompomentScan my program does not get any error, but it shows me error 404 and I can not see any rest call from the postman. When I use compoment scan I get the following error, what should I do to make my program work? Im very new to spring and maybe my question it's a little bit silly, but please a little help.

enter image description here

When i use @ComponentScan(basePackageClasses = WarehouseRestController.class):

Description:

Field warehouseService in com.example.rest.WarehouseRestController required a bean of type 'com.example.service.WarehouseService' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.example.service.WarehouseService' in your configuration.

When i dont use @CompomentScan :

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Jul 08 15:13:32 EEST 2021
There was an unexpected error (type=Not Found, status=404).
No message available

WarehouseEntity:

package com.example.entity;

import javax.persistence.*;

@Entity
@Table(name="warehouses")
//@Access(AccessType.FIELD)
public class Warehouse {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id_warehouse")
    private Long id_warehouse;

    @Column(name="description_warehouse")
    private String description_warehouse;


    public Warehouse() {
    }

    public Warehouse(Long id_warehouse, String description_warehouse) {
        this.id_warehouse = id_warehouse;
        this.description_warehouse = description_warehouse;
    }

    public Long getId_warehouse() {
        return id_warehouse;
    }

    public void setId_warehouse(Long id_warehouse) {
        this.id_warehouse = id_warehouse;
    }

    public String getDescription_warehouse() {
        return description_warehouse;
    }

    public void setDescription_warehouse(String description_warehouse) {
        this.description_warehouse = description_warehouse;
    }

    @Override
    public String toString() {
        return "Warehouses{" +
                "id_warehouse=" + id_warehouse +
                ", description_warehouse='" + description_warehouse + '\'' +
                '}';
    }
}

WarehouseDTO:

package com.example.dto;

public class WarehouseDTO {
private Long id_warehouse;
private String description_warehouse;

public WarehouseDTO() {
}

public WarehouseDTO(Long id_warehouse, String description_warehouse) {
    this.id_warehouse = id_warehouse;
    this.description_warehouse = description_warehouse;
}

public Long getId_warehouse() {
    return id_warehouse;
}

public void setId_warehouse(Long id_warehouse) {
    this.id_warehouse = id_warehouse;
}

public String getDescription_warehouse() {
    return description_warehouse;
}

public void setDescription_warehouse(String description_warehouse) {
    this.description_warehouse = description_warehouse;
}
}

WarehouseRepository:

package com.example.repository;

import com.example.entity.Warehouse;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface WarehouseRepository extends JpaRepository<Warehouse, Long>{


}

WarehouseRepositoryImpl:

package com.example.repositoryImpl;

import com.example.entity.QWarehouse;
import com.example.entity.Warehouse;
import com.example.repository.WarehouseRepositoryCustom;
import com.querydsl.jpa.impl.JPAQuery;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class WarehouseRepositoryImpl {

    @PersistenceContext
    private EntityManager entityManager;

    public Warehouse findByDescription(String description) {
        JPAQuery<Warehouse> warehouseJPAQuery = new JPAQuery<>();
        QWarehouse warehouse = QWarehouse.warehouse;

        Warehouse warehouse1 = warehouseJPAQuery.select(warehouse)
                .from(warehouse)
                .where(warehouse.description_warehouse.eq(description))
                .fetchOne();

        return warehouse1;
    }
}

WarehouseService:

package com.example.service;

import com.example.dto.WarehouseDTO;

import java.util.List;

public interface WarehouseService {
    List<WarehouseDTO> findAll();

    void create(WarehouseDTO warehouseDTO);

    void update(WarehouseDTO warehouseDTO);

    void deleteById(Long id);
}

WarehouseServiceImpl:

package com.example.serviceImpl;

import com.example.dto.WarehouseDTO;
import com.example.entity.Warehouse;
import com.example.repository.WarehouseRepository;
import com.example.service.WarehouseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Autowired
    private WarehouseRepository warehousesRepository;

    public Warehouse toEntity(WarehouseDTO warehouseDTO) {
        Warehouse warehouse = new Warehouse();
        BeanUtils.copyProperties(warehouseDTO, warehouse);
        return warehouse;
    }

    public WarehouseDTO toDTO(Warehouse warehouse) {
        WarehouseDTO warehouseDTO = new WarehouseDTO();
        BeanUtils.copyProperties(warehouse, warehouseDTO);
        return warehouseDTO;
    }
    @Override
    public List<WarehouseDTO> findAll() {
        List<Warehouse> warehouseList = this.warehousesRepository.findAll();
        List<WarehouseDTO> warehouseDTOList = new ArrayList<>();

        for (Warehouse warehouse : warehouseList) {
            warehouseDTOList.add(this.toDTO(warehouse));
        }
        return warehouseDTOList;
    }
    @Override
    public void create(WarehouseDTO warehouseDTO) {
        if (warehouseDTO != null && warehouseDTO.getId_warehouse() == null) {
            this.warehousesRepository.save(this.toEntity(warehouseDTO));
        }
    }
    @Override
    public void update(WarehouseDTO warehouseDTO) {
        if (warehouseDTO != null && warehouseDTO.getId_warehouse() != null) {
            this.warehousesRepository.save(this.toEntity(warehouseDTO));
        }
    }
    @Override
    public void deleteById(Long id) {
        if (id != null) {
            this.warehousesRepository.deleteById(id);
        }
    }
}

WarehouseRestController:

package com.example.rest;

import com.example.dto.WarehouseDTO;
import com.example.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@ResponseBody
@Component
@RequestMapping("/warehouse")
public class WarehouseRestController {

    @Autowired
    private WarehouseService warehouseService;

    @GetMapping("/all")
    private List<WarehouseDTO> findAll(){
        return warehouseService.findAll();
    }

    @PostMapping("/create")
    private void create(@RequestBody WarehouseDTO warehouseDTO){
         this.warehouseService.create(warehouseDTO);
    }

    @PutMapping("/update")
    private void update(@RequestBody WarehouseDTO warehouseDTO){
        this.warehouseService.create(warehouseDTO);
    }

    @DeleteMapping("/delete/(id)")
    private void delete(@PathVariable (value = "id") Long id){
        this.warehouseService.deleteById(id);
    }

}

SpringStage3Application :

package com.example.Spring_Stage_3;

import com.example.rest.WarehouseRestController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackageClasses = WarehouseRestController.class)
public class SpringStage3Application {

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

    }
}

Error:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field warehouseService in com.example.rest.WarehouseRestController required a bean of type 'com.example.service.WarehouseService' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.example.service.WarehouseService' in your configuration.
James Z
  • 12,209
  • 10
  • 24
  • 44
Konstantinos K.
  • 181
  • 1
  • 9
  • adding `WarehouseServiceImpl.class` and `WarehouseRepository.class` to `ComponentScan` in `SpringStage3Application` solves the problem? – badger Jul 08 '21 at 12:29
  • Move your `SpringStage3Application` to `com.example` and ditch the `@ComponentScan`. Or move all your other packages under `com.example.Spring_Stage_3` as subpackages and remove the `@ComponentScan`. – M. Deinum Jul 08 '21 at 12:38
  • @hatefAlipoor no :( but different error ! Consider defining a bean of type 'com.example.repository.WarehouseRepository' in your configuration. – Konstantinos K. Jul 08 '21 at 12:43
  • @M.Deinum 's approach solves the problem? the error says spring context couldn't find you beans – badger Jul 08 '21 at 13:03

2 Answers2

0

There is no need of annotation @componentScan in spring boot. Problem is with the folder structure of your project. Add all the other folders (controllers, services, repo....) under your main folder Spring_Stage_3.

folder structure example

enter image description here

James Z
  • 12,209
  • 10
  • 24
  • 44
0

@ComponentScan(basePackageClasses = WarehouseRestController.class) scans only components in the same package and subpackages of WarehouseRestController.class. Your service lays in different package: com.example.serviceImpl

Try using:

    @SpringBootApplication
    @ComponentScan(basePackageClasses = {WarehouseRestController.class,
    WarehouseServiceImpl.class,
    WarehouseRepositoryImpl.impl
    } )
    public class SpringStage3Application

PS @SpringBootApplication works also as a defualt @ComponentScan i.e. it scans all subpackeges of SpringStage3Application package. Since WarehouseRestController is in different package it does not load, therefore you get 404.

Saginatio
  • 99
  • 4