SPRINGMVC create public interface corresponding server and client

Source: Internet
Author: User
Tags parent directory readfile log4j

1. Interface Service side

1.1.SPRINGMVC build a good frame.

1.2.Controller (Control Class) create "Pubhealthcontroller"

Package com.oracle.sx.businessData.action;

Import java.io.IOException;
Import Java.io.PrintWriter;

Import Javax.annotation.Resource;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.log4j.Logger;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.ResponseBody;

Import Com.alibaba.fastjson.JSONObject;
Import Com.common.util.CommonConst.METHOD_NAME;
Import Com.oracle.sx.basicData.service.IDepartmentInfoService;
Import Com.oracle.sx.basicData.service.IHoldInfoService;
Import Com.oracle.sx.basicData.service.IMechanismInfoService;
Import Com.oracle.sx.basicData.service.IPersonService;
Import Com.oracle.sx.businessData.service.IChildHealthCheckService;
Import Com.oracle.sx.businessData.service.IChildrenHealthCareService;
Import Com.oracle.sx.businessData.service.IChronicDiseaseRecordService;
Import Com.oracle.sx.businessData.service.IHealthAssessmentService;
Import Com.oracle.sx.businessData.service.IHealthEducationRecordService;
Import Com.oracle.sx.businessData.service.IHealthRecordInfoService;
Import Com.oracle.sx.businessData.service.IPsychiatricManagementService;
Import Com.oracle.sx.businessData.service.IWomenHealthCareService;

/**
* Public Control class
*
* @author Administrator
*
*/
@Controller
@RequestMapping ("/jsp")

public class Pubhealthcontroller {
Private static final Logger log = Logger.getlogger (Pubhealthcontroller.class);
@Resource
Private Imechanisminfoservice Mechanisminfoservice; Agency Service class
@Resource
Private Ichildrenhealthcareservice Childrenhealthcareservice; Child health

@RequestMapping (value = "Invoke", method = Requestmethod.post)
@ResponseBody
public void Invoke (HttpServletRequest request, httpservletresponse response) {
try {
Response.setcontenttype ("Application/json; Charset=utf-8 ");
Response.setcharacterencoding ("UTF-8");
String Objjson = Request.getparameter ("JSON");
Log.info ("entry method ...");
Log.info ("Front-End JSON:" + Objjson);
String jsonstr = This.getdata (Objjson);
Log.info ("Return JSON:" + jsonstr);
PrintWriter out = Response.getwriter ();
Out.write (JSONSTR);
catch (IOException e) {
E.printstacktrace ();
Log.info ("Interface exception information:" + e);
}
}

/**
* Parse client-acquired JSON data
*
* @param Objectjson
* Client incoming JSON data
*/
public string GetData (string Objectjson) {
String objstr = "";
try {
Converts a JSON string to a JSON object
Jsonobject jsonobj = new Jsonobject ();
Jsonobj = (jsonobject) jsonobject.parse (Objectjson);
Jsonobj.get ("method");
Log.info ("Method:" + Jsonobj.get ("method"));
String method = Jsonobj.get (' method '). toString (); Interface Method Name
String param = jsonobj.get ("param"). toString (); interface methods correspond to arguments
if (Method.equals (method_name). Add_nb_family_visit)) {
New family visits for newborns
Jsonobject obj = (jsonobject) jsonobject.parse (param);
Objstr = Childrenhealthcareservice.addnbfamilyvisit (obj);
Log.info ("New neonatal family visit return parameter:" + objstr);
else if (Method.equals (method_name). Get_nb_family_visit)) {
Survey of neonatal family visit records
Jsonobject obj = (jsonobject) jsonobject.parse (param);
Objstr = Childrenhealthcareservice.getnbfamilyvisit (obj);
Log.info ("Neonatal family visit Records query return parameters:" + objstr);
else if (Method.equals (method_name). Get_nb_family_visits_by_date)) {
Neonatal family visit record (time period query)
Jsonobject obj = (jsonobject) jsonobject.parse (param);
Objstr = childrenhealthcareservice.getnbfamilyvisitsbydate (obj);
Log.info ("Neonatal family visit record (time period query) return parameter:" + objstr);
else if (Method.equals (method_name). Add_hos)) {
New agency Information
Jsonobject obj = (jsonobject) jsonobject.parse (param);
Objstr = Mechanisminfoservice.addhos (obj);
Log.info ("New information return Parameter:" + objstr);
}
Log.info ("Method:" + method);
} catch (

Exception e) {
Log.error ("Get Front-End JSON Data error:" + E);
}
return objstr;
}
}

