Java 从业务层使用 jersey 进行基本依赖注入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20358668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:20:32  来源:igfitidea点击:

Basic dependency injection with jersey from business layer

javarestmavendependency-injectionjersey

提问by Libor Zapletal

I am working on a project with RESTful services. I have modules as web layer, business layer and so. I added basic api layer (using jersey) and I get basic response for get request. Now I must connect it to business layer. I was googling but I am not sure how to implement each solutions to my project.

我正在开发一个带有 RESTful 服务的项目。我有模块作为 web 层,业务层等等。我添加了基本的 api 层(使用球衣)并且我得到了 get 请求的基本响应。现在我必须将它连接到业务层。我在谷歌搜索,但我不确定如何为我的项目实施每个解决方案。

This is my resource class for trip:

这是我的旅行资源类:

@Path("trip")
public class TripResource {

    @Context
    private UriInfo context;
    @Inject
    private AdminService adminService;

    public TripResource() {
    }

    @GET
    @Produces("text/plain")
    public List<TripDTO> getText() {
        return adminService.listAllTrips();
    }

}

and this I use for adding resources classes:

这我用于添加资源类:

@javax.ws.rs.ApplicationPath("api")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
        addRestResourceClasses(resources);
        return resources;
    }
    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(cz.infi.javatravelagency.ServiceResource.class);
        resources.add(cz.infi.javatravelagency.TripResource.class);
    }
}

My pom.xml:

我的 pom.xml:

<name>JavaTravelAgency - Api module</name>
    <dependencies>
         <dependency>
            <groupId>cz.infi</groupId>
            <artifactId>javatravelagency-business</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <type>jar</type>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- Java language version -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>6</source>
                    <target>6</target>
                </configuration>
            </plugin>
            <!-- Servlet 3.0 without web.xml -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

I tried to follow answer in this link. And I just added:

我试图按照此链接中的答案进行操作。我刚刚补充说:

public class MyApplicationBinder extends AbstractBinder {
    @Override
    protected void configure() {
        bind(AdminServiceImpl.class).to(AdminService.class);
    }
}

and now I am stuck.

现在我被困住了。

How can I add this binder to my config class? What's the easiest implementation without using any other technology?

如何将此绑定器添加到我的配置类?在不使用任何其他技术的情况下,最简单的实现是什么?

采纳答案by Ben

this also cost me a lot of time.

这也花费了我很多时间。

Try the following:

请尝试以下操作:

  1. Add a HK2 Binder to your project as described here: https://jersey.java.net/documentation/latest/migration.html
  1. 将 HK2 Binder 添加到您的项目中,如下所述:https: //jersey.java.net/documentation/latest/migration.html

Here you have to add the binding to your business logic. You have this already (just added for completeness).

在这里,您必须将绑定添加到您的业务逻辑。你已经有了这个(只是为了完整性而添加)。

e. g.

例如

public class MyBinder extends AbstractBinder {

    @Override
    protected void configure() {
        // request scope binding
        bind(MyInjectablePerRequest.class)
                .to(MyInjectablePerRequest.class)
                .in(RequestScope.class);
        // singleton binding
        bind(MyInjectableSingleton.class).in(Singleton.class);
        // singleton instance binding
        bind(new MyInjectableSingleton()).to(MyInjectableSingleton.class);
    }
}

Then add a "ResourceConfig" class to your project and register your binder like here: http://sleeplessinslc.blogspot.de/2012/10/jax-rs-20-jersey-20-preview-example.html

然后在你的项目中添加一个“ResourceConfig”类并像这里一样注册你的活页夹:http: //sleeplessinslc.blogspot.de/2012/10/jax-rs-20-jersey-20-preview-example.html

In your case you could simply extend your ApplicationConfig from ResourceConfig instead of ApplicationConfig (this is what I did). All classes registered in "getClasses()" should then be like described below.

在您的情况下,您可以简单地从 ResourceConfig 而不是 ApplicationConfig 扩展您的 ApplicationConfig (这就是我所做的)。在“getClasses()”中注册的所有类都应该如下所述。

e. g.

例如

/**
 * Application config
 */
public class ApplicationConfig extends ResourceConfig {

    public ApplicationConfig() {
        register(SomeResources.class, SomeProviders.class);

        // Register different Binders
        addBinders(new MyBinder());
    }
}

At least ensure that your web.xml uses the config. This depends on your setup (glassfish, servlet v1 / v2, etc.)

至少确保您的 web.xml 使用该配置。这取决于您的设置(glassfish、servlet v1 / v2 等)

As you're already using the ApplicationConfig class, chances are good, that you're using the correct settings already.

由于您已经在使用 ApplicationConfig 类,因此很有可能您已经在使用正确的设置。

Again here is an example:

这里再举一个例子:

<servlet>
    <servlet-name>om.example.package.to.your.config.ApplicationConfig</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.example.package.to.your.config.ApplicationConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Hope this will help ;)

希望这会有所帮助;)

Regards Ben

问候本



Found a similar post right now: Dependency injection with Jersey 2.0

现在找到了一个类似的帖子: Dependency injection with Jersey 2.0

回答by tonga

You need to register your resource class with Jersey. So if your application is named MyApplication, then you can do

您需要在 Jersey 中注册您的资源类。所以如果你的应用程序被命名MyApplication,那么你可以做

public class MyApplication extends ResourceConfig {

    /*Register JAX-RS application components.*/
    public MyApplication () {
        register(TripResource.class);
    }
}

Also in the web.xmlfile, add the MyApplicationto the servlet container:

同样在web.xml文件中,将 添加MyApplication到 servlet 容器:

<servlet>
    <servlet-name>SpringApplication</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>mypackage.MyApplication</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

A simple example of using Jersey with Spring DI can be found here.

可以在此处找到将 Jersey 与 Spring DI 结合使用的简单示例。