GithubHelp home page GithubHelp logo

rest-patch's Introduction

rest-patch

This library aims to aid in supporting patching for requests in Java-based RESTFUL webservices. It is not tied into Spring in anyway, but all examples here are shown using Spring Boot. It aims to support the logical way of handling REST in services like Stripe and Previsto and hence support JSON and FORM based requests.

What does it do?

Merging

First of all it allows you to patch a POJO upon another POJO of same type based on a list of dirty fields. You simply gather a list of dirty fields which you want to be patched from one POJO to another. You use the EntityMerger for this purpose as shown below.

Merge Example

public void main() {
  EntityMerger merger = new EntityMerger();
  Pet patch = new Pet();
  patch.setName("Bella");
  path.setKind(Kind.Cat);
  
  Pet original = new Pet();
  original.setName("Barkley");
  original.setKind(Kind.Dog);
  
  List<String> dirtyFields = Collections.singletonList("name");
  merger.mergeEntities(original, patch, dirtyFields);
  
  System.out.println(original.getName()); // Outputs "Bella" (Was patched)
  System.out.println(original.getKind()); // Outputs "Dog" (Was NOT patched)  
}

Resolve dirty fields

Second, it allows you to gather the dirty fields from JSON(via Jackson) and FORM(via Java Map) requests. Checkout the following examples:

JSON Example (Spring Boot)

@RestController
public class PetController {

  private TreeNodePropertyReferenceConverter fieldConverter = new TreeNodePropertyReferenceConverter();
  private EntityMerger<et> merger = new EntityMerger();
    
  @PutMapping("/pets/{id}")
  public Pet update(@RequestBody Pet patch, @PathVariable String id) {
    Pet original = ...;  // Get original from backend
    List<String> fields = fieldConverter.translate(TreeNodeHolder.get());
    Pet merge = this.merger.mergeEntities(original, patch, fields);
    ... // Save merge to backend
  } 
}

The above example allows us to send just the fields we want to update, fx. {"name": "Bessie"}, without overwriting other fields.

It gives you the same flexibility with FORM input which is handy when it comes to supporting access to the API via cUrl.

FORM Example (Spring Boot)

@RestController
public class PetController {

  private FormPropertyReferenceConverter fieldConverter = new FormPropertyReferenceConverter();
  private EntityMerger<et> merger = new EntityMerger();
    
  @PutMapping("/pets/{id}", consumes = "application/x-www-form-urlencoded")
  public Pet update(@ModelAttribute Pet patch, @PathVariable String id, HttpServletRequest request) {
    Pet original = ...;  // Get original from backend
    List<String> fields = fieldConverter.translate(request.getParameterMap()));
    Pet merge = this.merger.mergeEntities(original, patch, fields);
    ... // Save merge to backend
  } 
}

The above example allows us to send just the fields we want to update via cUrl, fx. curl -X PUT -d name=Bessie http://server/pets/{id}, without overwriting other fields.

rest-patch's People

Contributors

michaelkrog 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.