0

我正在尝试使用 Spring Controller 映射 url。我想要在我的网址末尾的变量请愿ID,例如,我想映射:

.../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 映射,但这不是最优雅的方法。有谁知道如何解决这个问题?

4

2 回答 2

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}
于 2013-06-26T20:20:45.320 回答
0

老实说,您的 URL 看起来有些复杂。

您是否考虑过其他 URL 方案,例如针对所有产品的请愿书:

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

?

于 2013-06-26T19:49:49.600 回答