hi, all,
I'm playing a bit with REST APIs recently and found WicketStuff
rest-annotations quite useful
https://github.com/wicketstuff/core/tree/master/wicketstuff-restannotations-parent
Though at present, I'm just started working with it.
Prior this, I've looked at trying to integrate Eclipse Jersey
https://eclipse-ee4j.github.io/jersey/
or RESTEasy
https://resteasy.dev/
Initially, one of the stumbling blocks is authentication and
authorization, especially if one intends to run such a service on the
open internet. It isn't easy integrating with Wicket security for
authentication/authorization with different architecture.
doing REST API completely within Apache Wicket alleviates this issue. A
problem is that I'd need to expose different http methods endpoints,
e.g. with the use of custom resources. rest endpoints isn't just
straightforward http get. restannotations has a proper implementation
and did quite a lot more. e.g. that it automatically deserializes to
Json via Gson etc.
But that there are some currently I deemed minor issues and I'm still
figuring out other use cases.
Some of the issues encountered are like:
- use AbstractRestResource as base class
If you review the readme
https://github.com/wicketstuff/core/tree/master/wicketstuff-restannotations-parent#readme
it isn't initially apparent that a better practice is to build your rest
classes by inheriting from AbstractRestResource
https://github.com/wicketstuff/core/blob/master/wicketstuff-restannotations-parent/restannotations/src/main/java/org/wicketstuff/rest/resource/AbstractRestResource.java
Initially, I'm troubleshooting why the RestAPI endpoints are not mapped.
it turns out to use @ResourcePath("/mountedpath") and
PackageScanner.scanPackage("your.restapi.package");
https://github.com/wicketstuff/core/tree/master/wicketstuff-restannotations-parent#mounting-resources-to-a-specific-path
it is necessary to subclass from AbstractRestResource as a REST API
container class,
then define the
@MethodMapping("/apipath")
methods as the rest endpoints for each method.
After that the it correctly mount the rest endpoints, and practically
many things 'just works'
A good example is to look at the examples e.g.
https://github.com/wicketstuff/core/blob/master/wicketstuff-restannotations-parent/restannotations-examples/src/main/java/org/wicketstuff/rest/resource/PersonsRestResource.java
to see how it is implemented.
- register the Json serializer and deserializer while constructing the
class wrapper for the rest APIs.
in the constructor one should have a line like.
public PersonsRestResource() {
super(new JsonWebSerialDeserial(new GsonObjectSerialDeserial()));
to register the Json serializer / deserializer, this is what takes pojo
classes including like List<Person> etc and serialize them directly to
json, practically no other codes is needed.
https://github.com/wicketstuff/core/blob/77dc12337444090867368e972cb66d35c048ce37/wicketstuff-restannotations-parent/restannotations-examples/src/main/java/org/wicketstuff/rest/resource/PersonsRestResource.java#L35
but that I encountered a rather tricky situation where the default Gson
serializer / deserializer can't convert a particular class / structure
in a class / object. it needs an (custom) adapter to do that
serialization / deserialization.
A trouble with the above is that in the earlier java versions superclass
constructor calls has to be the first line of code in the constructor.
This is a bit of trouble as I had trouble building a custom Gson()
object that has the conversion adapters registered.
I end up using discrete classes for the converters and used the Gson
builder in a fluent notation to build the Gson() converter object within
the constructor call.
hope this helps if anyone is working with the restannotations library
note also that WicketStuff rest annotations is directly available as a
dependency in maven central. e.g.
https://central.sonatype.com/artifact/org.wicketstuff/wicketstuff-restannotations
verify that by matching the pom entries in the github repo with that in
maven central.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]