CLOVER🍀

That was when it all began.

KotlinでJAX-RSのコンストラクタインジェクションがうまくいかないという話について

昨日、このようなエントリを書きました。

ScalaJAX-RSとコンストラクタインジェクションと
http://d.hatena.ne.jp/Kazuhira/20140916/1410890181

元ネタは、こちら。

Kotlin + JAX-RS
http://backpaper0.github.io/ghosts/kotlin-jaxrs.html#/9
http://backpaper0.github.io/ghosts/kotlin-jaxrs.html#/10

元ネタとなった資料では、@Contextなフィールドをvalにしてコンストラクタインジェクションしようとすると、失敗するという話でした。

これに対して、Scalaで単なるコンストラクタに引数を取る版と、さらにvalを付与する版の両方を作成して動かしたのですが、普通に両方とも動作しました。が、単にコンストラクタ引数にする方では、デコンパイルした結果、引数にもフィールドにも@Contextが付いていました。

public class HelloResource
{
  @Context
  private final UriInfo uriInfo;

  public HelloResource(@Context UriInfo uriInfo) {}

でも、エラーにならなかったですね。話が合わないですね。

ちょっと気になったので、自分でも試してみました。Kotlinで。

build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.8.11'
    }
}
  
apply plugin: 'kotlin'
apply plugin: 'war'

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

archivesBaseName = 'javaee7-web'
  
repositories {
    mavenCentral()
}
  
dependencies {
    compile 'org.jetbrains.kotlin:kotlin-stdlib:0.8.11'
    providedCompile 'javax:javaee-web-api:7.0'
}

src/main/kotlin/app/HelloApplication.kt

package app

import javax.ws.rs.ApplicationPath
import javax.ws.rs.core.Application

ApplicationPath("rest")
public class HelloApplication : Application()

src/main/kotlin/app/HelloResource.kt

package app

import javax.ws.rs.DefaultValue
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.Produces
import javax.ws.rs.QueryParam
import javax.ws.rs.core.Context
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.UriInfo

Path("hello")
public class HelloResource(
    Context val uriInfo : UriInfo
  ) {

  GET
  Produces(MediaType.TEXT_PLAIN)
  fun sayHello(
      QueryParam("name") DefaultValue("world") name: String
    ): String = "Hello, $name!"

  Path("path")
  GET
  Produces(MediaType.TEXT_PLAIN)
  fun getPath(): String? = uriInfo.getPath()
}

WARを作成して

$ gradle --daemon war

まずは、WildFly 8.1.0.Finalにデプロイ。

$ cp build/libs/javaee7-web.war /path/to/wildfly-8.1.0.Final/standalone/deployments/

動作確認。

$ curl http://localhost:8080/javaee7-web/rest/hello?name=Kotlin
Hello, Kotlin!

$ curl http://localhost:8080/javaee7-web/rest/hello/path
/hello/path

…やっぱり、普通に動きますね。

ということは…。

WildFlyはシャットダウンし、今度はGlassFish 4.1をダウンロードしてデプロイ。

$ glassfish4/bin/asadmin deploy /path/to/build/libs/javaee7-web.war

実行。

$ curl http://localhost:8080/javaee7-web/rest/hello?name=Kotlin
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>GlassFish Server Open Source Edition  4.1  - Error report</title><style type="text/css"><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - Internal Server Error</h1><hr/><p><b>type</b> Exception report</p><p><b>message</b>Internal Server Error</p><p><b>description</b>The server encountered an internal error that prevented it from fulfilling this request.</p><p><b>exception</b> <pre>javax.servlet.ServletException: A MultiException has 1 exceptions.  They are:
1. java.lang.IllegalArgumentException: The field field&#40;UriInfo uriInfo in app.HelloResource&#41; may not be static, final or have an Annotation type
</pre></p><p><b>root cause</b> <pre>A MultiException has 1 exceptions.  They are:
1. java.lang.IllegalArgumentException: The field field&#40;UriInfo uriInfo in app.HelloResource&#41; may not be static, final or have an Annotation type
</pre></p><p><b>root cause</b> <pre>java.lang.IllegalArgumentException: The field field&#40;UriInfo uriInfo in app.HelloResource&#41; may not be static, final or have an Annotation type</pre></p><p><b>note</b> <u>The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition  4.1  logs.</u></p><hr/><h3>GlassFish Server Open Source Edition  4.1 </h3></body></html>