1.3. Other structures are exactly the same as SPRINGMVC structures. The service end is completed.


2. Client Build

2.1. Create File File1.json

{
"Plat": "B47ffabc998547e4bbe46b92eb8aab06",
"Method": "Getbaseinfomessagecardsbydate",
"param": {
"Modifytimestart": "2017-05-01",
"Modifytimeend": "2017-05-21",
"Shownum": "10",
"Page": "1"
}
}

2.2. Create Readjsonfile class Read JSON file

Package com.test.JsonFile;

Import Java.io.BufferedReader;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;

Import Com.alibaba.fastjson.JSONObject;

public class Readjsonfile {
public static string ReadFile (String Path) {
String RootPath = ReadJsonFile.class.getResource (""). GetFile (). toString ();
BufferedReader reader = null;
String laststr = "";
try {
FileInputStream FileInputStream = new FileInputStream (RootPath + Path);
InputStreamReader InputStreamReader = new InputStreamReader (FileInputStream, "utf-8");
reader = new BufferedReader (InputStreamReader);
String tempstring = null;
while ((tempstring = Reader.readline ())!= null) {
Laststr + = tempstring;
}
Reader.close ();
catch (IOException e) {
E.printstacktrace ();
finally {
if (reader!= null) {
try {
Reader.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
}
Jsonobject jsonobj = new Jsonobject ();
Jsonobj = (jsonobject) jsonobject.parse (LASTSTR);
return jsonobj.tojsonstring ();
}

public static void Main (string[] args) {
To get the root directory path
String RootPath = ReadJsonFile.class.getResource (""). GetFile (). toString ();
Current directory path
String Currentpath1=getclass (). GetResource ("."). GetFile (). toString ();
String Currentpath2=getclass (). GetResource (""). GetFile (). toString ();
Parent directory path for current directory
String Parentpath=getclass (). GetResource (".. /"). GetFile (). toString ();
System.out.println (RootPath);
}
}

2.3. Create read service-side URL class

Package com.test;

Import Java.util.HashMap;
Import Java.util.Map;

Import Org.apache.log4j.Logger;

Import Com.alibaba.fastjson.JSONObject;
Import Com.test.JsonFile.ReadJsonFile;

/**
* Interface Public method
*
* @author Administrator
*/
Public abstract class Interfacemethod {
Private static final Logger log = Logger.getlogger (Interfacemethod.class);

@SuppressWarnings ("Unchecked")
public static void Addnbfamilyvisit () {
String datastr = Readjsonfile.readfile ("File1.json");
String datastr = Readjsonfile.readfile ("Orginfo.json");
String datastr = Readjsonfile.readfile ("Health.json");
String datastr = Readjsonfile.readfile ("Women8.json");
try {
map<string, object> jsonmap = new hashmap<string, object> ();
Jsonobject object = new Jsonobject ();

String sr = httprequest.sendpost ("Http://127.0.0.1:8080/EHR/jsp/invoke.do", "json=" +datastr);
SYSTEM.OUT.PRINTLN ("Call return Parameter:" + sr);

catch (Exception e) {
Log.error ("Test interface Error:" + E);
}
}

public static void Main (string[] args) {
Addnbfamilyvisit ()//new neonatal family visit record test
}
}

3. The boot client can be used for testing at the start of the server.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.