简体   繁体   中英

How to return JsonObject properly? | JsonRPC-API with SpringBoot

I'm trying to make JsonRPC-API working with Springboot.

I'm using briandilley/jsonrpc4j library.

link : https://github.com/briandilley/jsonrpc4j

I created a ApplicationConfig class and bind to Inteface by @JsonRpcService("/path") annotation like below.

ApplicationConfig.class

import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImplExporter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ApplicationConfig {

    @Bean
    public static AutoJsonRpcServiceImplExporter autoJsonRpcServiceImplExporter() {
        AutoJsonRpcServiceImplExporter exp = new AutoJsonRpcServiceImplExporter();
        //in here you can provide custom HTTP status code providers etc. eg:
        //exp.setHttpStatusCodeProvider();
        //exp.setErrorResolver();
        return exp;
    }
}

API Interface

import com.googlecode.jsonrpc4j.JsonRpcParam;
import com.googlecode.jsonrpc4j.JsonRpcService;

import java.util.ArrayList;

@JsonRpcService("/api/test")
public interface testApi {
    JsonObject test();
}

Here is the class that I implemented the Interface to execute actual logic and return JsonObject.

testApiImpl.class

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.gson.JsonObject;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@AutoJsonRpcServiceImpl
public class testApiImpl implements testApi {
    @Override
    public JsonObject test() {
        JsonObject obj = new JsonObject();
        obj.addProperty("id", "0");
        obj.addProperty("name", "rachael");
        obj.addProperty("age", "27");
        return obj;
    }
}

This is a just simple test to check that jsonObject is actually returned properly. I simply created a JsonObject and returned it. Maybe I thought it's too simple.

curl -sH "Content-Type:application/json" -d '{"id":"1","jsonrpc":"2.0","method":"test","params":{}}' http://localhost:8080/api/test

I got an error like this when I call the method by Curl.

{"jsonrpc":"2.0","id":"1","error":{"code":-32001,"message":"JsonObject (through reference chain: com.google.gson.JsonObject[\\"asLong\\"])","data":{"exceptionTypeName":"java.lang.IllegalArgumentException","message":"JsonObject (through reference chain: com.google.gson.JsonObject[\\"asLong\\"])"}} }

I am new to Java and SpringBoot also so I'm sure there is another way to return JsonObject and set the error code and message. How can I set state code (ie error code) and message and return JsonObject successfully?

Jackson is supported for org.json.JSONObject & org.json.JSONArray . So if you try using this package object, it should be working fine. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM