12

データ サービスの一部を、jersey-spring を使用する Jersey 1.x から jersey-spring3 を使用する Jersey 2.x に移行しています。

JerseyTest を継承するいくつかのテスト クラスがあります。これらのクラスの一部は、web.xml ファイルで指定されていないカスタム applicationContext.xml ファイルを使用します。

Jersey 1.x では、JerseyTest を拡張したテスト クラスは、WebappDescriptor.Builder を使用してスーパー コンストラクターを呼び出すことができ、コンテキスト パラメーターを渡してアプリケーション コンテキスト パスを設定またはオーバーライドすることができました。

例えば

public MyTestClassThatExtendsJerseyTest()
{
    super(new WebAppDescriptor.Builder("com.helloworld")
    .contextParam( "contextConfigLocation", "classpath:helloContext.xml")
    .servletClass(SpringServlet.class)
    .contextListenerClass(ContextLoaderListener.class)
    .requestListenerClass(RequestContextListener.class).build());
}

Jersey 2.xで同じことを達成するにはどうすればよいですか?

API ドキュメントユーザー ガイド、および一部のソースをくまなく調べましたが、答えが見つかりませんでした。

4

2 に答える 2

8

私は .xml スタイルの構成を使用しておらず、@Configuration注釈を使用していたため、これはうまくいきませんでした。そのため、アプリケーション コンテキストを ResourceConfig クラスに直接提供する必要がありました。

次のように、JerseyTest で configure メソッドを定義しました。

@Override
protected Application configure() {
  ResourceConfig rc = new ResourceConfig();

  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
  rc.property("contextConfig", ctx);
}

SpringConfig.class は、@Configurationアノテーションとインポートを含む私のクラスですorg.springframework.context.annotation.AnnotationConfigApplicationContext

于 2014-07-03T11:15:13.083 に答える
7

Applicationあなたが次のように見えると仮定しましょう:

@ApplicationPath("/")
public class MyApplication extends ResourceConfig {

    /**
     * Register JAX-RS application components.
     */
    public MyApplication () {
        // Register RequestContextFilter from Spring integration module. 
        register(RequestContextFilter.class);

        // Register JAX-RS root resource.
        register(JerseySpringResource.class);
    }
}

JAX-RS ルート リソースは次のようになります。

@Path("spring-hello")
public class JerseySpringResource {

    @Autowired
    private GreetingService greetingService;

    @Inject
    private DateTimeService timeService;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getHello() {
        return String.format("%s: %s", timeService.getDateTime(), greetingService.greet("World"));
    }
}

helloContext.xmlまた、クラスパスから直接利用できるという名前のスプリング記述子があります。getHello次に、Jersey Test Framework を使用してリソース メソッドをテストします。次のようにテストを記述できます。

public class JerseySpringResourceTest extends JerseyTest {

    @Override
    protected Application configure() {
        // Enable logging.
        enable(TestProperties.LOG_TRAFFIC);
        enable(TestProperties.DUMP_ENTITY);

        // Create an instance of MyApplication ...
        return new MyApplication()
                // ... and pass "contextConfigLocation" property to Spring integration.
                .property("contextConfigLocation", "classpath:helloContext.xml");
    }

    @Test
    public void testJerseyResource() {
        // Make a better test method than simply outputting the result.
        System.out.println(target("spring-hello").request().get(String.class));
    }
}
于 2013-08-16T19:14:45.500 に答える