2014-12-10 6 views
1

을 URLPATTERN 얻을 나는 내 web.xml을 내가 JAX-RS의 정의가 :아파치 윙크 programaticaly

<servlet> 
    <servlet-name>JAX-RS Servlet</servlet-name> 
    <servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class> 
    <init-param> 
     <param-name>applicationConfigLocation</param-name> 
     <param-value>/WEB-INF/resources.properties</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>JAX-RS Servlet</servlet-name> 
    <url-pattern>/api/*</url-pattern> 
</servlet-mapping> 

가 어떻게 API를 URLPATTERN programaticaly 코드를받을 수 있나요?

+0

것은 우리입니다 Application 서브 클래스의 정규화 된 클래스 이름이됩니다 이미 web.xml에서 경로를 정의하고 있지만 코드의 다른 부분에서 얻고 싶습니다. 그러므로 이것이 가능한지 물었습니다. thx –

답변

1

@Context 주석을 사용하여 ServletContext을 리소스 클래스 (또는 공급자)에 삽입 할 수 있습니다. 여기에서 ServletContext에서 ServletRegistration을 얻을 수 있습니다. ServletRegistration는 예를

@Path("/test") 
public class TestResource { 

    @Context 
    ServletContext context; 

    @GET 
    public Response getTest() { 
     ServletRegistration registration = 
       context.getServletRegistration("JAX-RS Servlet"); 
               // ^^^ Your servlet name 
     Collection<String> mappings = registration.getMappings(); 
     for (String mapping: mappings) { 
      System.out.println("Url Pattern: " + mapping); 
     } 
     ... 
    } 
} 

이 패턴 /api/*를 출력한다 위해 <url-pattern>

을 얻을 수 getMappings 방법이있다. 그냥 완전성에 대한


: web.xml을 사용할 및 Application 서브 클래스를 사용하지 않는 사람들을 위해는, 서블릿의 이름

+0

덕분에,이 작품! 그러나 JSP에서는 HttpServletRequest에서 ServletContext를 가져와야했습니다. –