如何在Spring Boot应用程序中使用域名、端口和路径获取特定应用程序路由?

huangapple go评论82阅读模式
英文:

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.

https://stackoverflow.com/questions/58133490/get-application-domain-port-and-path-programmatically-in-spring

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

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/env/Environment.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

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/env/Environment.html

huangapple
  • 本文由 发表于 2020年10月27日 02:20:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64542853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定