How to connect to a middleware in java to do POST, GET and PUT with JSON? [closed]

-2

they ask me to do POST, GET and PUT with a JSON to a middleware server, they gave me a URL to make my connection and send a JSON to then do the respective GET yPUT, the POST, GET and PUT have an address every one For example: POST / Net / Person

The problem is that I have no idea how to do this, I do not want to put all the code I just want to know where I should start looking or what I can do to do it.

Until now I have seen that it has to be done with maven and jersey, but I do not know if this really will work or not.

    
asked by mrkillmer 27.09.2018 в 13:16
source

1 answer

0

Examples of RESTful Web Service consumption in java:

public void listarJson(){
        String urlstr="http://url/restful/empleados/json/listarempleados";
        try{
            URL url = new  URL(urlstr);
            InputStream is = url.openStream();
            JsonReader rdr=Json.createReader(new InputStreamReader(is, "UTF-8"));
            JsonArray results= rdr.readArray();
            Iterator<?> iterator = results.iterator();
            while(iterator.hasNext()){
                JsonObject jsonObject = (JsonObject) iterator.next();
                System.out.println("JSON-> El id es: "+jsonObject.get("id").toString().toUpperCase()+", el nombre es: "+jsonObject.get("nombre").toString().toUpperCase()+", el puesto es: "+jsonObject.get("puesto").toString().toUpperCase());
            }
        }catch (Exception e) {
            e.getMessage();
            e.printStackTrace();
        }
    }
    public void insertar(){
        BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
        try {
            System.out.println("Ingrese el nuevo Id: ");
            String id = br.readLine();
            System.out.println("Ingrese el nuevo nombre: ");
            String nombre = br.readLine();
            System.out.println("Ingrese el nuevo puesto: ");
            String puesto = br.readLine();
            String url="http://url/restful/empleados/insertar/"+URLEncoder.encode(id,"UTF-8")+"/"+URLEncoder.encode(nombre,"UTF-8")+"/"+URLEncoder.encode(puesto,"UTF-8");
            URL urlObj=new URL(url);
            HttpURLConnection conexion=(HttpURLConnection)urlObj.openConnection();
            conexion.setRequestMethod("PUT");
            conexion.connect();
            System.out.println(conexion.getResponseMessage());
            conexion.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void borrar(){
        BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
        try {
            System.out.println("Ingrese el id a borrar: ");
            String id = br.readLine();
            String url="http://url/restful/empleados/borrar/"+URLEncoder.encode(id,"UTF-8");
            URL urlObj=new URL(url);
            HttpURLConnection conexion=(HttpURLConnection)urlObj.openConnection();
            conexion.setRequestMethod("DELETE");
            conexion.connect();
            System.out.println(conexion.getResponseMessage());
            conexion.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
answered by 27.09.2018 / 17:18
source