« NetBeans でパッケージ作成:REST(2) | トップページ | InitDatabase の実行まで:REST(4) »

2015年2月21日 (土)

依存性:REST(3)

クライアント pom.xml の要点

  • Web サーバは war ファイルで jar ではない。依存性に server を加えても server 側の infomodel などが読めないためビルドできない。
    Failed to execute goal on project client: Could not resolve dependencies for project open.dolphin:client:jar:1.3.0.10: Failure to find open.dolphin:server:jar:1.3.0.10 in http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]
    
    そこで,後述の server 側の pom.xml で,server 側のクラスを入れた xxxx-classes.jar を作る設定にして,<classifier> を設定して,クライアントでそれを読むようにする。
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>open.dolphin.server</artifactId>
        <version>${project.version}</version>
        <classifier>classes</classifier>
    </dependency>
    
  • JBoss の REST は RESTEasy というもので,WildFly 8.2 には 3.0.10.Final が入っている。クライアントには同じバージョンのクライアントを入れる。
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-client</artifactId>
        <version>3.0.10.Final</version>
        <type>jar</type>
    </dependency>
    
  • これまで,受付リストのリアルタイム同期に JMS というシステムを使っていたが,最近ホットなのは WebSocket というものらしく,WildFly 8.2 にも WebSocket 1.1 というのが入っている。クライアント側で standalone で WebSocket を使用するためには以下の依存性が必要になる。
    <dependency>
        <groupId>org.glassfish.tyrus.bundles</groupId>
        <artifactId>tyrus-standalone-client-jdk</artifactId>
        <version>1.10</version>
    </dependency>
    

サーバ pom.xml の要点

  • クライアント側の pom.xml でも触れたが,server の war をクライアントの依存に加えるためには jar ファイルを作る必要がある。maven-war-plugin に以下のように2行加えることで,xxxx-classes-jar ができるようになる。
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
            <attachClasses>true</attachClasses> <!-- this line is manually added -->
            <classesClassifier>classes</classesClassifier> <!-- this line is manually added -->
        </configuration>
    </plugin>
    
  • RESTEasy を使うための依存。ただし,WildFly 8.2 にはもう入っているので,scope を provided にする。
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>3.0.10.Final</version>
        <scope>provided</scope>
    </dependency>
    
  • Jackson 関連では,以下のものだけ provided にする。
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson2-provider</artifactId>
        <version>3.0.10.Final</version>
        <scope>provided</scope>
    </dependency>
    
  • Jackson で json encode/decode する際に,そのままだと entity の lazy fetch を追いかけてしまい,以下の Exception が出る。
    org.jboss.resteasy.spi.UnhandledException: Response is committed, can't handle exception
    Caused by: com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: open.dolphin.infomodel.PatientModel.healthInsurances, could not initialize proxy - no Session (through reference chain: open.dolphin.infomodel.PatientVisitModel["patient"]->open.dolphin.infomodel.PatientModel["healthInsurances"])
    Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: open.dolphin.infomodel.PatientModel.healthInsurances, could not initialize proxy - no Session
    
    jackson-datatype-hibernate4 を使うと lazy fetch を追いかけないようにできる。
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-hibernate4</artifactId>
        <version>2.4.5</version>
    </dependency>
    
  • Hibernate 関連で,最初,バージョンがぐちゃぐちゃになっているのに気付かないで,UnknownServiceException に苦しんだ。
    JBAS014134: EJB Invocation failed on component KarteServiceImpl for method public abstract long open.dolphin.service.KarteService.addDocument(open.dolphin.infomodel.DocumentModel): javax.ejb.EJBException: org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.service.jta.platform.spi.JtaPlatform]
    
    hibernate search を入れると,hibernate core 等の依存が自動的に入るが,それが古いバージョンになっていて,WildFly の hibernate とぶつかってしまうようだった。Hibernate の依存は難しいので,WildFly のソースに入っている pom.xml から該当部分をコピーして解決した。

追記

WildFly 8 には hibernate-search が入っているので,provided で依存を入れて,WEB-INF/jboss-deployment-structure.xml で hibernate search を有効化するとよいと増田先生の twitter で教えてもらった。
おかげさまで,pom.xml がかなりすっきりした(クライアント pom.xml,サーバ pom.xml)。さらに,war ファイルの容量がものすごく減った(21M → 3.5M)。 war 容量のほとんどが hibernate 関連のライブラリで占められてしまっていたようだ。

« NetBeans でパッケージ作成:REST(2) | トップページ | InitDatabase の実行まで:REST(4) »

OpenDolphin」カテゴリの記事