Giter Site home page Giter Site logo

codacy-badger / ditiow Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marcosvidolin/ditiow

0.0 0.0 0.0 167 KB

๐Ÿš€ Ditiow is a simple 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.

License: MIT License

Java 100.00%

ditiow's Introduction

ditiow

Download Codacy Badge Build Status

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.

Setup

  1. Ditiow is release by publishing in to the JCenter. So add the "jcenter" in your dependency management.

  2. Declaring the dependency

    • Gradle
    compile 'com.vidolima:ditiow:0.2.2'
    • Maven
    <dependency>
    	<groupId>com.vidolima</groupId>
    	<artifactId>ditiow</artifactId>
    	<version>0.2.2</version>
    	<type>pom</type>
    </dependency>
  3. Declare DitiowAspect as a bean. Add aspect bean in one of the @Configuration classes

    @Bean
    public DitiowAspect ditiow() {
        return new DitiowAspect();
    }

How to use

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;
  // ...
}

Returning a resource from the controller

  • 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 @ResourseResponse 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 PostGetResourse object that is inserted into the body of the ResponseEntity object.

Retrieving a resource as a parameter

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

TODO

  • Create an annotation to specify the attribute name of the resource (without having to match the name of the domain class);
  • Unit tests;

Contributors

ditiow's People

Contributors

marcosvidolin avatar renovate-bot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.