Spring Boot 和 JSON-RPC for Java 快速入门
服务器
配置
要使整个系统正常工作,您需要在 @Configuration 类中定义 AutoJsonRpcServiceImplExporter bean:
package example.jsonrpc4j.springboot;
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;
}
}
服务
然后创建您的服务接口。我的示例是一个简单的计算器端点:
package example.jsonrpc4j.springboot.api;
import com.googlecode.jsonrpc4j.JsonRpcParam;
import com.googlecode.jsonrpc4j.JsonRpcService;
@JsonRpcService("/calculator")
public interface ExampleServerAPI {
int multiplier(@JsonRpcParam(value = "a") int a, @JsonRpcParam(value = "b") int b);
}
并像这样实现您的界面:
package example.jsonrpc4j.springboot.api;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import org.springframework.stereotype.Service;
@Service
@AutoJsonRpcServiceImpl
public class ExampleServerAPIImpl implements ExampleServerAPI {
@Override
public int multiplier(int a, int b) {
return a * b;
}
}
Spring CORS——全局CORS配置
实现 WebMvcConfigurer
要为整个应用程序启用 CORS,请使用WebMvcConfigurer
添加CorsRegistry
.
CorsConfiguration.java
package example.jsonrpc4j.springboot;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebMvc
public class CorsConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("GET", "POST");
}
}
你完成了!向您的服务器发送请求时,请注意设置内容类型标头至关重要,例如:
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script>
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({"id": "1", "jsonrpc": "2.0", "method": "multiplier", "params": {"a": 5, "b": 6}});
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("http://localhost:8080/calculator", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
</script>
</body>
</html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script>
fetch('http://localhost:8080/calculator', {
body: '{"id":"1","jsonrpc":"2.0","method":"multiplier","params":{"a":5,"b":6}}',
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
method: "POST"
}).then(response => response.json())
.then(data => console.log(data));
</script>
</body>
</html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script>
fetch('http://localhost:8080/calculator', {
body: '{"id":"1","jsonrpc":"2.0","method":"multiplier","params":{"a":5,"b":6}}',
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
method: "POST"
}).then(response => response.json())
.then(data => console.log(data.result));
</script>
</body>
</html>
curl -H "Content-Type:application/json" -d '{"id":"1","jsonrpc":"2.0","method":"multiplier","params":{"a":5,"b":6}}' http://localhost:8080/calculator
整个例子可以在这里找到