Web applications 如何将Spring数据与Jersey 2绑定?
在本项目中: 我已经能够通过以下方式将Jersey 2链接到Spring数据:Web applications 如何将Spring数据与Jersey 2绑定?,web-applications,spring-data,jersey-2.0,Web Applications,Spring Data,Jersey 2.0,在本项目中: 我已经能够通过以下方式将Jersey 2链接到Spring数据: public class MyApplication extends ResourceConfig { /** * Register JAX-RS application components. */ @Inject public MyApplication(ServiceLocator serviceLocator) { //ToDo: find a way to have Jer
public class MyApplication extends ResourceConfig {
/**
* Register JAX-RS application components.
*/
@Inject
public MyApplication(ServiceLocator serviceLocator) {
//ToDo: find a way to have Jersey better interface with Spring
//Pull bean out of Spring
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
Rest rest = ctx.getBean(Rest.class);
//Link pulled out Spring bean Jersey's DI framework
DynamicConfiguration dc = Injections.getConfiguration(serviceLocator);
Injections.addBinding(
Injections.newBinder(rest)
.to(Rest.class),
dc);
dc.commit();//Don't forget to commit
register(Rest.class); //Mark Rest class as an REST endpoint to Jersey
}
}
然而,它不是很有说服力。我是否可以让Web.xml加载Jersey 2和Spring,并让Spring bean自动映射到Jersey REST端点?如果有人将我的回购协议分支并分享,以演示如何做得更好,那就太棒了 Jersey 2附带了与Spring集成的扩展,请参见 这很好,我们的Spring组件通过现场注入直接自动连接到我们的资源中,如下所示:
@Component
@Path("/")
public class FooResource {
@Autowired
private SomeComponent someComponent;
protected FooResource() {}
@GET
public String bar() {
return "foobar";
}
}
在扩展ResourceConfig
的类中,只需注册(FooResource.class)!有关详细信息,请参阅文档和