【问题标题】:Multiple RestController are not getting called from Spring Boot Application没有从 Spring Boot 应用程序调用多个 RestController
【发布时间】:2019-03-04 17:56:58
【问题描述】:

我正在尝试使用两个不同的 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

【问题讨论】:

  • 在 CustomerController 中调用 API 的 url 是什么?
  • 您能说明如何调用这些API 方法吗?你有什么错误? 40* 或 50*?
  • 无需添加 scanBasePackages = {"com.ayan.*"} 并且如果您添加包,请在末尾省略 .*跨度>
  • 你好@sidgate,请找到我从邮递员那里触发的 URL。我也看不到控制台中的任何日志。我还测试过,当我将此代码放在 DestinationController 类中时,它工作正常。
  • @sidgate URL - localhost:8080/customer/addCustomer 请求正文:{“customer”:{“customerId”:null,“firstName”:“Alison”,“middleName”:“”,“lastName”:“麦迪逊”,“手机”:“+61 434657890”,“电子邮件”:“alison.madison@gmail.com”,“地址”:“西珀斯海街 990 号”,“州”:“WA”,“国家” ":"澳大利亚","邮政编码":"6005","travelDocumentType":"驾驶执照","travelDocumentNumber":"AB990878" } }

标签: spring-boot spring-restcontroller


【解决方案1】:

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

@RequestMapping(name = "/customer")

应该替换为

@RequestMapping(value = "/customer")

或者只是

@RequestMapping("/customer")

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

【讨论】:

    【解决方案2】:

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

    说明:

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

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

    @ComponetScan 启用 @Component 扫描包 应用程序的位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      • 2023-02-24
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      相关资源
      最近更新 更多