spring boot学习第十三篇:使用jsonrpc

因为我学习spring boot会有很多内容,所以和spring boot有关的文章使用的pom.xml和application.yml这2个文件的内容就全列出来,不用再单独抽出来了,单独抽出来有点麻烦也不方便学习。

1、pom.xml文件内容如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.4
         
    
    com.hmblogs
    hm-jsonRpc
    0.0.1-SNAPSHOT
    hm-jsonRpc
    hm-jsonRpc
    
        8
        1.2.8
        1.16
        7.9.2
    
    
        
        
            com.alibaba
            druid-spring-boot-starter
            ${druid.version}
        

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.3.1
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
        

        
            org.projectlombok
            lombok
            true
        

        
        
            mysql
            mysql-connector-java
            runtime
        

        
        
            org.projectlombok
            lombok
            true
        

        
        
            org.bgee.log4jdbc-log4j2
            log4jdbc-log4j2-jdbc4.1
            ${log4jdbc.version}
        

        
            com.alibaba
            fastjson
            1.2.9
        

        
            redis.clients
            jedis
        

        
            org.apache.kafka
            kafka-clients

        

        
            org.springframework.kafka
            spring-kafka

        

        
        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            ${es.version}
            
                
                    org.elasticsearch
                    elasticsearch
                
                
                    org.elasticsearch.client
                    elasticsearch-rest-client
                
            
        

        
        
            org.elasticsearch
            elasticsearch
            ${es.version}
        
        
            org.elasticsearch.client
            elasticsearch-rest-client
            ${es.version}
        
        
            junit
            junit
        

        
            org.springframework.boot
            spring-boot-starter-data-mongodb
        

        
            com.github.briandilley.jsonrpc4j
            jsonrpc4j
            1.5.3
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2、application.yml文件内容如下:

server:
  port: 8081
  servlet.context-path: /

#配置数据源
spring:
  datasource:
    druid:
      db-type: com.alibaba.druid.pool.DruidDataSource
      driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
      url: jdbc:log4jdbc:mysql://${DB_HOST:localhost}:${DB_PORT:3306}/${DB_NAME:eladmin}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
      username: ${DB_USER:root}
      password: ${DB_PWD:demo}
  redis:
    host: localhost
    port: 6379
    password: demo
    database: 10
  data:
    mongodb:
      host: 43.138.0.199
      port: 27017
      username: hmblogs
      password: demo
      database: hmblogs
      authentication-database: admin

es:
  host: 43.138.0.199
  port: 9200
  scheme: http
  user: elastic
  password: demo

3、启动类BackendApplication内容如下:

package com.hmblogs.backend;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BackendApplication {

    public static void main(String[] args) {
        SpringApplication.run(BackendApplication.class, args);
    }

}

4、接口类AcceptSignedAPI内容如下:

package com.hmblogs.backend.jsonRpc;

import com.googlecode.jsonrpc4j.JsonRpcService;

@JsonRpcService(value = "acceptSigned")
public interface AcceptSignedAPI {
    public void acceptSigned(Object o);
}

5、接口实现类AcceptSignedImpl内容如下:

package com.hmblogs.backend.jsonRpc;

import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import org.springframework.stereotype.Service;

@Service
@AutoJsonRpcServiceImpl
public class AcceptSignedImpl implements AcceptSignedAPI {

    @Override
    public void acceptSigned(Object o) {
        System.out.println("222"+o);
    }
}

6、工具类RpcClientUtil内容如下:

package com.hmblogs.backend.jsonRpc;

import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import java.net.URL;

/**
 * address:要送数据到哪个地址
 * params : json数据
 * methodName: 接口的方法名字
 */
public class RpcClientUtil {
    public static String postJsonRPC(String address, Object params, String methodName) throws Throwable {
        URL url = null;
        JsonRpcHttpClient jsonRpcHttpClient = null;
        String result = null;
        try {
            url = new URL(address);
            jsonRpcHttpClient = new JsonRpcHttpClient(url);
            result = jsonRpcHttpClient.invoke(methodName, params, String.class);
        } catch (Exception e) {
            throw e;
        }  finally {
            url = null;
            jsonRpcHttpClient = null;
        }
        return result;
    }

}

7、测试类RpcTest内容如下:

package com.hmblogs.backend.jsonRpc;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

public class RpcTest {
    @Test
    public void sendRpc() throws Throwable {

        JSONObject jsonO = new JSONObject();
        jsonO.put("name","hem");
        jsonO.put("sex","nan");
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonO);
        RpcClientUtil.postJsonRPC("http://127.0.0.1:8081/acceptSigned", "[{\"sex\":\"nan\",\"name\":\"1111\"}]","acceptSigned");
    }

}

