指定定制应用上下文 [英] Specify Custom Application Context

查看:108
本文介绍了指定定制应用上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用jersey-spring将泽西1.x的一些数据服务迁移到泽西2.x.使用jersey-spring3。

We are migrating some of our data services from Jersey 1.x using jersey-spring to Jersey 2.x using jersey-spring3.

我们有一些测试从JerseyTest继承的类。其中一些类使用未在web.xml文件中指定的自定义applicationContext.xml文件。

We have a few test classes that inherit from JerseyTest. Some of these classes use custom applicationContext.xml files that are not specified in the web.xml file.

在Jersey 1.x中,扩展JerseyTest的测试类可以调用带有WebappDescriptor.Builder的超级构造函数,可以传递上下文参数来设置或覆盖应用程序上下文路径。

In Jersey 1.x the test classes that extended JerseyTest could call the super constructor with a WebappDescriptor.Builder to which a context parameter could be passed to set or override the application context path.

例如

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如何实现同样的目标?

How can the same be achieved with Jersey 2.x?

我已经梳理了 API文档用户指南和一些来源但无法找到答案。

I have combed through the API docs, user guides and some of the sources but was unable to find an answer.

谢谢。

推荐答案

让我们假设您的应用程序如下所示:

Lets assume your Application looks like:

@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根资源如:

Your JAX-RS root resource like:

@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 资源方法。您可以编写如下测试:

And you have spring descriptor named helloContext.xml available directly from your class-path. Now you want to test your getHello resource method using Jersey Test Framework. You can write your test like:

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

这篇关于指定定制应用上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