2013-08-16 46 views
11

我們正在使用jersey-spring3將Jersey的一些數據服務從Jersey 1.x遷移到Jersey 2.x。指定自定義應用上下文

我們有幾個從JerseyTest繼承的測試類。其中一些類使用未在web.xml文件中指定的自定義applicationContext.xml文件。

在Jersey 1.x中,擴展JerseyTest的測試類可以使用WebappDescriptor.Builder調用超級構造函數,可以傳遞上下文參數以設置或覆蓋應用程序上下文路徑。

E.g.

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

澤西2.x怎麼能實現呢?

我已經梳理了API docs,user guides和一些sources,但無法找到答案。

謝謝。

回答

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可直接。現在您要使用Jersey Test Framework來測試您的getHello資源方法。你可以寫你的測試,如:

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)); 
    } 
} 
+0

我在API中發現了'property'方法文檔,但我不清楚該方法可以應用於上下文參數。根據你的信息豐富的例子,我已經更新了我的代碼。從我的輸出日誌和測試中看來,正在調用正確的應用程序上下文文件。非常感謝您的幫助。現在我還有一個關於如何從正在運行的配置中檢索bean實例的問題。我應該創建一個新的問題嗎? –

+0

那麼,如果這個問題得到回答,那麼是的,創建一個新的問題。謝謝。 –

+0

謝謝。我發佈了一個關於問題的問題。 [使用jersey-spring3從JerseyTest容器中檢索托管bean](http://stackoverflow.com/questions/18282409/retrieve-a-managed-bean-from-a-jerseytest-container-with-jersey-spring3)。我在這裏連接它,因爲它可能有益於人們閱讀這個答案。 –

8

因爲我沒有使用.XML風格的配置,我用@Configuration註釋這並沒有爲我工作。所以我不得不直接向ResourceConfig類提供應用程序上下文。

我在JerseyTest定義的配置方法如下所示:

@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