2016-03-30 6 views
0

私は、ユーザーがさまざまなタイプの倉庫内のユニークなアイテムを取得できるようにするWebアプリケーションを作成しています。 コントローラクラスで定義しようとしている2つのGET APIエンドポイントがあります。2つの似たRequestMappingルートをマッピングする春

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)... 

答えて

1

リクエストマッピングアノテーションは、2つのルートを区別するために使用できるparamsという別のものを使用します。ローマが示唆するように、それは以下のようになります。

@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); 
} 
1

使用パラメータRequestMapping注釈のparams:次のエラーが表示されたと私は、すべてのテストメソッドを実行することができませんでした。同様のマッピングを区別するためのパラメータを定義することができます。

1

この

@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); 
} 
のようにそれを使用します
関連する問題