LoginSignup
5
3

More than 5 years have passed since last update.

Apache Wicket の wicketstuff-restannotations を使って RestAPI を作る

Last updated at Posted at 2017-12-21

表題の通りです。
大学で利用する場面が発生しましたので、メモ書き程度に残します。
Apache Wicket で RestAPI をつくります。
他に、RestAPI といえば、Jersey 等ありますが、私の大学では、Wicket を使用する場面が多いので、今回は表題で実装します。

RestAPIをつくるには、
wicketstuff-restannotations を使う必要があります。

pom.xml に以下の記述が必要です。

        <dependency>
            <groupId>org.wicketstuff</groupId>
            <artifactId>wicketstuff-restannotations</artifactId>
            <version>6.20.0</version>
        </dependency>
                <dependency>
            <groupId>org.wicketstuff</groupId>
            <artifactId>wicketstuff-restannotations-json</artifactId>
            <version>6.20.0</version>
        </dependency>

次に、APIを構成するクラスを作成します。
作成には、AbstractRestResource を継承します。
※ Wicket 6 では、 GsonRestResource を継承してAPIを作成することも可能ですが, Wicket 7 以降では、GsonRestResourceが削除されているので注意。

import org.wicketstuff.rest.annotations.MethodMapping;
import org.wicketstuff.rest.contenthandling.json.objserialdeserial.JacksonObjectSerialDeserial;
import org.wicketstuff.rest.contenthandling.json.webserialdeserial.JsonWebSerialDeserial;
import org.wicketstuff.rest.resource.AbstractRestResource;

public class SampleAPI extends AbstractRestResource<JsonWebSerialDeserial> {
    private static final long serialVersionUID = -3988855556227959163L;

    /**
     * インスタンス
     */
    public SampleAPI() {
        super(new JsonWebSerialDeserial(new JacksonObjectSerialDeserial()));
    }

    @MethodMapping("/test/{id}")
    public SampleBean sample(long id) {
        return new SampleBean(id, "test");
    }
}

次に、 SampleAPI で 利用した SampleBean の中身はこちら。JsonProperty を利用すると、 key の名前を変更できる。

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * sample 
 *
 */

public class SampleBean implements Serializable {
    private static final long serialVersionUID = -6787374014454880191L;

    @JsonProperty("id")
    private long sampleId;
    @JsonProperty("name")
    private String sampleName;

    /**
     * 引数なしコンストラクタ
     */
    public SampleBean() {
        this.sampleId = -1;
        this.sampleName = "";
    }

    /**
     * 引数ありコンストラクタ
     * @param sampleId
     * @param sampleName
     */
    public SampleBean(long sampleId, String sampleName) {
        this.sampleId = sampleId;
        this.sampleName = sampleName;
    }

    /**
     * @return sampleId
     */
    public long getSampleId() {
        return sampleId;
    }

    /**
     * @param sampleId セットする sampleId
     */
    public void setSampleId(long sampleId) {
        this.sampleId = sampleId;
    }

    /**
     * @return sampleName
     */
    public String getSampleName() {
        return sampleName;
    }

    /**
     * @param sampleName セットする sampleName
     */
    public void setSampleName(String sampleName) {
        this.sampleName = sampleName;
    }
}


次に、WebApplication クラスを継承したサブクラスに resouce をマウントする。

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.resource.IResource;
import org.apache.wicket.request.resource.ResourceReference;

import com.example.api.SampleAPI;
import com.example.page.IndexPage;

public class MyApplication extends WebApplication {

    @Override
    public Class<? extends WebPage> getHomePage() {
        return IndexPage.class;
    }

    @Override
    public void init() {
        super.init();

        mountPage("/index", Index.class);
        mountResource("/api", new ResourceReference("restReference") {
            private static final long serialVersionUID = -3002079769630066308L;

            private SampleAPI sampleAPI = new SampleAPI();

            @Override
            public IResource getResource() {
                return sampleAPI;
            }
        });
    }
}

あとは、APIを呼び出すと、 json 形式のデータが返ってきます。
今回のAPIは、api/test/2 とAPIを指定すると値が帰ってきます。

{"id":2,"name":"test"}

また今度、詳しくまとめます。
(私事で、12月等は大学関連で忙しいので。)

参考サイト

いつも Wicket・Java 等の記事でお世話になっています。感謝。
参考サイト:https://www.monotalk.xyz/blog/apache-wicket%E3%81%A7restapi%E3%82%92%E4%BD%BF%E3%81%86/

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3