如何使用Jersey 2.5将参数传递给REST资源 [英] How to pass parameters to REST resource using Jersey 2.5

查看:134
本文介绍了如何使用Jersey 2.5将参数传递给REST资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个为我的客户服务的Java服务器(非应用服务器)。

I have a Java server which serves my clients (Not application server).

现在我有兴趣添加REST支持。我初始化了Jetty服务器并创建了一些REST资源。

Now I'm interested to add REST support. I've initialized a Jetty server and created few REST resources.

我的问题是:如何在创建REST资源时传递参数?

通常我更喜欢每个资源的构造函数,但我不控制它。

Normally I would prefer in the constructor of each resource, but I don't control it.

我知道有一种方法可以注入依赖项。如何使用Jersey 2.5 ??

I understand there is a way to inject dependencies. How to do it using Jersey 2.5??

谢谢!

推荐答案

定义你的应用程序

public class MyApplication extends ResourceConfig {
  public MyApplication() {
    register(new FacadeBinder());
    register(JacksonFeature.class);
    register(MyEndpoint.class);
}

配置注射

public class FacadeBinder extends AbstractBinder {

  @Override
  protected void configure() {
    bind(MyManager.class).to(MyManager.class);
  }
}

在端点中注入已配置的类

Inject configured classes in your endpoint

@Path("/jersey")
public class MyEndpoint {
  @Inject
  MyManager myManager;
  ...
}

这篇关于如何使用Jersey 2.5将参数传递给REST资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