Get server name from PostConstruct method in a Spring controller

六眼飞鱼酱① 提交于 2020-01-15 03:22:08

问题


If I had an instance of HttpServletRequest, I could do request.getServerName() to get the server name. However, during the post initialization of the controller beans, I have no instance of HttpServletRequest.

@Controller
@RequestMapping(value = {"/data"})
public class DataController {
    @PostConstruct
    public void init() {
        // how to get server name?
    }
}

How do I get the server name in this case?


回答1:


There are no good ways to do it.

Generally speaking, server doesn't know the name that can be used to access it from the outside. Actually, HttpServletRequest.getServerName() isn't a solution either, because it returns a name used to send that particular request, not the canonical name you want your server to be known under.

The typical solution is to configure the canonical server name for a particular deployment in a property file, etc.



来源:https://stackoverflow.com/questions/8343634/get-server-name-from-postconstruct-method-in-a-spring-controller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!