在 spring 中以编程方式获取应用程序域 + 端口和路径?

编程入门 行业动态 更新时间:2024-10-24 22:28:13
本文介绍了在 spring 中以编程方式获取应用程序域 + 端口和路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的网站调用了也在同一个应用程序上的休息服务.如果其余地址托管在不同的域名和/或端口上,我不想更改它的域.我想以编程方式获取域、端口和应用程序路径.

如果我们正在访问该网站

解决方案

希望我不会迟到.

我遇到了与您类似的问题,我设法使用 ServletUriComponentsBuilder 解决了您问题的第一部分.

我一直这样使用它来获取当前服务器 URL:

ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString();

这将为我返回 localhost:8080

我写的所有内容都不起作用,我正在处理与 OP 类似的问题,并且此修复程序部分对我有用,经过一番挖掘后,我发现了这个问题,该问题已得到解答:

是否可以从任务调用的 Spring 服务中获取 Web 应用程序的完整 url?

这家伙还说这是不可能的:如何在 Spring MVC 启动期间找到基本 URL?

My website calls a rest service which is also on the same application. I don't want to change the domain of the rest address if it's hosted on a different domain name and/or port. I want to get the domain, port and application path programmatically.

If we are accessing the website like

example/article1

then I should get example

or if our address is like below

example:8080/ArticleSite/article1

then I want to get example:8080/ArticleSite

How can this be done?

I did look at the answers at

What's the best way to get the current URL in Spring MVC?

but non are getting the application path.

解决方案

Hope im not late for the party.

I had issue similar to yours, and i managed to solve first part of your question by using ServletUriComponentsBuilder.

I've been using it like this to get current server URL:

ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString();

which would return localhost:8080 for me

EDIT: everything i wrote will not work, i'm working on a similar issue as OP, and this fix partially worked for me, after some digging i found this question, that has already been answered:

Is possible to get web application full url from Spring service that is invoked by task?

EDIT2: this guy also says it's not possible: How do I find base URL during Spring MVC startup?

更多推荐

在 spring 中以编程方式获取应用程序域 + 端口和路径?

本文发布于:2023-11-25 17:23:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630661.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中以   端口   应用程序   路径   方式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!