0

I use JAX-RS to build a REST API. To bootstrap all resources I have a overridden an "Application":

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationScoped
@ApplicationPath("/")
open class ApiConfig : Application() {
    override fun getSingletons(): MutableSet<Any> {
        println("----- init jaxrs -----")
        return mutableSetOf(EchoResource())
    }
}

As you can see I register the EchoResource() with brackets. It does not work when I use EchoResource::class.

My Problem is, that I want to inject some service into EchoResource:

import dev.elysion.mail.smtp.MailService
import javax.enterprise.context.RequestScoped
import javax.inject.Inject
import javax.ws.rs.GET
import javax.ws.rs.Path

@Path("/echo")
@RequestScoped
class EchoResource @Inject constructor(private val mailService: MailService) {

    @GET
    fun getHello(): String {
        return "Hello World"
    }
}

When I add the constructor I get an error in API Config saying that I do not pass a parameter for MailService.

In Java I would register the resource with EchoResource.class which does not care about any parameters.

How can I achieve the same with Kotlin?

Mrvonwyl
  • 347
  • 2
  • 15

1 Answers1

0

It works if getClasses() is used instead of getSingletons():

import javax.enterprise.context.ApplicationScoped
import javax.ws.rs.ApplicationPath
import javax.ws.rs.core.Application

@ApplicationScoped
@ApplicationPath("/")
open class ApiConfig : Application() {
    override fun getClasses(): MutableSet<Class<*>> {
        return mutableSetOf(EchoResource::class.java)
    }
}

Furthermore, all resources and all of their methods need to be open (not final) in order to be proxyable for CDI.

Mrvonwyl
  • 347
  • 2
  • 15