JAXRS – 创建多个具有独立提供者的端点

huangapple go评论72阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年7月31日 16:41:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63188559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定