java spring restcontroller错误404或配置和bean服务问题

j0pj023g  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(369)

当我不使用@ComponentScan时,我的程序不会得到任何错误,但它显示了错误404,我看不到来自 Postman 的任何rest调用。当我使用Component scan时,我得到了以下错误,我应该如何使我的程序工作?我对Spring很陌生,也许我的问题有点傻,但请帮我一点忙。

当我使用@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.

当我不使用@ComponentScan时:

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

仓库实体:

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 + '\'' +
                '}';
    }
}

仓库地址:

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

仓库储存库:

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

}

仓库仓储设施:

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

仓库服务:

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

仓库管理员:

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

}

SpringStage3应用程序:

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

    }
}

错误:


***************************

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.
aurhwmvo

aurhwmvo1#

@componentscan(basepackageclasses=warehouserestcontroller.class)只扫描warehouserestcontroller.class的相同包和子包中的组件。您的服务包含在不同的包中: com.example.serviceImpl 尝试使用:

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

附言 @SpringBootApplication 也可以作为一种消遣 @ComponentScan i、 它扫描SpringStage3应用程序包的所有子包。因为warehouserestcontroller位于不同的包中,所以它不会加载,所以您得到404。

uinbv5nw

uinbv5nw2#

spring boot中不需要注解@componentscan。问题在于项目的文件夹结构。在主文件夹spring_stage_3下添加所有其他文件夹(控制器、服务、回购…)。
文件夹结构示例

相关问题