gpt4 book ai didi

java - Spring Integration HTTP 入站网关路径中的可选参数

转载 作者:太空宇宙 更新时间:2023-11-04 06:15:00 24 4
gpt4 key购买 nike

我正在使用 Spring Integration 来提供一些 REST Web 服务。我的服务的网址类似于

http://myhost/param1_name/param1_value/param2_name/param2_value

当然,我不仅仅只有 2 个参数,而且我希望其中一些参数是“可选的”。

我找到了以下链接http://www.nakov.com/blog/2009/07/15/jax-rs-path-pathparam-and-optional-parameters/中描述的解决方案

不幸的是,它不适用于 Spring Integration。在 Spring Integration 中,每当我尝试使用正则表达式 [^/] 来处理参数时,这都不起作用。

例如,firstName 永远不会像这样匹配:路径=“/注册/firstName/{firstName:[^/]+}/lastName/”

知道如何使用 Spring Integration 实现可选参数吗?

最佳答案

我已经与 Rossen Stoyanchev 就此事进行了讨论,他发现您最初的方法是错误的。以下是他的原话:

The path is supposed to reflect the hierarchy of REST resources. Concerns such as encoding and content type don't belong there. They're cross-cutting and could apply to any URL. Just imagine a REST API that supports /encoding/utf-8/format/pdf on every URL. In Spring MVC we even support content negotiation by query parameter (e.g. format=pdf), or file extension (.pdf) as first-class mechanisms alternative to using the Accept header (based on ContentNegotiationStrategy). That's common practice and I can't imagine why anyone would think to put such information in path variables.

He does have an example with firstName and optional lastName. I don't think that's a good idea either. First of all names can change even if not often. Also think of all the places where a person can appear in a REST API. Now you'd have to support firstName with optional lastName everywhere. E.g. /person/{firstName}/{lastName}/children/{firstName}/{lastName}.

Seems like both examples are trying to put random data in the URL path that belongs to query parameters or the request body. The path should reflect hierarchy of resources

BTW the referenced article actually says JAX-RS doesn't support optional path variables. He describes it as a hack.

因此,如果您可以将这些 optinal 部分移至请求参数并使用一些通用path 来映射它们,那就更好了。或者... @RequestMappingparams 选项,允许将方法映射到 URL(如果它包含或不包含特定参数)。

关于java - Spring Integration HTTP 入站网关路径中的可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28204047/

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