英文:
How to get an specific application route in spring boot application with domain, port and path?
问题
我在StackOverflow上找到了类似的问题,但没有被采纳的答案。
我想发送一个电子邮件验证链接。
例如:https://host:port/path?encrypted_email=encrypted-data-of-user-email
我想从注册控制器将此URL发送到用户的电子邮件中。但我不想硬编码验证电子邮件的Http/https、主机、端口和路径。我想要使用Spring Boot的帮助来获取这些信息。我该怎么做,如何解决这个问题?
谢谢
英文:
I have found similar questions in StackOverflow but there are no accepted answers.
I want to send an email verification link.
For example: https://host:port/path?encrypted_email=encrypted-data-of-user-email
I want to send this URL to the user's email from the signup controller. But I won't write the Http/https, host, port, and path of verifying email hardcoded. I want to get that using spring boot's help. What can I do and how can I overcome this situation?
Thanks
答案1
得分: 0
在到处搜索,包括 StackOverflow 后,我找到了这个:
获取主机名:
String host = InetAddress.getLocalHost().getHostName();
获取端口:
// 在你的 application.properties 文件中
server.port=8080
String port = environment.getProperty("server.port");
我还编写了一个示例类。
import org.springframework.core.env.Environment;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class ApiUrl {
private final Environment environment;
public static final String apiUrlPrefix = "/api";
public static final String apiVersion = "/v1";
public ApiUrl(Environment environment) {
this.environment = environment;
}
public String getApiBaseUrl() throws UnknownHostException {
String host = InetAddress.getLocalHost().getHostName();
String port = environment.getProperty("server.port");
return "https://" + host + ":" + port + apiUrlPrefix + apiVersion;
}
}
希望这对大家有所帮助。
更多学习参考:
https://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html
英文:
After searching everywhere including StackOverflow. I found this:
to get host:
String host = InetAddress.getLocalHost().getHostName();
to get port:
// In your application.properties
server.port=8080
String port = environment.getProperty("server.port");
I have also written an example class on it.
import org.springframework.core.env.Environment;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class ApiUrl {
private final Environment environment;
public static final String apiUrlPrefix = "/api";
public static final String apiVersion = "/v1";
public ApiUrl(Environment environment) {
this.environment = environment;
}
public String getApiBaseUrl() throws UnknownHostException {
String host = InetAddress.getLocalHost().getHostName();
String port = environment.getProperty("server.port");
return "https://" + host +":"+ port + apiUrlPrefix + apiVersion;
}
}
I hope this might help people.
References for more study:
https://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论