Basic business level jersey dependency injection

I am working on a project with RESTful services. I have modules like web layer, business layer and so on. I added a base api layer (using knitwear) and I get a basic response to receive the request. Now I have to connect it to the business layer. I was googling, but I'm not sure how to implement each solution for my project.

This is my class of travel resources:

@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 to add resource 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:

 <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 execute the answer in this link . And I added:

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

and now I'm stuck.

How to add this binder to my configuration class? What is the easiest implementation without using any other technology?

+6
source share
2 answers

it also cost me a lot of time.

Try the following:

Here you should add a binding to your business logic. You have it 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 the "ResourceConfig" class to your project and register your binder, as here: http://sleeplessinslc.blogspot.de/2012/10/jax-rs-20-jersey-20-preview-example.html

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

e. g.

 /** * Application config */ public class ApplicationConfig extends ResourceConfig { public ApplicationConfig() { register(SomeResources.class, SomeProviders.class); // Register different Binders addBinders(new MyBinder()); } } 

At the very least, make sure your web.xml uses the configuration. It depends on your setup (glassfish, servlet v1 / v2, etc.)

As you are already using the ApplicationConfig class, there is a good chance that you are already using the correct settings.

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 helps;)

Relationship ben


Found a similar post now: Enabling Dependencies with Jersey 2.0

+9
source

You need to register your resource class in Jersey. Therefore, if your application has the name MyApplication , you can do

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

Also in the web.xml add MyApplication to the servlet container:

 <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 .

-6
source

Source: https://habr.com/ru/post/959265/


All Articles