gpt4 book ai didi

java - Micronaut Controller 如何确定其基本 URL

转载 作者:行者123 更新时间:2023-11-30 05:31:16 28 4
gpt4 key购买 nike

例如,如果我有以下 Controller :

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.*;

@Controller("/test")
public class TestController {
@Get()
@Produces(MediaType.TEXT_PLAIN)
public String index() {
// How should this be implemented?
return "???";
}
}

我在我的服务器上运行它,然后我希望索引方法返回 http://my-server:8080 .

最佳答案

截至 Micronaut V1.2.0,您可以使用HttpHostResolver interface ,例如:

import io.micronaut.http.*;
import io.micronaut.http.annotation.*;
import io.micronaut.http.server.util.HttpHostResolver;
import io.micronaut.web.router.RouteBuilder;

@Controller("/test")
public class TestController {
private final HttpHostResolver httpHostResolver;
private final RouteBuilder.UriNamingStrategy uriNamingStrategy;

public TestController(
HttpHostResolver httpHostResolver,
RouteBuilder.UriNamingStrategy uriNamingStrategy
) {
this.httpHostResolver = httpHostResolver;
this.uriNamingStrategy = uriNamingStrategy;
}

@Get()
@Produces(MediaType.TEXT_PLAIN)
public String index(HttpRequest httpRequest) {
return httpHostResolver.resolve(httpRequest) +
uriNamingStrategy.resolveUri(TestController.class);
}
}

关于java - Micronaut Controller 如何确定其基本 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57495779/

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