没有从Spring Boot Application中调用多个RestController

时间:2019-03-03 17:48:32

标签: spring-boot spring-restcontroller

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

需要您的帮助来解决此问题。

包装结构 enter image description here

请找到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)

2 个答案:

答案 0 :(得分:1)

您在RequestMapping中使用了错误的属性来定义URL映射

@RequestMapping(name = "/customer")

应替换为

@RequestMapping(value = "/customer")

或者只是

@RequestMapping("/customer")

name属性仅为映射分配名称,而value指定URL映射

答案 1 :(得分:0)

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

说明:

Spring引导扫描组件从@SpringBootApplication类所在的文件夹开始,紧随所有子文件夹。

  

单个 @SpringBootApplication 注释可用于启用   这三个功能,即: @ EnableAutoConfiguration,@ ComponentScan,@ Configuration

     

@ComponetScan 在包装上启用 @Component 扫描,   应用程序已找到。