DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Testing REST Controller Methods With JUnit 5 [Video]
  • Update User Details in API Test Client Using REST Assured [Video]

Trending

  • Agile Estimation: Techniques and Tips for Success
  • Monkey-Patching in Java
  • REST vs. Message Brokers: Choosing the Right Communication
  • Build a Serverless App Fast With Zipper: Write TypeScript, Offload Everything Else
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Working with REST in Wicket

Working with REST in Wicket

Apache Wicket is known for its capability of transparently handling the state of web applications on server side, and can be easily adopted to create RESTful services.

Andrea Del Bene user avatar by
Andrea Del Bene
·
Aug. 28, 13 · Tutorial
Like (3)
Save
Tweet
Share
18.79K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

REST-based APIs are becoming more and more popular around the web and the number of services based on REST are constantly increasing. Apache Wicket is well-known for its capability of transparently handling the state of web applications on server side, but it can be easily adopted to create RESTful services as well.

With the last release of WicketStuff project (version 6.10.0) we can find the Wicket REST annotations module that allows us to develop REST applications/APIs in much the same way as we do it with Spring MVC or with the standard JAX-RS.

In this article, we will explore the basic features and the artifacts offered by the Wicket REST annotations module to implement the REST architecture with Apache Wicket.

The REST Annotations module in short

The central element of the module is the abstract class  AbstractRestResource that is a generic class to implement a Wicket resource that handles the request and the response using a particular data format (like XML, JSON, etc...). As JSON is de-facto standard format for REST API, the project comes also with a ready-to-use resource called GsonRestResource that works with the JSON format.

The example sub-module contains resource PersonsRestResource which subclasses GsonRestResource and implements a simple REST service to handle instances of the following bean:

package org.wicketstuff.rest.domain;

public class PersonPojo {
  private String name;
  private String email;
  private String password;

  public PersonPojo(String name, String email, String password) {
    this.name = name;
    this.email = email;
    this.password = password;
  }
}

To indicate which methods must be invoked to serve REST requests, we must use annotation  MethodMapping . Its basic usage is quite simple and intuitive, as we can see in the code of PersonsRestResource:

public class PersonsRestResource extends GsonRestResource {
  private final List<PersonPojo> persons = new ArrayList<PersonPojo>();

  @MethodMapping("/persons")
  public List<PersonPojo> getAllPersons() {
    return persons;
  }

  @MethodMapping(value = "/persons/{personIndex}", httpMethod = HttpMethod.DELETE)
  public void deletePerson(int personIndex) {
    persons.remove(personIndex);
  }

  @MethodMapping(value = "/persons", httpMethod = HttpMethod.POST)
  public PersonPojo createPerson(@RequestBody PersonPojo personPojo) {
    persons.add(personPojo);
    return personPojo;
  }
}

The two required attributes for MethodMapping are the subpath to use to mount the method and the HTTP method to map to. The final path for the mapped method will be the path the resource is mounted at plus the value expressed in MethodMapping. This value can contain path variables that can be used as parameters for mapped methods (like personIndex in the code above).


To promote the principle of convention over configuration, we don't need to use any annotation to map method parameters to path parameters if they are declared in the same order (but we can always decide to do this manually, as we will see shortly).

If a mapped method returns a value, this latter will be serialized to JSON and written to the response. The code above shows also how to do the inverse procedure (i.e. reading an object serialized as JSON from request body) with annotation RequestBody (method createPerson).

To convert strings to Java type, AbstractRestResource uses the standard Wicket mechanism based on the application converter locator:


//return the converter for type clazz
IConverter converter = Application.get().getConverterLocator().getConverter(clazz);
//convert string to object
return converter.convertToObject(value, Session.get().getLocale());

This is all we have to know to start using REST with Wicket. The Wicket REST annotations module offers more advanced features such as regular expression for path parameters and integration with Wicket role based authorizations.

To learn more about these advanced features you can go to the project page  where you will find an exhaustive documentation about this new module. 

REST Web Protocols

Published at DZone with permission of Andrea Del Bene. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Testing REST Controller Methods With JUnit 5 [Video]
  • Update User Details in API Test Client Using REST Assured [Video]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: