0

I am making a rest service application with JAX-RS. Its for some project for school. For this project I need to use follow techniques: • Maven • JAX-RS • CDI • JPA - EJB • JNDI • Bean Validation

So now I already maded my domain "Cafes" with a Fake DB ("CafeStub") and a real DB using JPA ("CafeDB"). My domain also makes a little usage of CDI. (@Inject in the CafeService class ...)

Non I wanted to create my rest service, using JAX-RS. This worked fine: enter image description here

My problem is when I try to use CDI again it fails and it gives an 500 exception, NullPointerException, "Severe: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container" Full Stacktrace: enter image description here

I don't know how to fix this, already searched a long time .. Hopefully somebody can help me :s

This is my "CafeController" class. Producing the rest service

Path("/cafes")
public class CafeController {

@Inject
private CafeFacade cafeFacade;

public CafeController() {
   //this.cafeFacade = new CafeService();
}

@GET
@Produces("application/json")
public Response getCafes(){
    try{
        // test ........ 
        ObjectMapper mapper = new ObjectMapper();
        Cafe cafe = cafeFacade.getCafe(new Long(1));
        String jsonInString = mapper.writeValueAsString(cafe);
        return Response.status(200).entity(jsonInString).build();
    }catch (JsonProcessingException jsonEx) {
        System.out.println("Json Exception");
        System.out.println(jsonEx.getMessage());
        return null;
    }
}

This one is the "CafeService" class, the one who implemented "CafeFacade"

public class CafeService implements CafeFacade {

@Inject
private CafeRepository cafeRepository;

public CafeService() {
    //cafeRepository = new CafeStub();
    //cafeRepository = new CafeDB("CafesPU");
}

@Override
public long addCafe(Cafe cafe) {
    return this.cafeRepository.addCafe(cafe);
}

@Override
public Cafe getCafe(long cafeID) {
    return this.cafeRepository.getCafe(cafeID);
}

Her you see the "CafeStub" class, the one who implemented "CafeRepository"

public class CafeStub implements CafeRepository {

private static Map<Long, Cafe> cafes;
private static long counter = 0;

public CafeStub() {
    cafes = new HashMap<Long, Cafe>();

    // adding some dara
    this.addSomeData();
}    

@Override
public long addCafe(Cafe cafe) {
    if(cafe == null){
        throw new DBException("No cafe given");
    }
    counter++;
    cafe.setCafeID(counter);
    cafes.put(cafe.getCafeID(), cafe);
    return cafe.getCafeID();
}

@Override
public Cafe getCafe(long cafeID) {
    if(cafeID < 0){
        throw new DBException("No correct cafeID given");
    }
    if(!cafes.containsKey(cafeID)){
        throw new DBException("No cafe was found");
    }
    return cafes.get(cafeID);
}

At least here you can see my pom.xml (dependencies from CafeService project) - web.xml (from CafeService project) and project structure ...

<dependencies>
    <dependency>
        <groupId>Cafes</groupId>
        <artifactId>Cafes</artifactId>
        <version>0.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.3</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>asm</groupId>
        <artifactId>asm</artifactId>
        <version>3.3.1</version>
    </dependency>    
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-bundle</artifactId>
        <version>1.19.4</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.19.4</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.19.4</version>
    </dependency>
</dependencies>

enter image description here

enter image description here

Thanks in advance ... Cheers Tom

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Tom
  • 85
  • 10

2 Answers2

1

A class annotated with just @Path does not mark the class as a CDI bean as it is not in the list of bean defining annotations in the CDI spec. Adding RequestScoped to the REST service marks it as a CDI bean so injection works as you've discovered.

This answer here lists the annotations which mark a class as a CDI bean. Is @javax.annotation.ManagedBean a CDI bean defining annotation?

leet java
  • 304
  • 1
  • 3
0

Solved .. RequestScoped did the trick.. Daimn searched so long for one annotation.

@RequestScoped
@Path("/cafes")
public class CafeController {

Still I don't understand why I need to use it. @RequestScoped : CDI instantiates and manages the bean -> I thought my bean.xml would have instantiates and manages the bean ?

Tom
  • 85
  • 10