gpt4 book ai didi

java - Spring 在映射 2 个类似的 RequestMapping 路由时遇到问题

转载 作者:行者123 更新时间:2023-12-01 21:42:15 24 4
gpt4 key购买 nike

我正在编写一个网络应用程序,它允许用户检索不同类型仓库中的唯一项目。我尝试在 Controller 类中定义 2 个 GET API 端点。

Retrieve items related to a particular warehouse.
api-item/v1/items?warehouse="001"
Parameter: warehouse

Retrieve all items from all warehouses
api-item/v1/items

ItemController.java:

@RequestMapping(value = "/v1/items", method = RequestMethod.GET)
public ReturnResponse getItems() {

List <Product> itemsList = itemService.getItems();

return myDefinedUtilities.getHttpStatusResponse("success.items", HttpStatus.OK, itemsList, null);
}




@RequestMapping(value = "/v1/items", method = RequestMethod.GET)
public ReturnResponse getItemsForWarehouse(@RequestParam(required = true, value = "warehouse") String wareHouseId) {

List <Product> itemsList = itemService.getItemsForWarehouse(wareHouseId);

return myDefinedUtilities.getHttpStatusResponse("success.items", HttpStatus.OK, itemsList, null);
}

我已经定义了在 JUnit 测试类中测试这 2 个方法的测试方法,模拟服务类:itemService。我无法运行任何测试方法,并显示以下错误:

java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at ....
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'itemController' method
public com.trinet.domain.common.ReturnResponse com.trinet.web.controller.product.itemController.getItemsForWarehouse(java.lang.String)
to {[/v1/items],methods=[GET]}: There is already 'itemController' bean method
public com.trinet.domain.common.ReturnResponse com.trinet.web.controller.product.itemController.getItems() mapped.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)...

最佳答案

请求映射注释采用另一个参数,即params,可用于区分两个路由。正如 Roman 建议的那样,如下所示。

@RequestMapping(value = "/v1/items", method = RequestMethod.GET, params="warehouse")

@RequestMapping(value = "/v1/items", method = RequestMethod.GET)
public ReturnResponse getItems() {

List <Product> itemsList = itemService.getItems();

return myDefinedUtilities.getHttpStatusResponse("success.items", HttpStatus.OK, itemsList, null);
}




@RequestMapping(value = "/v1/items", method = RequestMethod.GET, params="warehouse")
public ReturnResponse getItemsForWarehouse(@RequestParam(required = true, value = "warehouse") String wareHouseId) {

List <Product> itemsList = itemService.getItemsForWarehouse(wareHouseId);

return myDefinedUtilities.getHttpStatusResponse("success.items", HttpStatus.OK, itemsList, null);
}

关于java - Spring 在映射 2 个类似的 RequestMapping 路由时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36312368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com