RESTEasy通过ApacheAPI或JavaAPI进行TEXT、XML、JSON数据交换

RESTEasy通过ApacheAPI或JavaAPI进行HTTP协议的TEXT、XML、JSON数据交换

 

服务器端代码:

    @POST
    @Path("format/text")
    @Consumes(MediaType.TEXT_PLAIN)
    public String text(String input) {
        System.out.println(input);
        return input;
    }
    
    @GET
    @POST
    @Path("format/xml")
    @Produces(MediaType.APPLICATION_XML)
    @Consumes(MediaType.APPLICATION_XML)
    public MyCustomer xml(MyCustomer input) {
        System.out.println(input);
        return input;
    }
    
    @POST
    @Path("format/json")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public MyCustomer json(MyCustomer input) {
        System.out.println(input);
        return input;
    }

 

ApacheAPI:

    @Test
    public void testJavaNetString() throws Exception {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://localhost:8080/resteasy-json/resteasy/body/format/text");
        StringEntity entity = new StringEntity("Hello Jerval美国!", "UTF-8");//
        entity.setContentType("text/plain");
        post.setEntity(entity);
        HttpClientParams.setRedirecting(post.getParams(), false);
        HttpResponse response = client.execute(post);
        System.out.println("Content-Type: " + response.getEntity().getContentType());
        HttpEntity entityReturn = response.getEntity();
        BufferedReader reader = new BufferedReader(new InputStreamReader(entityReturn.getContent()));
        String line = reader.readLine();
        while (line != null) {
            System.out.println(line);
            line = reader.readLine();
        }
    }
    
    @Test
    public void testJavaNetXml() throws Exception {
        String newCustomer = "<mycustomer><firstName>Jerval</firstName><lastName>Burke</lastName><street>256 Clarendon Street中文呢</street></mycustomer>";
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://localhost:8080/resteasy-json/resteasy/body/format/xml");
        StringEntity entity = new StringEntity(newCustomer, "UTF-8");
        entity.setContentType("application/xml");
        post.setEntity(entity);
        HttpClientParams.setRedirecting(post.getParams(), false);
        HttpResponse response = client.execute(post);
        System.out.println("Content-Type: " + response.getEntity().getContentType());
        HttpEntity entityReturn = response.getEntity();
        BufferedReader reader = new BufferedReader(new InputStreamReader(entityReturn.getContent()));
        String line = reader.readLine();
        while (line != null) {
            System.out.println(line);
            line = reader.readLine();
        }
    }

    @Test
    public void testJavaNetJson() throws Exception {
        String newCustomer = "{'mycustomer':{'firstName':'Jerval222','id':1,'lastName':'Burke222中文','street':'263 Clarendon Street'}}";
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://localhost:8080/resteasy-json/resteasy/body/format/json");
        StringEntity entity = new StringEntity(newCustomer, "UTF-8");//
        entity.setContentType("application/json");
        post.setEntity(entity);
        HttpClientParams.setRedirecting(post.getParams(), false);
        HttpResponse response = client.execute(post);
        System.out.println("Content-Type: " + response.getEntity().getContentType());
        HttpEntity entityReturn = response.getEntity();
        BufferedReader reader = new BufferedReader(new InputStreamReader(entityReturn.getContent()));
        String line = reader.readLine();
        while (line != null) {
            System.out.println(line);
            line = reader.readLine();
        }
    }

 

JavaAPI:

    @Test
    public void testJavaNetString2() throws Exception {
        String str = "Hello Jerval美国!";
        URL postUrl = new URL("http://localhost:8080/resteasy-json/resteasy/body/format/text");
        HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
        connection.setDoOutput(true);
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "text/plain");
        OutputStream os = connection.getOutputStream();
        os.write(str.getBytes("UTF-8"));
        os.flush();
        InputStream is = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line = reader.readLine();
        while (line != null) {
            System.out.println(line);
            line = reader.readLine();
        }
        System.out.println(connection.getResponseCode());
        connection.disconnect();
    }

    @Test
    public void testJavaNetXml2() throws Exception {
        String newCustomer = "<mycustomer><firstName>Jerval</firstName><lastName>Burke美国</lastName><street>256 Clarendon Street</street></mycustomer>";
        URL postUrl = new URL("http://localhost:8080/resteasy-json/resteasy/body/format/xml");
        HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
        HttpURLConnection.setFollowRedirects(false);
        connection.setDoOutput(true);
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Content-Type", "application/xml");
        OutputStream os = connection.getOutputStream();
        os.write(newCustomer.getBytes("UTF-8"));
        os.flush();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line = reader.readLine();
        while (line != null) {
            System.out.println(line);
            line = reader.readLine();
        }
        System.out.println(connection.getResponseCode());
        connection.disconnect();
    }
    
    @Test
    public void testJavaNetJson2() throws Exception {
        String newCustomer = "{'mycustomer':{'firstName':'Jerval222','id':1,'lastName':'Burke222中文','street':'263 Clarendon Street'}}";
        URL postUrl = new URL("http://localhost:8080/resteasy-json/resteasy/body/format/json");
        HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
        HttpURLConnection.setFollowRedirects(false);
        connection.setDoOutput(true);
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Content-Type", "application/json");
        OutputStream os = connection.getOutputStream();
        os.write(newCustomer.getBytes("UTF-8"));
        os.flush();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line = reader.readLine();
        while (line != null) {
            System.out.println(line);
            line = reader.readLine();
        }
        System.out.println(connection.getResponseCode());
        connection.disconnect();
    }

 

 

你可能感兴趣的:(resteasy)