YunaiV / spring-boot-starter-jsonrpc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Maven Central

Spring Boot Starter JSON-RPC

The primary goal of the Spring Boot Starter JSON-RPC project is to make it easier to build Spring-powered applications that use JSON-RPC 2.0 Specification.

Features

  • Exports beans' methods to make it available from JSON-RPC API
  • Generates Swagger's documentation on available methods through JSON-RPC API
  • Gives opportunity to test JSON-RPC methods with raw methods' params using SwaggerUI
  • Easy Spring integration

Restrictions

  • Works only for Spring Web (Servlet), doesn't work with Spring Web Flux
  • Allows methods with one or none parameters
  • JSON-RPC's batch requests are not allowed

Quick Start

Download the jar through Maven:

<dependency>
  <groupId>com.github.krupt</groupId>
  <artifactId>spring-boot-starter-jsonrpc</artifactId>
  <version>${version}</version>
</dependency>

Add properties to your application:

spring.jsonrpc:
    path: api
    basePackage: com.github.krupt

where:

  • path is the HTTP URL path that is the endpoint of JSON-RPC engine(for example: http://localhost:8080/api)
  • basePackage is a base package's name to export all your beans' methods for Swagger

The simple JSON-RPC service looks like this:

@JsonRpcService
public class UserService {

    public User get(UUID userId) {
        ...
    }
}

All the public methods with one or none parameters are collected by JSON RPC Engine and can be accessed through HTTP API. If you want to hide beans' public methods, you need to mark the target method with @NoJsonRpcMethod annotation.

The other way to expose methods is implementing com.github.krupt.jsonrpc.JsonRpcMethod interface.

Example:

package com.github.krupt.web.jsonrpc.user;

public class GetAllMethod implements JsonRpcMethod<Void, List<User>> {

    public List<User> invoke(Void input) {
        ...
    }
}

, that will be exposed as user.getAll method.

If you want specific exception handling, add bean that implements com.github.krupt.jsonrpc.exception.JsonRpcExceptionHandler.


There are some Gradle projects that demonstrate typical use cases with and features available in the Spring Boot JSON-RPC Starter:

For adding methods to Swagger don't forget to enable swagger profile for your application.

In these projects Swagger's documentation looks like this:

Swagger's documentation

Information of JSON-RPC method

Trying JSON-RPC method


About


Languages

Language:Kotlin 93.8%Language:HTML 3.4%Language:JavaScript 1.9%Language:Java 0.6%Language:CSS 0.3%