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

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

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

当我使用@componentscan(basepackageclasses=warehouserestcontroller.class)时:

  1. Description:
  2. Field warehouseService in com.example.rest.WarehouseRestController required a bean of type 'com.example.service.WarehouseService' that could not be found.
  3. The injection point has the following annotations:
  4. - @org.springframework.beans.factory.annotation.Autowired(required=true)
  5. Action:
  6. Consider defining a bean of type 'com.example.service.WarehouseService' in your configuration.

当我不使用@ComponentScan时:

  1. This application has no explicit mapping for /error, so you are seeing this as a fallback.
  2. Thu Jul 08 15:13:32 EEST 2021
  3. There was an unexpected error (type=Not Found, status=404).
  4. No message available

仓库实体:

  1. package com.example.entity;
  2. import javax.persistence.*;
  3. @Entity
  4. @Table(name="warehouses")
  5. //@Access(AccessType.FIELD)
  6. public class Warehouse {
  7. @Id
  8. @GeneratedValue(strategy = GenerationType.IDENTITY)
  9. @Column(name="id_warehouse")
  10. private Long id_warehouse;
  11. @Column(name="description_warehouse")
  12. private String description_warehouse;
  13. public Warehouse() {
  14. }
  15. public Warehouse(Long id_warehouse, String description_warehouse) {
  16. this.id_warehouse = id_warehouse;
  17. this.description_warehouse = description_warehouse;
  18. }
  19. public Long getId_warehouse() {
  20. return id_warehouse;
  21. }
  22. public void setId_warehouse(Long id_warehouse) {
  23. this.id_warehouse = id_warehouse;
  24. }
  25. public String getDescription_warehouse() {
  26. return description_warehouse;
  27. }
  28. public void setDescription_warehouse(String description_warehouse) {
  29. this.description_warehouse = description_warehouse;
  30. }
  31. @Override
  32. public String toString() {
  33. return "Warehouses{" +
  34. "id_warehouse=" + id_warehouse +
  35. ", description_warehouse='" + description_warehouse + '\'' +
  36. '}';
  37. }
  38. }

仓库地址:

  1. package com.example.dto;
  2. public class WarehouseDTO {
  3. private Long id_warehouse;
  4. private String description_warehouse;
  5. public WarehouseDTO() {
  6. }
  7. public WarehouseDTO(Long id_warehouse, String description_warehouse) {
  8. this.id_warehouse = id_warehouse;
  9. this.description_warehouse = description_warehouse;
  10. }
  11. public Long getId_warehouse() {
  12. return id_warehouse;
  13. }
  14. public void setId_warehouse(Long id_warehouse) {
  15. this.id_warehouse = id_warehouse;
  16. }
  17. public String getDescription_warehouse() {
  18. return description_warehouse;
  19. }
  20. public void setDescription_warehouse(String description_warehouse) {
  21. this.description_warehouse = description_warehouse;
  22. }
  23. }

仓库储存库:

  1. package com.example.repository;
  2. import com.example.entity.Warehouse;
  3. import org.springframework.data.jpa.repository.JpaRepository;
  4. import org.springframework.stereotype.Repository;
  5. @Repository
  6. public interface WarehouseRepository extends JpaRepository<Warehouse, Long>{
  7. }

仓库仓储设施:

  1. package com.example.repositoryImpl;
  2. import com.example.entity.QWarehouse;
  3. import com.example.entity.Warehouse;
  4. import com.example.repository.WarehouseRepositoryCustom;
  5. import com.querydsl.jpa.impl.JPAQuery;
  6. import javax.persistence.EntityManager;
  7. import javax.persistence.PersistenceContext;
  8. public class WarehouseRepositoryImpl {
  9. @PersistenceContext
  10. private EntityManager entityManager;
  11. public Warehouse findByDescription(String description) {
  12. JPAQuery<Warehouse> warehouseJPAQuery = new JPAQuery<>();
  13. QWarehouse warehouse = QWarehouse.warehouse;
  14. Warehouse warehouse1 = warehouseJPAQuery.select(warehouse)
  15. .from(warehouse)
  16. .where(warehouse.description_warehouse.eq(description))
  17. .fetchOne();
  18. return warehouse1;
  19. }
  20. }

仓库服务:

  1. package com.example.service;
  2. import com.example.dto.WarehouseDTO;
  3. import java.util.List;
  4. public interface WarehouseService {
  5. List<WarehouseDTO> findAll();
  6. void create(WarehouseDTO warehouseDTO);
  7. void update(WarehouseDTO warehouseDTO);
  8. void deleteById(Long id);
  9. }

