Coder Social home page Coder Social logo

syamb4u / spring-http-patch-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from countrogue/spring-http-patch-example

0.0 2.0 0.0 30 KB

This is a sample project for supporting HTTP Patch in spring framework. This uses the google-like Patch mechanism.

License: GNU General Public License v3.0

Java 100.00%

spring-http-patch-example's Introduction

spring-http-patch-example

This is a sample project for supporting HTTP Patch in spring framework. This follows a simple HTTP Patch mechanism, same as the way google does it. It uses the following

  • spring-boot with json(jackson) response
  • swagger for viewing
  • in memory database(h2)

PATCH Example

The following describes an example flow on the HTTP Patch implemented here

Current JSON Model

{
  "id": "1",
  "name": "Computer X",
  "dateAcquired": 1472617178.936,
  "count": 101,
  "locations": [
    {
      "id": "canada",
      "name": "Canada"
    },
    {
      "id": "philippines",
      "name": "Philippines"
    },
    {
      "id": "newzealand",
      "name": "New Zealand"
    }
  ]
}

HTTP Patch Request

As you can see here an incomplete Asset model is passed with only the property name, this tells the server to only update the name property.

curl -X PATCH -H "Content-Type: application/json" -d '{
"name": "New Asset Name"
}' http://localhost:8080/api/v1/assets/1

Updated JSON Model

{
  "id": "1",
  "name": "New Asset Name",
  "dateAcquired": 1472617178.936,
  "count": 101,
  "locations": [
    {
      "id": "canada",
      "name": "Canada"
    },
    {
      "id": "philippines",
      "name": "Philippines"
    },
    {
      "id": "newzealand",
      "name": "New Zealand"
    }
  ]
}

Usage

  1. Controller methods for PATCH should be annotated with @Patch where you can specify
    • id - type of id the entity used
    • service - bean service class that retrieves the entity.
  2. Instead of using spring's @RequestBody use @PatchRequestBody, this will retrieve the object from the service specified and update it accordingly with the body in the request.

spring-http-patch-example's People

Contributors

countrogue avatar

Watchers

James Cloos avatar Syamkumar Sanka 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.