티스토리 뷰
SpringFramework JSON-RPC (jsonrpc4j) 사용법.
XML-RPC방식과 비슷한 형태의 JSON-RPC 통신 방법에 대해 알아보고자 한다.
일단 JSON RPC 기본 규격부터 살펴 보자.
Request
jsonrpc : json-rpc 스펙을 정의한다.
method : 실행할 메소드의 타입을 포함한다.
params : 메소드 실행 시 사용하는 파라미터 값을 포함한다.
id : 통신의 식별값을 설정, String, Number, Null값이라도 상관 없다.
예) {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}
Response
jsonrpc : json-rpc 스펙을 정의한다.
result : 요청 성공 시 결과 값을 포함한다.
error : 요청 에러 시, 에러 메세지, 코드값을 포함하여 Return한다.
id : 통신의 식별값을 설정, String, Number, Null값이라도 상관 없다.
성공 예) {"jsonrpc": "2.0", "result": 19, "id": 4}
에러 예) {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}
출처 : http://www.jsonrpc.org/specification
이제 설정 방법에 대해 알아보자.
환경
Spring Framework (Web MVC) : 3.2
JSON-RPC : jsonrpc4j : 1.1
maven : 3.0.4
step 1. pop.xml 파일 추가
jsonrpc4j를 이용하여 구현하였다.
pom.xml
<!-- jsonrpc4j --> <dependency> <groupId>com.github.briandilley.jsonrpc4j</groupId> <artifactId>jsonrpc4j</artifactId> <version>1.1</version> </dependency>
step 2. service 객체 생성
인증 메서드 예제를 만들어 보도록 한다.
AuthServiceImpl.java
package kr.co.rocksea.auth.service; import kr.co.rocksea.auth.vo.User; public class AuthServiceImpl implements AuthService { public User AUTH(String installerId, String installerPw, String serialCode, String mac) throws Exception { User user = new User(); user.setInstallerId(installerId); user.setInstallerPw(installerPw); user.setSerialCode(serialCode); user.setMac(mac); if(user.getInstallerId().equals("rocksea")){ throw new Exception(""); } return user; }
}
AuithService.java
package kr.co.rocksea.auth.service; import com.googlecode.jsonrpc4j.JsonRpcError; import com.googlecode.jsonrpc4j.JsonRpcErrors; import com.googlecode.jsonrpc4j.JsonRpcParamName; import com.googlecode.jsonrpc4j.JsonRpcService; import kr.co.rocksea.auth.vo.User; @JsonRpcService("JSON-RPC") public interface AuthService { @JsonRpcErrors({ @JsonRpcError(exception=Exception.class, code=-5678, message="User already exists", data="The Data"), @JsonRpcError(exception=Throwable.class,code=-187) }) User AUTH(@JsonRpcParamName("installerId") String installerId, @JsonRpcParamName("installerPw") String installerPw, @JsonRpcParamName("serialCode") String serialCode, @JsonRpcParamName("mac") String mac) throws Exception; }
User.java
package kr.co.rocksea.auth.vo; public class User { String installerId; String installerPw; String serialCode; String mac; public String getInstallerId() { return installerId; } public void setInstallerId(String installerId) { this.installerId = installerId; } public String getInstallerPw() { return installerPw; } public void setInstallerPw(String installerPw) { this.installerPw = installerPw; } public String getSerialCode() { return serialCode; } public void setSerialCode(String serialCode) { this.serialCode = serialCode; } public String getMac() { return mac; } public void setMac(String mac) { this.mac = mac; } }
step 3. json-rpc 환경 설정
인증 서비스 객체 bean을 등록 설정을 한다.
json-rpc-config.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean class="com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceExporter"/> <bean class="kr.co.rocksea.auth.service.AuthServiceImpl" /> </beans>
web.xml
<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:json-rpc-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
step 4. 테스트
Chrome의 Postman등의 Client 프로그램을 이용하여 테스트 해본다.
[그림 1] JSON-RPC 결과화면
'Developer' 카테고리의 다른 글
[SpringSecurity#1] How to use SpringSecurity (0) | 2015.07.24 |
---|---|
[ORM#1] Getting started with Hibernate (0) | 2015.07.23 |
[Django#3] Getting started with simple application (2) | 2015.07.10 |
[Django#2] Getting started with simple application (0) | 2015.07.02 |
[Django#1] How to use Django on Ubuntu (2) | 2015.06.25 |
- Total
- Today
- Yesterday
- Python Django
- hadoop
- 여행
- JBOSS
- maven
- 비지니스 영어
- 영문법
- it
- k8s
- NGINX
- 비교구문
- ubuntu
- 스페인 여행
- 조동사
- nodejs
- 영작
- 가정법
- 해외여행
- redis
- hdfs
- Python
- 다낭
- 도덕경
- Business English
- 베트남
- PostgreSQL
- mongoDB
- memcached
- AWS
- 대명사 구문
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |