2017-02-01 7 views
0

JBoss EAP 6.2にJAX-RSアプリケーションをデプロイします。 ServletContextをJAX-RSリソースクラスの内部から取得しようとしています。WEB-INF/web.xmlファイルに設定したcontext-paramの値を読み取ることができます。 私はServletContextを手に入れた後、ServletContext#getInitParamに電話をかけて値を取得する予定だった。 注射を使用してServletContexthereとしてアドバイスしています。RESTEasy JAX-RSアプリケーションで@ContextがServletContextを注入しない

web.xmlの関連部分は次のとおりです。

<servlet> 
    <servlet-name>resteasy-servlet</servlet-name> 
    <servlet-class> 
     org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher 
    </servlet-class> 
    <init-param> 
     <param-name>javax.ws.rs.Application</param-name> 
     <param-value>foo.MyApplication</param-value> 
    </init-param> 
</servlet> 
<servlet-mapping> 
    <servlet-name>resteasy-servlet</servlet-name> 
    <url-pattern>/jax-rs/*</url-pattern> 
</servlet-mapping> 

だから私は、JBossに同梱されたRESTEasyを使用しています。

クラスMyApplicationは次のとおりです。

public class MyApplication extends Application { 

    private Set<Object> singletons = new HashSet<>(); 

    public MyApplication() { 
     singletons.add(new MyResource()); 
    } 

    @Override 
    public Set<Object> getSingletons() { 
     return singletons; 
    } 
} 

&hellip;そして最後に、クラスMyResourceに、私は次のようしている:

@Path(...) 
public class MyResource { 

    @Context 
    ServletContext context; 

    public MyResource() { 
     // I understand context is supposed to be null here 
    } 

    // ... but it should have been injected by the time we reach the service method. 
    @Path("/somePath") 
    @GET 
    @Produces(MediaType.APPLICATION_JSON) 
    public Response someMethod() { 
     if (context==null) throw new RuntimeException(); 
     ... 
    } 
} 

を上記のコードは、常にスローされRuntimeExceptionになります。私。 RESTEasyはどうにかしてServletContextを注入できません。他のJAX-RSの問題はありません。私。私は `のServletContext#getInitParameter」を介して取得することができるように期待していたcontext-param値をハードコーディングした場合に予想されるようにWARをJBossへデプロイされたとき、その後、JAX-RS残りの機能が動作します。

さらに私はServletContextことを発見試します私はこのようなサービスのメソッドの引数で注入を実行した場合にのみ注入される:

@Path("/somePath") 
@GET 
@Produces(MediaType.APPLICATION_JSON) 
public Response someMethod(@Context ServletContext servletContext) { 
    ... 
} 

&hellip;しかし、私はAPIを変更しないことを好むだろう。また、私はに基づいて、いくつかの高価な初期化を実行したいと思います。 context-param値は1回であり、すべてのサービスメソッド呼び出しではありません。

私の質問は以下のとおりです。

  1. 理由注入が失敗していますか?
  2. 私は、実行時に失敗するアノテーションの魔法に少し疲れました。アノテーションを使わずにServletContextを取得する方法はありますか?
  3. また、MyApplicationクラスはServletContextを取得し、それをコンストラクタパラメータとしてMyResourceクラスに渡すことはできますか?
  4. 他のすべてがうまくいかない場合、私は常にClass#getResourceAsStreamを使ってweb.xmlファイルを読み込んで解析できますか?
+0

http://stackoverflow.com/questions/1814517/get-servletcontext-in-jax-rs-resource?noredirect=1&lq=1 – gladiator

+0

@gladiator私の質問では、その回答に具体的にリンクしています。オープニングパラグラフ) –

+1

これをチェック:http://stackoverflow.com/questions/5434667/rest-easy-and-init-params-how-to-access – FrAn

答えて

0

this answerにリンクフランのコメントに基づいて、これは私がやってしまったものです:

public class JaxRsApplication extends Application { 

    private Set<Object> singletons = new HashSet<>(); 

    public JaxRsApplication(@Context ServletContext servletContext) { 
     Assert.assertNotNull(servletContext); 
     singletons.add(new UserDatabaseResource(servletContext)); 
    } 

    @Override 
    public Set<Object> getSingletons() { 
     return singletons; 
    } 
} 

&hellip;そして、UserDatabaseResourceクラスで私は、次のしている:私のDAL層であるUserDatabaseResourceクラスはシングルトンであると私はちょうどから(使用するデータソースのJNDI名を取得するために、必要に応じて

public UserDatabaseResource(ServletContext servletContext) { 
    Assert.assertNotNull(servletContext); 
    ... 
    String jndiNameForDatasource = servletContext.getInitParameter("whatever")) ; 
    ... 
} 

これは動作しますweb.xmlファイル)。しかし、おそらくこのアプローチは非シングルトンクラスに対しても若干の調整を加えて動作します。

関連する問題