Specify Custom Application Context

南笙酒味 提交于 2019-12-05 14:51:21

问题


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

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.

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.

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

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

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

Thank you.


回答1:


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

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

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



回答2:


This didn't work for me as I was not using the .xml style configuration, I was using @Configuration annotations. So I had to directly provide the application context to the ResourceConfig class.

I defined the configure method in my JerseyTest like so:

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

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

where SpringConfig.class is my class with the @Configuration annotation and importing org.springframework.context.annotation.AnnotationConfigApplicationContext



来源:https://stackoverflow.com/questions/18278769/specify-custom-application-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!