2つの異なるコントローラを作成することをお勧めします。自分のプロファイルのそれぞれが
@RestController
@RequestMapping(value = "/dev")
@ConditionalOnExpression("#{environment.getActiveProfiles()[0] == 'DEV'}")
public class DevCheckoutController {
と
@RestController
@RequestMapping(value = "/prod")
@ConditionalOnExpression("#{environment.getActiveProfiles()[0] == 'PROD'}")
public class ProdCheckoutController {
別の(より複雑な)方法RequestMappingHandlerMapping
をオーバーライドすることです。メソッドが必要です。
protected RequestMappingInfo createRequestMappingInfo(
RequestMapping requestMapping, RequestCondition<?> customCondition) {
return RequestMappingInfo
.paths(resolveEmbeddedValuesInPatterns(requestMapping.path()))
.methods(requestMapping.method())
.params(requestMapping.params())
.headers(requestMapping.headers())
.consumes(requestMapping.consumes())
.produces(requestMapping.produces())
.mappingName(requestMapping.name())
.customCondition(customCondition)
.options(this.config)
.build();
}
.paths(resolveEmbeddedValuesInPatterns(requestMapping.path()))
には、プロファイルに応じた値を追加するだけです。だから、環境
@Autowired
Environment env;
を追加し、この方法は、もう一つの方法は、あなたのマッピングだけで表現
@RequestMapping(value = "/${my.profile.property:}prod")
とDEVプロファイルのためだけの定義を追加することで、この
String prefix = "DEV".equals(env.getActiveProfiles()[0]) ? "DEV" : ""
return RequestMappingInfo
.paths(prefix + resolveEmbeddedValuesInPatterns(requestMapping.path()))
ようにする必要がありますプロパティー
my.profile.property=DEV
メモth 'resolveEmbeddedValuesInPatterns(requestMapping.path())'は配列を返しますので、最初の項目にアクセスして接頭辞: '.paths(接頭辞+ resolveEmbeddedValuesInPatterns(requestMapping.path())[]を追加する必要があります。 – angeldev
私は配列のすべての項目にプレフィックスを追加する必要があると言います。同じコントローラメソッドを指す複数のマッピングがある場合は、すべてのマッピングを適応させる必要があります。 – StanislavL