orderlyfashion
orderlyfashion

Reputation: 613

JAXRS - creating multiple endpoints with separate providers

I would like to create two separate endpoints, with different providers.

So if I just register one endpoint this works fine:

@Bean
public Server rsServer(MyService myService) {
    JAXRSServerFactoryBean serverFactory = new JAXRSServerFactoryBean();
    serverFactory.setServiceBean(myService);
    serverFactory.setAddress("/");
    serverFactory.setBus(new SpringBus());
    serverFactory.setProviders(MyCustomProviders.getProviders());
    return serverFactory.create();
}

Now I would like to add a second service to this, but it should not use MyCustomProviders.getProviders().

I haven't been able to figure out how I could add a second Bean (which I think is the wrong way), or looking at JAXRSServerFactoryBean I haven't found a way where I can specify which providers should operate on which beans.

So something like this:

@Bean
public Server rsServer(MyService myService, MyOtherService myOtherService) {
    JAXRSServerFactoryBean serverFactory = new JAXRSServerFactoryBean();
    serverFactory.setServiceBean(List.of(myService, myOtherService));
    serverFactory.setAddress("/");
    serverFactory.setBus(new SpringBus());
    serverFactory.setProviders(MyCustomProviders.getProviders()); // How do I specify this only for MyService?
    return serverFactory.create();
}

I'm using with org.apache.cxf:cxf-rt-frontend-jaxrs:3.1.4.

I'd prefer if I could do it programatically.

Any ideas?

Upvotes: 1

Views: 619

Answers (1)

dropbear
dropbear

Reputation: 1727

If you're okay with separating them on different base addresses you should be able to do something like this:

@Bean
public Server createMyServiceServer(MyService myService) {
    JAXRSServerFactoryBean serverFactory = new JAXRSServerFactoryBean();
    serverFactory.setServiceBean(myService);
    serverFactory.setAddress("/");
    serverFactory.setBus(new SpringBus());
    serverFactory.setProviders(MyCustomProviders.getProviders());
    return serverFactory.create();
}

@Bean
public Server createMyOtherServiceServer(MyOtherService myOtherService) {
    JAXRSServerFactoryBean serverFactory = new JAXRSServerFactoryBean();
    serverFactory.setServiceBean(myOtherService);
    serverFactory.setAddress("/otherservice");
    serverFactory.setBus(new SpringBus());
    return serverFactory.create();
}

Upvotes: 1

Related Questions