From c5f431116c8e810c41f3bada07e9ca5b60b2d223 Mon Sep 17 00:00:00 2001 From: snoop Date: Tue, 11 Apr 2017 11:02:52 +0900 Subject: [PATCH] fixed output data -> json byte[] --- .../loafle/overflow/rpc/api/RemoteImpl.java | 30 +++++++++++++++++-- .../com/loafle/overflow/rpc/model/Output.java | 6 ++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/loafle/overflow/rpc/api/RemoteImpl.java b/src/main/java/com/loafle/overflow/rpc/api/RemoteImpl.java index 845f829..f1920de 100644 --- a/src/main/java/com/loafle/overflow/rpc/api/RemoteImpl.java +++ b/src/main/java/com/loafle/overflow/rpc/api/RemoteImpl.java @@ -1,6 +1,9 @@ package com.loafle.overflow.rpc.api; +import com.fasterxml.jackson.core.JsonEncoding; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.util.JSONPObject; import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl; import com.loafle.overflow.rpc.crawler.Crawler; import com.loafle.overflow.rpc.model.Input; @@ -40,8 +43,11 @@ public class RemoteImpl implements Remote { } else { Output out = new Output(); out.setStartDate(new Date()); + + Object obj = crawler.add(input.getId()); + out.setEndDate(new Date()); - out.setData(crawler.add(input.getId())); + out.setData(convertObjectToJsonBytes(obj)); return out; } @@ -74,8 +80,11 @@ public class RemoteImpl implements Remote { Output out = new Output(); out.setStartDate(new Date()); + + Object result = crawler.get(input.getId()); out.setEndDate(new Date()); - out.setData(crawler.get(input.getId())); + + out.setData(convertObjectToJsonBytes(result)); return out; } @@ -90,9 +99,24 @@ public class RemoteImpl implements Remote { } else { Output out = new Output(); out.setStartDate(new Date()); + + Object obj = crawler.remove(input.getId()); out.setEndDate(new Date()); - out.setData(crawler.remove(input.getId())); + out.setData(convertObjectToJsonBytes(obj)); return out; } } + + private byte[] convertObjectToJsonBytes(Object object) throws Exception { + + if(object == null) { + return null; + } + + ObjectMapper mapper = new ObjectMapper(); + + String jsonInString = mapper.writeValueAsString(object); + + return jsonInString.getBytes(); + } } \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/rpc/model/Output.java b/src/main/java/com/loafle/overflow/rpc/model/Output.java index 3e8dd33..8683a3d 100644 --- a/src/main/java/com/loafle/overflow/rpc/model/Output.java +++ b/src/main/java/com/loafle/overflow/rpc/model/Output.java @@ -8,7 +8,7 @@ import java.util.Date; public class Output { private Date startDate; private Date endDate; - private Object data; + private byte[] data; public Date getStartDate() { return startDate; @@ -26,11 +26,11 @@ public class Output { this.endDate = endDate; } - public Object getData() { + public byte[] getData() { return data; } - public void setData(Object data) { + public void setData(byte[] data) { this.data = data; } }