Get server name from PostConstruct method in Spring controller

If I had an instance HttpServletRequest, I could do request.getServerName()to get the server name. However, during the post-initialization of the beans controller, I do not have an instance 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?

+2
source share
1 answer

There are no good ways to do this.

Generally speaking, the server does not know the name that can be used to access it from the outside. In fact, it is HttpServletRequest.getServerName()also not a solution, because it returns the name used to send this particular request, and not the canonical name in which you want your server to be known.

..

+4

All Articles