[JAVA] Create RestAPI using wicketstuff-restannotations of Apache Wicket

As the title says. Since there was a scene where I used it at the university, I will leave it as a memo. Create RestAPI with Apache Wicket. Other RestAPIs include Jersey, but at my university, Wicket is often used, so this time I will implement it with the title.

To create a Rest API You need to use wicketstuff-restannotations.

The following description is required in 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>

Next, create the classes that make up the API. To create it, inherit AbstractRestResource.

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;
	
	/**
	 *instance
	 */
	public SampleAPI() {
		super(new JsonWebSerialDeserial(new JacksonObjectSerialDeserial()));
	}
	
	@MethodMapping("/test/{id}")
	public SampleBean sample(long id) {
		return new SampleBean(id, "test");
	}
}

Next, here is the contents of the SampleBean used in the SampleAPI. You can use JsonProperty to rename the 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;
	
	/**
	 *Argumentless constructor
	 */
	public SampleBean() {
		this.sampleId = -1;
		this.sampleName = "";
	}
	
	/**
	 *Constructor with arguments
	 * @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 Set sampleId
	 */
	public void setSampleId(long sampleId) {
		this.sampleId = sampleId;
	}

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

	/**
	 * @param sampleName set sampleName
	 */
	public void setSampleName(String sampleName) {
		this.sampleName = sampleName;
	}
}


Next, mount resouce on a subclass that inherits from the WebApplication class.

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;
            }
        });
    }
}

After that, when you call the API, json format data will be returned. For this API, if you specify ʻapi / test / 2` and the API, the value will be returned.

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

I will summarize in detail this time. (Because I am busy with university related matters such as December.)

Reference site

I am always indebted to articles such as Wicket and Java. Thanks. Reference site: https://www.monotalk.xyz/blog/apache-wicket%E3%81%A7restapi%E3%82%92%E4%BD%BF%E3%81%86/

Recommended Posts

Create RestAPI using wicketstuff-restannotations of Apache Wicket
I tried using Apache Wicket
Create Restapi with Spring Boot ((1) Until Run of App)
Try using JobScheduler's REST-API
Example of using vue.config.js
Summary of using FragmentArgs
Summary of using DBFlow
[Apache Tomcat] The story of using Apache OpenWebBeans to enable CDI