In large organization, it can be common to share the data between different systems.
For example, the first system get the “Not authorized transactions” from the other system, and authorize them.
This example shows communicating two different websites through Web Service. The each website uses the different Database. Moreover, they are different kind of database; RDB and NoSQL DB.
They are completely different system but they can share the same data.
- URL: http://localhost:8080/ns2015mvcmongo001
- DB: Mongo DB
- Send a request to Website 2, and received the response (JSON) and store them in Mongo DB.
- URL: http://localhost:8080/NS2015V07
- DB: My SQL
- Receive the request from Website 1, and get the data from DB (My SQL) and send a response (JSON object) back to Website 1.
Summarized (Three steps) as two websites are communicating through HTTP:
- The Website 1 request the Return transactions (RMAs) which are not “Authorized” to Website 2.
- The Website 2 response to the Website 1 with the transactions (in JSON object)
- The Website 2 stored them in Mongo DB
From: http://localhost:8080/ns2015mvcmongo001/welcome_01.html
Controller (Website 1) calls Request to (Website 2):
<input type="submit" value="WebService" name="webService" />
Button in JSP
@RequestMapping(value = "/welcome_01", params = "webService", method = RequestMethod.POST) public ModelAndView refreshByWebService() { ModelAndView modelAndView = new ModelAndView("welcome_01"); List<JSONObject> jsonList = getRmaHdrList(); saveRma(jsonList); List<RMA_HDR> rmaList = rmaRep.findByRma_exclude("auth"); modelAndView.addObject("rmaList", rmaList); return modelAndView; }
Step 1.1. Calls “getRmaHdrList()” method
private List<JSONObject> getRmaHdrList() { String output = getJsonStrByURL(this.url); List<JSONObject> list = new ArrayList<JSONObject>(); try { JSONParser parser = new JSONParser(); Object obj = parser.parse(output); JSONArray array = (JSONArray) obj; for (int i = 0; i < array.size(); i++) { JSONObject json = (JSONObject) array.get(i); list.add(json); Long id = (Long) json.get("id"); String rmaNum = (String) json.get("rmaNum"); String rmaHdrStsCd = (String) json.get("rmaHdrStsCd"); System.out.println("id: " + id); System.out.println("rmaNum: " + rmaNum); System.out.println("rmaHdrStsCd: " + rmaHdrStsCd); } } catch (Exception e) { e.printStackTrace(); } return list; }
Step 1.2. Calls “getJsonStrByURL(String url)” method
private String getJsonStrByURL(String url) { Client client = Client.create(); WebResource response = client.resource("http://localhost:8080/NS2015V07/ns-home/json"); ClientResponse clientRes = response.accept("application/json").get(ClientResponse.class); if (clientRes.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + clientRes.getStatus()); } return clientRes.getEntity(String.class); }
Step 1.3. Calls Website 2: “http://localhost:8080/NS2015V07/ns-home/json”
From: http://localhost:8080/NS2015V07/ns-home/json
Controller in Website 2 received a request and send a response back to Website 1:
@RequestMapping(value = "/ns-home/json", method = RequestMethod.GET) public @ResponseBody List<RmaHdrModel> getAll(HttpServletRequest req) { List<RmaHdrModel> list = RmaBL.getRmaHdrListWCdTblNm(req); List<JSONObject> entities = new ArrayList<JSONObject>(); for (RmaHdrModel rma : list) { JSONObject jsonObject = new JSONObject(); jsonObject.put("id", rma.getId()); jsonObject.put("rmaNum", rma.getRmaNum()); jsonObject.put("rmaHdrStsCd", rma.getRmaHdrStsCd()); entities.add(jsonObject); } return list; }
Step 2.1. At line 3, get the record from DB (MySQL).
Step 2.2. At line 6 to 10, create a JSON object and being set to a list.
Step 2.3. At line 12, return a list (JSON) as a response to: http://localhost:8080/ns2015mvcmongo001/welcome_01.html