$ curl http://localhost:8080/javaee7-web/rest/hello/path
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>GlassFish Server Open Source Edition  4.1  - Error report</title><style type="text/css"><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - Internal Server Error</h1><hr/><p><b>type</b> Exception report</p><p><b>message</b>Internal Server Error</p><p><b>description</b>The server encountered an internal error that prevented it from fulfilling this request.</p><p><b>exception</b> <pre>javax.servlet.ServletException: A MultiException has 1 exceptions.  They are:
1. java.lang.IllegalArgumentException: The field field&#40;UriInfo uriInfo in app.HelloResource&#41; may not be static, final or have an Annotation type
</pre></p><p><b>root cause</b> <pre>A MultiException has 1 exceptions.  They are:
1. java.lang.IllegalArgumentException: The field field&#40;UriInfo uriInfo in app.HelloResource&#41; may not be static, final or have an Annotation type
</pre></p><p><b>root cause</b> <pre>java.lang.IllegalArgumentException: The field field&#40;UriInfo uriInfo in app.HelloResource&#41; may not be static, final or have an Annotation type</pre></p><p><b>note</b> <u>The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition  4.1  logs.</u></p><hr/><h3>GlassFish Server Open Source Edition  4.1 </h3></body></html>

エラーになった…。

ということは、元の資料はGlassFishでやってダメだったということなのでしょうね。

ちなみに、前回作成したScalaのコードも、valを付与しなかった場合は同様のエラーになりました。そしてKotlinで書いた方も、WildFlyにデプロイしたら動作しました。なんとまあ…。

GlassFishの挙動の方が正解なんでしょうかね??

ちなみに、この時GlassFish側に出力されているスタックトレースはこんな感じです。

[2014-09-18T02:55:09.317+0900] [glassfish 4.1] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=30 _ThreadName=http-listener-1(2)] [timeMillis: 1410976509317] [levelValue: 900] [[
  StandardWrapperValve[app.HelloApplication]: Servlet.service() for servlet app.HelloApplication threw exception
java.lang.IllegalArgumentException: The field field(UriInfo uriInfo in app.HelloResource) may not be static, final or have an Annotation type
	at org.jvnet.hk2.internal.Utilities.findInitializerFields(Utilities.java:1443)
	at org.jvnet.hk2.internal.DefaultClassAnalyzer.getFields(DefaultClassAnalyzer.java:113)
	at org.glassfish.jersey.internal.inject.JerseyClassAnalyzer.getFields(JerseyClassAnalyzer.java:244)
	at org.jvnet.hk2.internal.Utilities.getInitFields(Utilities.java:257)
	at org.jvnet.hk2.internal.Utilities.justInject(Utilities.java:932)
	at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(ServiceLocatorImpl.java:902)
	at org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:977)
	at org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:968)
	at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:173)
	at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
	at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:74)
	at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)
	at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
	at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
	at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
	at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
	at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:94)
	at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:63)
	at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
	at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:263)
	at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
	at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
	at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
	at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
	at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
	at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
	at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:254)
	at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)
	at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:372)
	at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
	at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
	at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
	at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
	at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
	at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
	at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
	at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
	at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
	at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
	at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
	at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
	at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
	at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
	at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
	at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
	at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
	at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
	at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
	at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
	at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
	at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
	at java.lang.Thread.run(Thread.java:745)

あんまり追う時間はないので、ここまでということで。