Jax-rs tomcat glasfish - nie możność injecotwania

0

probuje wczytac klasę ale dostaje to :
ktos wie o co chodzi? uruchamiam na tomcat ee

pre>javax.servlet.ServletException: A MultiException has 3 exceptions. They are:

  1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=InjectMe,parent=UserService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,652298842)
  2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.github.sbahmani.rest.UserRest.UserService errors were found
  3. java.lang.IllegalStateException: Unable to perform operation: resolve on com.github.sbahmani.rest.UserRest.UserService
//import javax.enterprise.context.ApplicationScoped;
//
//@ApplicationScoped
public class InjectMe {
    public void get () {
        System.out.println("hello");
    }
}

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

@Path("/user")
public class UserService {
    @Inject
    private InjectMe injectMe;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/users")
    public Response getAll() {
        try {
            injectMe.get();
            List<User> users = UserDAO.findAll();
            return Response.status(Response.Status.OK).entity(users).build();
        } catch(Exception e) {
            return Response.status(Response.Status.BAD_REQUEST).entity(e).build();
        }
    }

}

fragment pom.xml

  <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <junit.version>4.12</junit.version>
       <hibernate.version>5.3.6.Final</hibernate.version>
       <hibernate.validator.version>6.0.13.Final</hibernate.validator.version>
       <javax-el.version>3.0.1-b10</javax-el.version>
       <maven-compiler-plugin-version>3.7.0</maven-compiler-plugin-version>
       <jersey.version>2.27</jersey.version>
       <servlet-api.version>4.0.1</servlet-api.version>
   </properties>

       <dependency>
           <groupId>org.glassfish</groupId>
           <artifactId>javax.el</artifactId>
           <version>${javax-el.version}</version>
       </dependency>
       <dependency>
           <groupId>org.glassfish.jersey.containers</groupId>
           <artifactId>jersey-container-servlet</artifactId>
           <version>${jersey.version}</version>
       </dependency>
       <dependency>
           <groupId>org.glassfish.jersey.core</groupId>
           <artifactId>jersey-client</artifactId>
           <version>${jersey.version}</version>
       </dependency>
       <dependency>
           <groupId>org.glassfish.jersey.core</groupId>
           <artifactId>jersey-common</artifactId>
           <version>${jersey.version}</version>
       </dependency>
       <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>javax.servlet-api</artifactId>
           <version>${servlet-api.version}</version>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.glassfish.jersey.bundles</groupId>
           <artifactId>jaxrs-ri</artifactId>
           <version>2.26</version>
       </dependency>
       <dependency>
           <groupId>org.glassfish.jersey.core</groupId>
           <artifactId>jersey-server</artifactId>
           <version>2.26</version>
       </dependency>

       <dependency>

       <dependency>
           <groupId>javax.inject</groupId>
           <artifactId>javax.inject</artifactId>
           <version>1</version>
       </dependency>


0

https://github.com/apache/tomee/tree/master/examples/cdi-basic sugeruje że TomEE wymaga jakiegoś xmla żeby CDI zaczęło działać.

0

ale nie działa .. pie.... beansy

0

jak dodałem to :

package com.github.sbahmani.rest;

import com.github.sbahmani.rest.UserRest.InjectMe;
import com.github.sbahmani.rest.UserRest.InjectMe2;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;

public class JerseyResourceConfiguration extends ResourceConfig {

    public JerseyResourceConfiguration() {

        packages("com.github.sbahmani.rest");

        register(new AbstractBinder() {
            @Override
            protected void configure() {
                bind(new InjectMe()).to(InjectMe.class);
                bind(new InjectMe2()).to(InjectMe2.class);
            }
        });
    }
}

to działa ....ale trzeba każdą klasę do injectu tutaj wstawić..o m g

0

teraz jak dodam @RequestScoped nad klasą InjectMe to wysyłaja z postmana 2 razy pod rząd reuqesta na ten sam adres. to w konsoli powinienem otrzymać inny adres obiektu? bo dostaje ten sam

@Path("/user")
public class UserService {
    @Inject
    private InjectMe injectMe;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/users")
    public Response getAll() {
        try {
            System.out.println(injectMe);
            injectMe.get();
           }
    }

0

adres obiektu xD a kto ci nagadał takich głupot że hashCode obiektu ma cokolwiek wspólnego z jakimś adresem? Zresztą scope określa ci tylko że dany obiekt jest w danej chwili używany tylko przez jeden wątek i zasięg widoczności jego thread local to request. Nie ma żadnego powodu żeby to był nowy obiekt tworzony za każdym razem, równie dobrze może być ich pula, szczególnie jak ten obiekt nie ma żadnych pól.

1

Ja bym wystartował z jakiegoś githuba i hello worlda który działa "od kopa" bo to co robisz na pewno jest źle. Przecież inny serwer aplikacyjny będzie miał innego dostawcę CDI, jakiś Weld czy Guice (czy nawet Spring) i to powinno działać "automatycznie" niezależnie od tego i jakieś importy z org.glassfish. są raczej nie na miejscu.

0

no nie wiem czy żle, jak teraz patrze na internecie to każy tutorial w jersey robi to w ten sposób bind(InjectMe.class).to(InjectMe.class).in(RequestScoped.class);
nie wiem to jest chore

0

Niezarejestrowałeś kontrolera jako ejb beana dodaj adonatacje @stateless nad kontrollerem a InjectMe jako komponent CDI @ApplicationScoped. I usuń to ręczne bindowanie

1

Po pierwsze na czym jest ta aplikacja odpalana? GlassFish? WildFly? Tomcat? Pierwsze dwa są pełnymi serwerami aplikacyjnymi, na których jeśli postępuje się wg dokumentacji JavaEE, to powinno działać, Tomcat tylko obsługuje servlety.
Po drugie wyrzuciłbym wszystkie zależności z pom.xml i wrzuciłbym tylko to:

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>8.0</version>
    <scope>provided</scope>
</dependency>

W skrócie, nie po to odpala się aplikacje na serwerze aplikacyjnym, aby wrzucać na niego jakieś zależności, które on obsługuje, nie wozi się drewna do lasu.
O ile Twój projekt jest <packaging>war</packaging>, to w katalogu src/main/webapp/WEB-INF powinieneś umieścić plik beans.xml, najlepiej o takiej zawartości:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
       version="2.0">
</beans>
0

tomcat ++ plumee. Ale nie mogę wyrzucić bo przecież projekt jest oparty na implemntacji jersey...w beansach mam to ustawione. Ale wygląda na to że w jeresy jest tak że musi się to dodać do bindera, a jak wyłaczyć jersry cdi a właczyć jakiś inny to nie umiem jeszcze.

1 użytkowników online, w tym zalogowanych: 0, gości: 1