Giter VIP home page Giter VIP logo

spring-bulk-api's Introduction

Maven Central

spring-bulk-api

Add bulk operations support to any Spring RESTful API by a single annotation @EnableBulkApi.

Purpose

Allow user to define a list of RESTful operations by JSON,
and send them to server-side for bulk actions.

Note

For simplicity, spring-bulk-api doesn't deal with any of security issues.
Server-side should set up the Spring security for safety concerns.
Ex: enable basic authentication and SSL, disable CSRF protection... etc.

Maven Repo

<dependency>
	<groupId>com.github.wnameless.spring</groupId>
	<artifactId>spring-bulk-api</artifactId>
	<version>0.7.0</version>
</dependency>

Important

@RequestMapping allows user to define the url in the path property instead of value property.
(ex: @ResuestMapping(path="/index"), @RequestMapping(value="/index")).

Before v0.6.0, the path value of @ResuestMapping(path="/index") is not read to spring-bulk-api,
this bug has been fixed since v0.6.0.

Since Spring Boot v2.1.1, spring-bulk-api 0.7.0+ is required.

Quick Start

Add @EnableBulkApi to enable bulk API

@Configuration
@EnableBulkApi
public class WebConfig {
  ...
}

Since v0.7.0, URITransformer is added to manipulate each request URI which is computed by each bulk request

@Bean
public URITransformer uriTransformer() {
  
  return new URITransformer() {

    @Override
    public URI transform(URI uri) {
      // Some actions with uri..
      return uri;
    }

};

Since v0.6.0, following Spring mapping annotations are supported

@GetMapping
@PostMapping
@DeleteMapping
@PutMapping
@PatchMapping

Since v0.5.0, @AcceptBulk is added to provide more controls

@Bulkable(autoApply=false)
@RestController
public class HomeController {

  // Not allow bulk operations
  @RequestMapping("/index")
  public void index() {
    ...
  }

  // Allow bulk operations
  @AcceptBulk
  @RequestMapping("/home")
  public void home() {
    ...
  }
  ...
}

Since v0.4.0, @Bulkable is required to be annotated on the controller which accepts bulk operations

@Bulkable
@RestController
public class HomeController {
  ...
}

application.properties

By default path is /bulk and limit to 100 operations, it can be configured by Spring application.properties

spring.bulk.api.path=/batch # default is /bulk
spring.bulk.api.limit=200 # default is 100

Request JSON example

# POST /bulk
# Content-Type: application/json

{
  "operations": [
    {"method": "GET", "url": "/home"},
    {"method": "POST", "url": "/posts/new", "params": {"title": "My Dream"}},
    {"method": "DELETE", "url": "/posts/123", "headers": {"Authentication": "Basic ..."}}
  ]
}
  • url - the API endpoint, relative to your application context path. (required)
  • method - the HTTP method(GET, POST, DELETE ...etc.) Default is GET. (optional)
  • params - the HTTP parameters to the API. (optional)
  • headers - a hash of of headers which should be included in this operation. (optional)
  • silent - if it's set to true, there is no result created in the response for this operation. (optional)

Response JSON example

{
  "results": [
    {"status": 200, "body": "Welcome!", "headers": {}},
    {"status": 201, "body": {"id": 222, "title": "My Dream"}, "headers": {}}
  ]
}
  • status - the HTTP status.
  • body - the response body.
  • headers - the headers of single result.

BulkApiService

@Autowired
@Bean
public BulkApiService bulkApiService(ApplicationContext appCtx) {
  return new DefaultBulkApiService(appCtx)
}

By default, the DefaultBulkApiService will be autowired automatically, you don't need to provide it.
However you can extend the DefaultBulkApiService to provide your custom implementation or implement the BulkApiService interface by your own.

spring-bulk-api's People

Contributors

wnameless avatar ebann avatar

Watchers

James Cloos 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.