Advertisement
andrzejiwaniuk

REST Web Service/Client JSON send data over HTTP to Rest

Jan 18th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. /*
  2.  
  3. REST service and JSON
  4.  
  5. */
  6. package tedoweb1;
  7. import java.io.BufferedReader;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10.  
  11. import javax.print.attribute.standard.Media;
  12. import javax.ws.rs.Consumes;
  13. import javax.ws.rs.GET;
  14. import javax.ws.rs.POST;
  15. import javax.ws.rs.Path;
  16. import javax.ws.rs.Produces;
  17. import javax.ws.rs.core.MediaType;
  18. import javax.ws.rs.core.Response;
  19.  
  20. @Path("/")
  21. public class RESTService {
  22.     @POST
  23.     @Path("/tedoRestService")
  24.     @Consumes(MediaType.APPLICATION_JSON)
  25.     public Response crunchifyREST(InputStream incomingData) {
  26.         StringBuilder textBuilder= new StringBuilder();
  27.         try {
  28.             BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
  29.             String line = null;
  30.             while ((line = in.readLine()) != null) {
  31.                 textBuilder.append(line);
  32.             }
  33.         } catch (Exception e) {
  34.             System.out.println("Error Parsing: - ");
  35.         }
  36.         System.out.println("Dane otrzymane: " + textBuilder.toString());
  37.  
  38.         // return HTTP response 200 in case of success
  39.         return Response.status(200).entity(textBuilder.toString()).build();
  40.     }
  41.    
  42.     @GET
  43.     @Path("/sprawdz")
  44.     @Produces(MediaType.TEXT_PLAIN)
  45.     public Response verifyRESTService(InputStream incomingData) {
  46.         String result = "Tedo Tech Rest usluga - uruchomiona";
  47.  
  48.         // return HTTP response 200 in case of success
  49.         return Response.status(200).entity(result).build();
  50.     }
  51. }
  52.  
  53. /*
  54.  
  55. REST client and JSON
  56. get values from file and send to the Rest web Service
  57.    
  58. */
  59. package tedoweb1;
  60.  
  61. import java.io.BufferedReader;
  62. import java.io.FileInputStream;
  63. import java.io.InputStream;
  64. import java.io.InputStreamReader;
  65. import java.io.OutputStreamWriter;
  66. import java.net.URL;
  67. import java.net.URLConnection;
  68.  
  69. import org.json.JSONObject;
  70.  
  71. public class RESTServiceClient {
  72.  
  73.     public static void main(String[] args) {
  74.         String string = "";
  75.         try {
  76.  
  77.             // Step1: Let's 1st read file from fileSystem
  78.             // Change CrunchifyJSON.txt path here
  79.             //nie dziala sieciowe mapowanie
  80.             //InputStream crunchifyInputStream = new FileInputStream("\\192.168.1.2\\JSON\\data.txt");
  81.             InputStream crunchifyInputStream = new FileInputStream("C://JSON//data.json");
  82.             InputStreamReader crunchifyReader = new InputStreamReader(crunchifyInputStream);
  83.             BufferedReader br = new BufferedReader(crunchifyReader);
  84.             String line;
  85.             while ((line = br.readLine()) != null) {
  86.                 string += line + "\n";
  87.             }
  88.  
  89.             JSONObject jsonObject = new JSONObject(string);
  90.             System.out.println(jsonObject);
  91.  
  92.             // Step2: Now pass JSON File Data to REST Service
  93.             try {
  94.                 URL url = new URL("http://localhost:8100/tedoweb1/api/tedoRestService");
  95.                 URLConnection connection = url.openConnection();
  96.                 connection.setDoOutput(true);
  97.                 connection.setRequestProperty("Content-Type", "application/json");
  98.                 connection.setConnectTimeout(5000);
  99.                 connection.setReadTimeout(5000);
  100.                 OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
  101.                 out.write(jsonObject.toString());
  102.                 out.close();
  103.  
  104.                 BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  105.  
  106.                 while (in.readLine() != null) {
  107.                 }
  108.                 System.out.println("\nService Invoked Successfully..");
  109.                 in.close();
  110.             } catch (Exception e) {
  111.                 System.out.println("\nError while calling REST Service");
  112.                 System.out.println(e);
  113.             }
  114.  
  115.             br.close();
  116.         } catch (Exception e) {
  117.             e.printStackTrace();
  118.         }
  119.     }
  120.  
  121. }
  122.  
  123. /*
  124.      JSON file (data.json)
  125.  
  126. */
  127.  
  128. {
  129.     "data: {
  130.        "id": "1",
  131.        "topic": "data1",
  132.        "description": "Andrzej Iwaniuk"
  133.    }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement