请求映射模式在URL的末尾* /

时间:2013-06-26 15:08:31

标签: spring url spring-mvc pattern-matching

我正在尝试使用Spring Controller映射网址。 我希望变量petitionId位于我的url的末尾,例如,我想要映射:

.../product/all/petitionId/{petitionId}

以及

.../product/productId/{productId}/clientId/{clientId}/petitionId/{petitionId}

为此,我尝试在控制器头中设置RequestMapping,就像这样

@Controller
@RequestMapping(value = "product/*/petitionId/{petitionId}")
public class ProductController

并在我想要映射的方法的声明中

@RequestMapping(value = "*/all/*", method = RequestMethod.GET)
public @ResponseBody
String getProducts(@PathVariable Long petitionId) 

我也试过带有一个,两个没有星号的斜杠......同样的404错误结果。 我想要的要求是

http://192.168.1.27:9999/middleware/product/all/petitionId/20

我知道我可能在每种方法中都有完整的URL映射,但这并不是最优雅的方法。 有人知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

按功能使用注释@RequestMapping。 您可以在类中使用它,但只能在每个函数的requestMapping中少写。在课堂上加入您在控制器的所有功能中的共同点。

例如:

@Controller
@RequestMapping(value = "/products")
public class ProductController {
    ...

    @RequestMapping(value = "", method = RequestMethod.GET)
    public @ResponseBody
    String getProducts() { ... }

    @RequestMapping(value = "/{productId}", method = RequestMethod.GET)
    public @ResponseBody
    String getProductsById(@PathVariable Long productId) { ... }

    @RequestMapping(value = "/{productId}/clients/{clientId}/petitions/{petitionId}", method = RequestMethod.GET)
    public @ResponseBody
    String getPetition(@PathVariable Long productId, @PathVariable Long clientId, @PathVariable Long petitionId) { ... }
}

您最终会得到以下映射:

/products
/products/{productId}
/products/{productId}/clients/{clientId}/petitions/{petitionId}

答案 1 :(得分:0)

说实话,您的网址看起来有些复杂。

您是否考虑过其他网址方案,例如通过请愿所有产品:

GET http://192.168.1.27:9999/middleware/petitions/20/products 

或产品ID,客户ID和请愿ID:

GET http://192.168.1.27:9999/middleware/products?clientId=10&productId=10&petitionId=20