https://github.com/cloudogu/jaxrs-tie
Generates link builder from JAX-RS annotation
https://github.com/cloudogu/jaxrs-tie
annotation annotation-processor hateoas java jax-rs jax-rs-annotations jax-rs-resources jax-rs-tie link link-builder
Last synced: 6 months ago
JSON representation
Generates link builder from JAX-RS annotation
- Host: GitHub
- URL: https://github.com/cloudogu/jaxrs-tie
- Owner: cloudogu
- License: mit
- Created: 2018-10-01T19:58:07.000Z (about 7 years ago)
- Default Branch: develop
- Last Pushed: 2024-09-17T14:34:21.000Z (about 1 year ago)
- Last Synced: 2024-09-17T17:23:57.629Z (about 1 year ago)
- Topics: annotation, annotation-processor, hateoas, java, jax-rs, jax-rs-annotations, jax-rs-resources, jax-rs-tie, link, link-builder
- Language: Java
- Homepage:
- Size: 183 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JAX-RS Tie
Generate a type safe link builder from your JAX-RS annotations.
## Why?
In modern rest applications it is usual to generate links between resources ([Rest Maturity Model Level 3](https://martinfowler.com/articles/richardsonMaturityModel.html#level3)).
In JAX-RS resources can be linked using a [UriBuilder](https://jakarta.ee/specifications/restful-ws/3.0/apidocs/jakarta/ws/rs/core/uribuilder) for example:
```java
@GET
@Path("{planet}")
public String planetLink(@Context UriInfo uriInfo, @PathParam("planet") String planetName) {
return uriInfo.getBaseUriBuilder()
.path(PlanetResource.class)
.path(PlanetResource.class, "planet")
.build(planetName)
.toASCIIString();
}
```The code above generates a link which points to the `planet` method of the `PlanetResource`.
It seams easy, but there are problems with this approach:
* Methods are passed as strings, which could cause problems if a method is renamed during a refactoring
* If we are using sub resources, we have to rememeber the hirachy e.g.:```java
uriInfo.getBaseUriBuilder()
.path(PersonResource.class, "person")
.path("luke")
.path(PlanetResource.class, "planet")
.build("tatooine");
```JAX-RS Tie tries to tacle both problems.
It will generate a link builder which is automatically regenerated if a resources changes
and it map the hirachy or the resources.With JAX-RS Tie link generation could be look as the following:
```java
@GET
@Path("{planet}")
public String planetLink(@Context UriInfo uriInfo, @PathParam("planet") String planetName) {
return new SwLinks(uriInfo).planets()
.planet(planetName)
.asString();
}
```## Usage
Only a single annotation is required to use JAX-RS Tie.
Just annotate a class with the `@GenerateLinkBuilder` annotation and JAX-RS Tie generates the link builder
with the name of the annotated class and appends "Links" to the name e.g.:```java
@GenerateLinkBuilder
class StarWars {}
```The example above will create a `StarWarsLinks` link builder in the same package as the `StarWars` class.
The link builder will automatically find all JAX-RS resources which are annotated with the `@Path` annotation.## Installation
Get the latest stable version from [](https://search.maven.org/search?q=g:com.cloudogu.jaxrs-tie%20a:jaxrs-tie)
### Gradle
```groovy
compileOnly 'com.cloudogu.jaxrs-tie:jaxrs-tie:x.y.z'
annotationProcessor 'com.cloudogu.jaxrs-tie:jaxrs-tie:x.y.z'
```### Maven
```xml
com.cloudogu.jaxrs-tie
jaxrs-tie
x.y.z
true```
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details