然后报错了,文件结构、运行方法、报错整体截图如下:

spring boot学习第十三篇:使用jsonrpc_第1张图片

报错内容如下:

16:31:29,188 |-INFO in ch.qos.logback.classic.LoggerContext[logback] - Could NOT find resource [logback-test.xml]
16:31:29,188 |-INFO in ch.qos.logback.classic.LoggerContext[logback] - Found resource [logback.xml] at [file:/D:/workspace/hm-jsonRpc/target/classes/logback.xml]
16:31:29,248 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
16:31:29,254 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [file:/D:/workspace/hm-jsonRpc/target/classes/logback.xml] 
16:31:29,255 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 10 seconds
16:31:29,255 |-INFO in ch.qos.logback.classic.joran.action.ContextNameAction - Setting logger context name as [logback]
16:31:29,257 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
16:31:29,259 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [Console]
16:31:29,262 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
16:31:29,282 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
16:31:29,283 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [RollingFileBackend]
16:31:29,284 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
16:31:29,294 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1453774246 - No compression will be used
16:31:29,295 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1453774246 - Will use the pattern logs/hmblogs/log-hmblogs-%d{yyyy-MM-dd}.%i.log for the active file
16:31:29,297 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@18ce0030 - The date pattern is 'yyyy-MM-dd' from file name pattern 'logs/hmblogs/log-hmblogs-%d{yyyy-MM-dd}.%i.log'.
16:31:29,297 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@18ce0030 - Roll-over at midnight.
16:31:29,298 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@18ce0030 - Setting initial period to Mon Feb 19 16:15:00 CST 2024
16:31:29,298 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@18ce0030 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
16:31:29,298 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@18ce0030 - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
16:31:29,301 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[RollingFileBackend] - Active log file name: logs/hmblogs.log
16:31:29,301 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[RollingFileBackend] - File property is set to [logs/hmblogs.log]
16:31:29,302 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.springframework] to INFO
16:31:29,302 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [org.springframework] to false
16:31:29,302 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [Console] to Logger[org.springframework]
16:31:29,302 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [RollingFileBackend] to Logger[org.springframework]
16:31:29,303 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.mybatis] to INFO
16:31:29,303 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.hibernate.SQL] to DEBUG
16:31:29,303 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [org.hibernate.SQL] to false
16:31:29,303 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [Console] to Logger[org.hibernate.SQL]
16:31:29,303 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [RollingFileBackend] to Logger[org.hibernate.SQL]
16:31:29,303 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.apache.catalina] to INFO
16:31:29,303 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.apache.tomcat.util] to INFO
16:31:29,303 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
16:31:29,303 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [Console] to Logger[ROOT]
16:31:29,303 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [RollingFileBackend] to Logger[ROOT]
16:31:29,303 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
16:31:29,303 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@4445629 - Registering current configuration as safe fallback point


com.googlecode.jsonrpc4j.HttpException: stream is closed

	at com.googlecode.jsonrpc4j.JsonRpcHttpClient.invoke(JsonRpcHttpClient.java:161)
	at com.googlecode.jsonrpc4j.JsonRpcHttpClient.invoke(JsonRpcHttpClient.java:118)
	at com.googlecode.jsonrpc4j.JsonRpcHttpClient.invoke(JsonRpcHttpClient.java:176)
	at com.hmblogs.backend.jsonRpc.RpcClientUtil.postJsonRPC(RpcClientUtil.java:19)
	at com.hmblogs.backend.jsonRpc.RpcTest.sendRpc(RpcTest.java:16)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
 at [Source: (com.googlecode.jsonrpc4j.NoCloseInputStream); line: 1, column: 2]
	at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:2391)
	at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:735)
	at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:659)
	at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2737)
	at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._nextTokenNotInObject(UTF8StreamJsonParser.java:902)
	at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:794)
	at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4761)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4667)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3666)
	at com.googlecode.jsonrpc4j.ReadContext.nextValue(ReadContext.java:25)
	at com.googlecode.jsonrpc4j.JsonRpcClient.readResponseNode(JsonRpcClient.java:312)
	at com.googlecode.jsonrpc4j.JsonRpcClient.getValidResponse(JsonRpcClient.java:246)
	at com.googlecode.jsonrpc4j.JsonRpcClient.readResponse(JsonRpcClient.java:194)
	at com.googlecode.jsonrpc4j.JsonRpcClient.readResponse(JsonRpcClient.java:531)
	at com.googlecode.jsonrpc4j.JsonRpcHttpClient.invoke(JsonRpcHttpClient.java:159)
	... 31 more

怎么处理呢,搜了一圈资料没解决该问题。

你可能感兴趣的:(Spring,Boot,spring,boot,java)