英文:
JAXRS - creating multiple endpoints with separate providers
问题
Sure, here's the translated code portion you requested:
我想创建两个单独的端点,使用不同的提供者。
所以,如果我只注册一个端点,这将正常工作:
@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();
}
现在我想添加第二个服务,但它不应该使用MyCustomProviders.getProviders()。
我还没有找到如何添加第二个Bean的方法(我认为这是错误的方法),或者查看JAXRSServerFactoryBean,我还没有找到一种可以指定哪些提供者应该在哪些Bean上运行的方法。
所以类似这样的:
@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()); // 如何仅为MyService指定此项?
return serverFactory.create();
}
我正在使用org.apache.cxf:cxf-rt-frontend-jaxrs:3.1.4。
我更喜欢以编程方式完成。
有什么想法?
Please note that I have translated the code, but if you have specific questions or need further assistance with it, feel free to ask.
英文:
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?
答案1
得分: 1
If you're okay with separating them on different base addresses you should be able to do something like this:
如果您同意将它们分开放在不同的基本地址上,您应该可以像这样做:
英文:
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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论