Ditiow is an aspect library designed to help you safely expose features of your Spring REST API without having to expose data from the persistence or business layer of your application.
-
Ditiow is release by publishing in to the JCenter. So add the "jcenter" in your dependency management.
- Gradle
repositories { jcenter() }
- Maven
<repositories> <repository> <id>jcenter</id> <name>jcenter</name> <url>https://jcenter.bintray.com</url> </repository> </repositories>
-
Declaring the dependency
- Gradle
compile 'com.vidolima:ditiow:1.2.0'
- Maven
<dependency> <groupId>com.vidolima</groupId> <artifactId>ditiow</artifactId> <version>1.2.0</version> <type>pom</type> </dependency>
-
Declare DitiowAspect as a bean. Add aspect bean in one of the @Configuration classes
@Configuration public class DitiowConfig { @Bean public DitiowAspect ditiow() { return new DitiowAspect(); } }
Domain class with all fields. Nothing needs to be done at this point.
public class Post {
private Long code;
private UUID uuid;
private User author;
private String content;
private Date publishedAt;
private Collection<Comment> comments;
// ...
}
-
Here we specify the resource or how our Post object will be exposed by the API. We do this by extending the AbstractResource class and informing the domain class (Post) as type. Note that the name of the attributes are the same as Post.
But I don't want to expose some attributes like database id and comments, for example.
public class PostGetResource extends AbstractResource<Post> { private UUID uuid; private User author; private String content; private Date publishedAt; // ... }
-
Enable conversion by adding @ResponseResource annotation on controller class with the "PostGetResource" as the value of the annotation.
@GetMapping(path = "/posts/{uuid}") @ResponseResource(PostGetResource.class) public ResponseEntity<?> get(@PathVariable UUID uuid) { Post post = this.postService.findPostByUuid(uuid); return ResponseEntity.ok(post); }
Here the magic happens. The response will be converted to a PostGetResource object that is inserted into the body of the ResponseEntity object.
-
In this example the values of
author
andpublichedAt
fields will not be returned in the responseWhenever a primitive field is ignored, its original value will be omitted and the corresponding default value will be returned.
@GetMapping(path = "/posts/{uuid}")
@ResponseResource(PostGetResource.class, ignoreProperties = {"author", "publishedAt"})
public ResponseEntity<?> get(@PathVariable UUID uuid) {
Post post = this.postService.findPostByUuid(uuid);
return ResponseEntity.ok(post);
}
public class PostCreateResource extends AbstractResource<Post> {
private UUID uuid;
@NotEmpty
@Length(min = 50, max = 300)
private String content;
// ...
}
@PostMapping(path = "/posts")
@ResponseResource(PostGetResource.class)
public ResponseEntity<?> create(@Valid @RequestBody PostCreateResource resource) {
Post post = resource.toDomain(); // converts the resource into a Post
post.setAuthor(this.currentUserUtil.getUser());
return ResponseEntity.ok(this.postService.create(post));
}