问题 指定定制应用上下文


我们正在使用泽西弹簧3将泽西1.x的一些数据服务从泽西1.x迁移到泽西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文档用户指南 和一些 来源 但无法找到答案。

谢谢。


8065
2017-08-16 17:13


起源



答案:


让我们假设你的 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));
    }
}

7
2017-08-16 19:14



我发现了 property API文档中的方法,但我不清楚该方法可以应用于上下文参数。我已根据您的信息丰富的示例更新了我的代码。从我的输出日志和测试中可以看出,现在正在调用正确的应用程序上下文文件。非常感谢您的帮助。我现在还有一个关于如何从正在运行的配置中检索bean实例的问题。我应该创建一个新问题吗? - robert.andre.brink
好吧,如果这个问题得到了回答,那么请创建一个新问题。谢谢。 - Michal Gajdos
谢谢。我发布了一个跟进问题。 使用jersey-spring3从JerseyTest容器中检索托管bean。我在这里链接它,因为它可能对阅读这个答案的人有益。 - robert.andre.brink


答案:


让我们假设你的 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));
    }
}

7
2017-08-16 19:14



我发现了 property API文档中的方法,但我不清楚该方法可以应用于上下文参数。我已根据您的信息丰富的示例更新了我的代码。从我的输出日志和测试中可以看出,现在正在调用正确的应用程序上下文文件。非常感谢您的帮助。我现在还有一个关于如何从正在运行的配置中检索bean实例的问题。我应该创建一个新问题吗? - robert.andre.brink
好吧,如果这个问题得到了回答,那么请创建一个新问题。谢谢。 - Michal Gajdos
谢谢。我发布了一个跟进问题。 使用jersey-spring3从JerseyTest容器中检索托管bean。我在这里链接它,因为它可能对阅读这个答案的人有益。 - robert.andre.brink


这对我不起作用,因为我没有使用.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


8
2017-07-03 11:15