6
  • RESTEasy 2.0.1GA
  • Java 1.6
  • 春 3.0.3

私はできる限りのことを試しましたが、何が起こっているのかを理解することはできません. 私は Spring MVC アプリケーションを持っていますが、Spring MVC アプリの外部で使用できるいくつかの RESTEasy エンドポイントを同じコンテナー内で使用して、最終的に同じ Bean に接続できるようにしたいと考えています。

最初のステップとして、コンテナー内で RESTEasy を立ち上げ、Spring 構成の Bean からの要求を処理しようとしています。説明書のボイラープレートを試してみましたが、手動セットアップも試しましたが、役に立ちませんでした。

@Resource
@Path("/")
public class NeighborComparison {

    private String foo;

    @GET @Path(value="customer") @Produces("text/plain")
    public String getNeighborComparison() {
        return "foo";
    }
}

web.xml

<context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/api</param-value>
</context-param>

<listener>
    <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>

<!-- NOT configuring SpringContextLoaderListener because I declare my own, so if I do, everything
     blows up, plus  all it actually does is sanity check configuration -->
<listener>
    <listener-class>com.example.MyCustomContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>Resteasy</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Resteasy</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

applicationContext.xml

<bean id="resteasy.providerFactory" class="org.jboss.resteasy.spi.ResteasyProviderFactory"
      factory-method="getInstance">
</bean>

<bean id="resteasy.dispatcher" class="org.jboss.resteasy.core.SynchronousDispatcher">
    <constructor-arg ref="resteasy.providerFactory"/>
</bean>

<bean id="resteasy.spring.bean.processor" class="org.jboss.resteasy.plugins.spring.SpringBeanProcessor">
    <description>
        Add Resources and @Providers to the appropriate places
        in Resteasy's infrastructure
    </description>
    <constructor-arg ref="resteasy.dispatcher"/>
</bean>

<bean id="neighborComparison" class="opower.api.customer.neighbor_comparison.NeighborComparison">
</bean>

ドキュメントによると、「org.jboss.resteasy.plugins.spring.SpringBeanProcessor のインスタンスを割り当てて、RESTeasy BeanFactoryPostProcessor を手動で登録する」だけで済みます。この春の構成がそれを行うと信じています。

Jetty が起動し、アプリ コンテキストが問題なくスピンアップします。アプリケーションは正常に動作しますが、

> curl -H"Accept: text/plain" localhost:8080/ei/api/customer

(「ei」はアプリケーション コンテキストです)。ログには次のように表示されます (これとこれのみ):

2011-03-29 16:44:24,153 DEBUG [qtp-575315405-0] [EI] [] [asy.core.SynchronousDispatcher] PathInfo: /customer
2011-03-29 16:44:24,156 DEBUG [qtp-575315405-0] [EI] [] [asy.core.SynchronousDispatcher] Failed executing GET /customer
org.jboss.resteasy.spi.NotFoundException: Could not find resource for relative : /customer of full path: http://localhost:8080/ei/api/customer

RESTEasy にマッピングを表示するよう説得できたとしても、Bean が検出されていないようです。

コンテキストパラメーターを介して明示的にマップするresteasy.resourcesと機能しますが、明らかに自動配線された Spring Bean にアクセスできません。

他に試せることはありますか?RESTEasy コードベース全体のデバッグ ログがありますが、メッセージが表示されません。また、Spring が実際に私の Bean を作成していることも確認したので、RESTEasy がそれを見つけられないだけです。

4

1 に答える 1

8

@Pathブートストラップ中に RESTeasy が取得できるようにするには、リソース クラスに注釈を付ける必要があります。

@Path("/customer")
@Resource
public class NeighborComparison {

    @GET @Path("/{customerId}") @Produces("text/plain")
    public String getNeighborComparison(@PathParam("customerId") long customerId) {
        return "foo";
    }
}

@Path("/{customerId}}パラメータが正しくマッピングされていないアノテーションに注意してください@PathParam。その結果、かなり詳細な例外が発生します (およびクライアント側で 500 応答が付随します)。もちろん、RESTeasy によってサービスが取得されると仮定します。

さらに、RESTeasy の を使用しない場合は、インスタンスが に登録されてSpringContextLoaderいることを確認する必要があります。RESTeasy は、 in を登録することでそれに委任します。SpringBeanProcessorApplicationContextApplicationListenerSpringContextLoader

  ApplicationListener listener = new ApplicationListener() {
     public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ContextRefreshedEvent) {
           ContextRefreshedEvent cre = (ContextRefreshedEvent) event;
           ConfigurableListableBeanFactory autowireCapableBeanFactory = (ConfigurableListableBeanFactory) cre
                 .getApplicationContext().getAutowireCapableBeanFactory();
           new SpringBeanProcessor(dispatcher, registry, providerFactory)
                 .postProcessBeanFactory(autowireCapableBeanFactory);
        }
     }
  };
  configurableWebApplicationContext.addApplicationListener(listener);

RESTEasy が提供するものではなく、カスタム コンテキスト ローダーを使用する場合、このコードはコンテキスト ローダーのどこかに表示され、すべてが接続されるようにする必要があります。少し複雑です、ええ。それはSpringBeanProcessor、すべての Spring Bean を通過@Pathし、階層 (型と対応するインターフェース) のどこかに注釈を持つものを RESTeasy に登録することです。

于 2011-03-29T21:50:55.667 に答える