warehouseserviceimpl:

  1. package com.example.serviceImpl;
  2. import com.example.dto.WarehouseDTO;
  3. import com.example.entity.Warehouse;
  4. import com.example.repository.WarehouseRepository;
  5. import com.example.service.WarehouseService;
  6. import org.springframework.beans.BeanUtils;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. @Service
  12. public class WarehouseServiceImpl implements WarehouseService {
  13. @Autowired
  14. private WarehouseRepository warehousesRepository;
  15. public Warehouse toEntity(WarehouseDTO warehouseDTO) {
  16. Warehouse warehouse = new Warehouse();
  17. BeanUtils.copyProperties(warehouseDTO, warehouse);
  18. return warehouse;
  19. }
  20. public WarehouseDTO toDTO(Warehouse warehouse) {
  21. WarehouseDTO warehouseDTO = new WarehouseDTO();
  22. BeanUtils.copyProperties(warehouse, warehouseDTO);
  23. return warehouseDTO;
  24. }
  25. @Override
  26. public List<WarehouseDTO> findAll() {
  27. List<Warehouse> warehouseList = this.warehousesRepository.findAll();
  28. List<WarehouseDTO> warehouseDTOList = new ArrayList<>();
  29. for (Warehouse warehouse : warehouseList) {
  30. warehouseDTOList.add(this.toDTO(warehouse));
  31. }
  32. return warehouseDTOList;
  33. }
  34. @Override
  35. public void create(WarehouseDTO warehouseDTO) {
  36. if (warehouseDTO != null && warehouseDTO.getId_warehouse() == null) {
  37. this.warehousesRepository.save(this.toEntity(warehouseDTO));
  38. }
  39. }
  40. @Override
  41. public void update(WarehouseDTO warehouseDTO) {
  42. if (warehouseDTO != null && warehouseDTO.getId_warehouse() != null) {
  43. this.warehousesRepository.save(this.toEntity(warehouseDTO));
  44. }
  45. }
  46. @Override
  47. public void deleteById(Long id) {
  48. if (id != null) {
  49. this.warehousesRepository.deleteById(id);
  50. }
  51. }
  52. }

仓库管理员:

  1. package com.example.rest;
  2. import com.example.dto.WarehouseDTO;
  3. import com.example.service.WarehouseService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Component;
  6. import org.springframework.web.bind.annotation.*;
  7. import java.util.List;
  8. @RestController
  9. @ResponseBody
  10. @Component
  11. @RequestMapping("/warehouse")
  12. public class WarehouseRestController {
  13. @Autowired
  14. private WarehouseService warehouseService;
  15. @GetMapping("/all")
  16. private List<WarehouseDTO> findAll(){
  17. return warehouseService.findAll();
  18. }
  19. @PostMapping("/create")
  20. private void create(@RequestBody WarehouseDTO warehouseDTO){
  21. this.warehouseService.create(warehouseDTO);
  22. }
  23. @PutMapping("/update")
  24. private void update(@RequestBody WarehouseDTO warehouseDTO){
  25. this.warehouseService.create(warehouseDTO);
  26. }
  27. @DeleteMapping("/delete/(id)")
  28. private void delete(@PathVariable (value = "id") Long id){
  29. this.warehouseService.deleteById(id);
  30. }
  31. }

SpringStage3应用程序:

  1. package com.example.Spring_Stage_3;
  2. import com.example.rest.WarehouseRestController;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.context.annotation.ComponentScan;
  6. @SpringBootApplication
  7. @ComponentScan(basePackageClasses = WarehouseRestController.class)
  8. public class SpringStage3Application {
  9. public static void main(String[] args) {
  10. SpringApplication.run(SpringStage3Application.class, args);
  11. }
  12. }

错误:

  1. ***************************
  2. APPLICATION FAILED TO START
  3. ***************************
  4. Description:
  5. Field warehouseService in com.example.rest.WarehouseRestController required a bean of type 'com.example.service.WarehouseService' that could not be found.
  6. The injection point has the following annotations:
  7. - @org.springframework.beans.factory.annotation.Autowired(required=true)
  8. Action:
  9. Consider defining a bean of type 'com.example.service.WarehouseService' in your configuration.
aurhwmvo

aurhwmvo1#

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

  1. @SpringBootApplication
  2. @ComponentScan(basePackageClasses = {WarehouseRestController.class,
  3. WarehouseServiceImpl.class,
  4. WarehouseRepositoryImpl.impl
  5. } )
  6. public class SpringStage3Application

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

uinbv5nw

uinbv5nw2#

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

相关问题