2013-08-16 6 views
11

jersey-spring을 사용하여 Jersey 1.x에서 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()); 
} 

저지 2.x에서도 어떻게 동일한 결과를 얻을 수 있습니까?

나는 API docs, user guidessources 중 일부를 빗질했지만 답변을 찾을 수 없습니다.

감사합니다.

답변

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 리소스 메소드를 테스트하려고합니다. 내가, 내가 @Configuration 주석을 사용했다 .XML 스타일의 구성을 사용하지 않은 것처럼이 나를 위해 작동하지 않았다

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

org.springframework.context.annotation.AnnotationConfigApplicationContext 내가 API에'property' 방법을 발견 오기 내 @Configuration 주석을 가진 클래스와 입니다

@Override protected Application configure() { ResourceConfig rc = new ResourceConfig(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); rc.property("contextConfig", ctx); } 

:

나는 그렇게처럼 내 JerseyTest의 구성 방법을 정의 문서화되었지만이 방법이 컨텍스트 매개 변수에 적용될 수 있다는 것은 분명하지 않았습니다. 귀하의 매우 유익한 예에 따라 코드를 업데이트했습니다. 내 출력 로그 및 테스트에서 올바른 응용 프로그램 컨텍스트 파일이 호출되고있는 것 같습니다. 당신의 도움을 주셔서 대단히 감사합니다. 실행중인 구성에서 빈 인스턴스를 검색하는 방법에 대한 추가 질문이 있습니다. 새로운 질문을 만들어야합니까? –

+0

글쎄,이 질문에 대한 답이 맞다면 예, 새로운 질문을 만드십시오. 고마워. –

+0

감사합니다. 나는 질문에 대한 후속편을 게시했다. [jersey-spring3이있는 JerseyTest 컨테이너에서 관리 빈 검색] (http://stackoverflow.com/questions/18282409/retrieve-a-managed-bean-from-a-jerseytest-container-with-jersey-spring3). 이 답변을 읽는 사람들에게 유익 할 수 있으므로 여기에 연결하고 있습니다. –

8

: 당신은 같은 테스트를 작성할 수 있습니다. 따라서 ResourceConfig 클래스에 응용 프로그램 컨텍스트를 직접 제공해야했습니다. SpringConfig.class이

관련 문제