Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- REST service and JSON
- */
- package tedoweb1;
- import java.io.BufferedReader;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import javax.print.attribute.standard.Media;
- import javax.ws.rs.Consumes;
- import javax.ws.rs.GET;
- import javax.ws.rs.POST;
- import javax.ws.rs.Path;
- import javax.ws.rs.Produces;
- import javax.ws.rs.core.MediaType;
- import javax.ws.rs.core.Response;
- @Path("/")
- public class RESTService {
- @POST
- @Path("/tedoRestService")
- @Consumes(MediaType.APPLICATION_JSON)
- public Response crunchifyREST(InputStream incomingData) {
- StringBuilder textBuilder= new StringBuilder();
- try {
- BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
- String line = null;
- while ((line = in.readLine()) != null) {
- textBuilder.append(line);
- }
- } catch (Exception e) {
- System.out.println("Error Parsing: - ");
- }
- System.out.println("Dane otrzymane: " + textBuilder.toString());
- // return HTTP response 200 in case of success
- return Response.status(200).entity(textBuilder.toString()).build();
- }
- @GET
- @Path("/sprawdz")
- @Produces(MediaType.TEXT_PLAIN)
- public Response verifyRESTService(InputStream incomingData) {
- String result = "Tedo Tech Rest usluga - uruchomiona";
- // return HTTP response 200 in case of success
- return Response.status(200).entity(result).build();
- }
- }
- /*
- REST client and JSON
- get values from file and send to the Rest web Service
- */
- package tedoweb1;
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.net.URL;
- import java.net.URLConnection;
- import org.json.JSONObject;
- public class RESTServiceClient {
- public static void main(String[] args) {
- String string = "";
- try {
- // Step1: Let's 1st read file from fileSystem
- // Change CrunchifyJSON.txt path here
- //nie dziala sieciowe mapowanie
- //InputStream crunchifyInputStream = new FileInputStream("\\192.168.1.2\\JSON\\data.txt");
- InputStream crunchifyInputStream = new FileInputStream("C://JSON//data.json");
- InputStreamReader crunchifyReader = new InputStreamReader(crunchifyInputStream);
- BufferedReader br = new BufferedReader(crunchifyReader);
- String line;
- while ((line = br.readLine()) != null) {
- string += line + "\n";
- }
- JSONObject jsonObject = new JSONObject(string);
- System.out.println(jsonObject);
- // Step2: Now pass JSON File Data to REST Service
- try {
- URL url = new URL("http://localhost:8100/tedoweb1/api/tedoRestService");
- URLConnection connection = url.openConnection();
- connection.setDoOutput(true);
- connection.setRequestProperty("Content-Type", "application/json");
- connection.setConnectTimeout(5000);
- connection.setReadTimeout(5000);
- OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
- out.write(jsonObject.toString());
- out.close();
- BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- while (in.readLine() != null) {
- }
- System.out.println("\nService Invoked Successfully..");
- in.close();
- } catch (Exception e) {
- System.out.println("\nError while calling REST Service");
- System.out.println(e);
- }
- br.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- /*
- JSON file (data.json)
- */
- {
- "data: {
- "id": "1",
- "topic": "data1",
- "description": "Andrzej Iwaniuk"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement