Spring 引导应用程序未调用多个 RestController

Multiple RestController are not getting called from Spring Boot Application

我正在尝试使用两个不同的 Rest Controller 构建 Spring Boot Rest 应用程序。 我看到正在调用 DestinationController,而没有调用 CustomerController。

需要你的帮助来解决这个问题。

包结构

请查找 RestController 代码

目的地控制器代码:

package com.ayan.tourismSystem.controller.rest;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ayan.tourismSystem.entity.Destination;
import com.ayan.tourismSystem.entity.wrapper.DestinationWrapper;
import com.ayan.tourismSystem.service.DestinationService;

@RestController
@RequestMapping(value="/api/destination")
public class DestinationController {

    private static final Logger logger = LoggerFactory.getLogger(DestinationController.class);

    @Autowired
    private DestinationService destinationService;

    /**
     * @author Ayan Bhattacharyya
     * @param region
     * @return Destination List by region
     */
    @RequestMapping(method= RequestMethod.GET, value="/region/{region}")
    public List<Destination> getDestinationByRegion(
            @PathVariable(value="region")String region){
        return this.destinationService.getDestinationByRegion(region);
    }

    /**
     * @author Ayan Bhattacharyya
     * @param country
     * @return Destination List by country
     */
    @RequestMapping(method= RequestMethod.GET, value="/country/{country}")
    public List<Destination> getDestinationByCountry(
            @PathVariable(value="country")String country){
        return this.destinationService.getDestinationByCountry(country);
    }

    /**
     * @author Ayan Bhattacharyya 
     * @param destinationDetails(Destination Name, Destination Country, Destination Region)
     * @return Destination Id (Created)
     */
    @RequestMapping(method = RequestMethod.POST, value = "/addDestination")
    public Long addDestination(@RequestBody DestinationWrapper addDestination) {
        logger.info("Add destination Service Call Started");
        if (addDestination != null) {
            String name = addDestination.getDestination().getDestinationName();
            String country = addDestination.getDestination().getCountry();
            String region = addDestination.getDestination().getRegion();

            logger.info("Values before passing to service " + name + " " + country + " " + region);
            Destination addedDestination = this.destinationService.addDestination(name, country, region);

            Long response = addedDestination.getDestinationId();

            return response;
        }
        return null;
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/updateDestination")
    public Long updateDestination(@RequestBody DestinationWrapper addDestination) {
        logger.info("Update Destination Call Started");
        if (addDestination != null) {
            Long destinationId = addDestination.getDestination().getDestinationId();
            String name = addDestination.getDestination().getDestinationName();
            String country = addDestination.getDestination().getCountry();
            String region = addDestination.getDestination().getRegion();

            logger.info("Values before passing to service " + destinationId + " " + name + " " + country + " " + region);
            Destination addedDestination = this.destinationService.updateDestination(destinationId, name, country, region);

            Long response = addedDestination.getDestinationId();

            return response;
        }
        return null;
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/deleteDestination/{destinationId}")
    public void deleteDestination(@PathVariable(value="destinationId")String destinationId) {
        logger.info("Delete Destination Call Started");

            logger.info("Values before passing to service " + destinationId);
            Long id = Long.valueOf(destinationId);
            this.destinationService.deleteDestination(id);
    }
}

客户控制代码

package com.ayan.tourismSystem.controller.rest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ayan.tourismSystem.entity.Customer;
import com.ayan.tourismSystem.entity.wrapper.CustomerWrapper;
import com.ayan.tourismSystem.service.CustomerService;

@RestController
@RequestMapping(name = "/customer")
public class CustomerController {

    private static final Logger logger = LoggerFactory.getLogger(CustomerController.class);

    @Autowired
    private CustomerService customerService;

    /**
     * @author Ayan Bhattacharyya 
     * @param destinationDetails(Destination Name, Destination Country, Destination Region)
     * @return Destination Id (Created)
     */
    @RequestMapping(method = RequestMethod.POST, value = "/addCustomer")
    public Long addCustomer(@RequestBody CustomerWrapper addCustomer) {
        logger.info("Add Customer Call Started");
        if (addCustomer != null) {
            String firstName = addCustomer.getCustomer().getFirstName();
            String middleName = addCustomer.getCustomer().getMiddleName();
            String lastName = addCustomer.getCustomer().getLastName();
            String email = addCustomer.getCustomer().getEmail();
            String mobile = addCustomer.getCustomer().getMobile();
            String address = addCustomer.getCustomer().getAddress();
            String state = addCustomer.getCustomer().getAddress();
            String country = addCustomer.getCustomer().getCountry();
            String postcode = addCustomer.getCustomer().getPostcode();
            String travelDocumentType = addCustomer.getCustomer().getTravelDocumentType();
            String travelDocumentNumber = addCustomer.getCustomer().getTravelDocumentNumber();

            Customer customer = this.customerService.addCustomer(firstName, middleName, 
                    lastName, email, mobile, address, state, country, 
                    postcode, travelDocumentType, travelDocumentNumber);
            if(customer != null){
                return customer.getCustomerId();
            }
            else{
                return null;
            }
        }

        return null;
    }
}

Spring申请代码

package com.ayan.tourismSystem;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = {"com.ayan.*"})
public class TourismSystemApplication {

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

}

请告诉我我在这里遗漏/做错了什么。

提前致谢! Ayan Bhattacharyya

TLDR; 您不需要添加 scanBasePackages 因为 spring 会根据您的主文件夹的位置查找所有子文件夹 class。

解释:

Spring 引导扫描组件从您的 @SpringBootApplication class 所在的文件夹开始,在所有子文件夹之后。

A single @SpringBootApplication annotation can be used to enable those three features, that is: @EnableAutoConfiguration, @ComponentScan, @Configuration.

@ComponetScan enable @Component scan on the package where the application is located.

您在 RequestMapping 中使用了不正确的属性来定义 URL 映射

@RequestMapping(name = "/customer")

应替换为

@RequestMapping(value = "/customer")

@RequestMapping("/customer")

name 属性只是为映射分配一个名称,而 value 指定 URL 映射