41

I would like to be able to dynamically retrieve the "servlet context path" (e.g. http://localhost/myapp or http://www.mysite.com) for my spring web application from a Service spring bean.

The reason for this is that I want to use this value in email that are going to be sent to users of the website.

While it would be pretty easy to do this from a Spring MVC controller, it is not so obvious to do this from a Service bean.

Can anyone please advise?

EDIT: Additional requirement:

I was wondering if there wasn't a way of retrieving the context path upon startup of the application and having it available for retrieval at all time by all my services?

balteo
  • 23,602
  • 63
  • 219
  • 412

5 Answers5

60

If you use a ServletContainer >= 2.5 you can use the following code to get the ContextPath:

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component

@Component
public class SpringBean {

    @Autowired
    private ServletContext servletContext;

    @PostConstruct
    public void showIt() {
        System.out.println(servletContext.getContextPath());
    }
}
Andreas Berger
  • 843
  • 6
  • 11
31

As Andreas suggested, you can use the ServletContext. I use it like this to get the property in my components:

    @Value("#{servletContext.contextPath}")
    private String servletContextPath;
stuchl4n3k
  • 588
  • 5
  • 14
6

I would avoid creating a dependency on the web layer from your service layer. Get your controller to resolve the path using request.getRequestURL() and pass this directly to the service:

String path = request.getRequestURL().toString();
myService.doSomethingIncludingEmail(..., path, ...);
Rich Cowin
  • 668
  • 1
  • 8
  • 17
  • Thanks Rich! I was wondering if there wasn't a way of retrieving the context path **upon startup of the application** and having it available for retrieval at all time by all my services? – balteo Sep 02 '12 at 19:11
  • You could set the path in the configuration of the service. Though I can't see how you can retrieve this automatically from the servlet configuration. – Rich Cowin Sep 02 '12 at 21:02
  • Yes. Hummmm... But how do I retrieve it upon startup of the application? – balteo Sep 02 '12 at 21:05
  • No idea sorry, I think only the servlet container would know this value. – Rich Cowin Sep 02 '12 at 21:12
  • OK. I just wanted this value available before any user httprequest is done. I'll research the matter and post here accordingly. – balteo Sep 02 '12 at 22:02
2

If the service is triggered by a controller, which I am assuming it is you can retrieve the path using HttpSerlvetRequest from the controller and pass the full path to the service.

If it is part of the UI flow, you can actually inject in HttpServletRequest in any layer, it works because if you inject in HttpServletRequest, Spring actually injects a proxy which delegates to the actual HttpServletRequest (by keeping a reference in a ThreadLocal).

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;

public class AServiceImpl implements AService{

 @Autowired private HttpServletRequest httpServletRequest;


 public String getAttribute(String name) {
  return (String)this.httpServletRequest.getAttribute(name);
 }
}
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • How do you `@Autowire` in a `HttpServletRequest` to a service? Surely the request is not available at this point? – Rich Cowin Sep 02 '12 at 18:39
  • Yes, you are right, that is the reason why I indicated that this has to be part of a UI flow - some request from the user to controller to this service triggering an email, if that is not the case yes then this will not work. The best I can then think of is to get the context path of the webapp using HttpServletContext injected by declaring your service as ServletContextAware, but the URL will have to be based off a configuration file – Biju Kunjummen Sep 02 '12 at 22:19
1

With Spring Boot, you can configure the context-path in application.properties:

server.servlet.context-path=/api

You can then get the path from a Service or Controller like this:

import org.springframework.beans.factory.annotation.Value;

@Value("${server.servlet.context-path}")
private String contextPath;
yglodt
  • 13,807
  • 14
  • 91
  • 127