users@jersey.java.net

[Jersey] Re: Message Body Writer

From: Rimy Sfm <atitallah.rim_at_gmail.com>
Date: Wed, 13 Apr 2011 03:57:03 -0700 (PDT)

The method declaration:

        @Path("/add")
        @POST
        
        public String add(@FormParam("x")String x,_at_FormParam("y")String y){
                
                String s = String.valueOf(Integer.parseInt(x)+Integer.parseInt(y));
           return s;}

The client code:


package testClient;


import java.net.URI;

//import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.MultivaluedMap;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.core.util.MultivaluedMapImpl;

public class test {
        public static void main(String[] args) {
                ClientConfig config = new DefaultClientConfig();
                Client client = Client.create(config);
                WebResource service = client.resource(getBaseURI());
                // Get plain text
                System.out.println(service.path("hello").accept(
                                MediaType.TEXT_PLAIN).get(String.class));
                
                
                // Get XML
                System.out.println(service.path("hello").accept(
                                MediaType.TEXT_XML).get(String.class));
                // The HTML
                System.out.println(service.path("hello").accept(
                                MediaType.TEXT_HTML).get(String.class));
                
        
                //POST
                 MultivaluedMap&lt;String, String&gt; formData = new MultivaluedMapImpl();
                  
                formData.add("x", "10");
                formData.add("y", "2");
                 
                  ClientResponse response =
service.path("/hello").path("/add").post(ClientResponse.class,formData);
                  
                 // EntityTag e = response.getEntityTag();
                  String entity = response.getEntity(String.class);
                  System.out.println (response);
                  System.out.println (entity);
                  
              

                                /*MultivaluedMapImpl formDataInt = new MultivaluedMapImpl();
                                  
                                        formDataInt.add("a", 2);
                                        formDataInt.add("b", 3);
                                        ClientResponse responseInt =
service.path("hello/addInt").post(ClientResponse.class,formDataInt);
                                        String entityInt = responseInt.getEntity(String.class);
                                        System.out.println (responseInt);
                                        System.out.println (entityInt);*/
              
        }
        
        
        private static URI getBaseURI() {
                return UriBuilder.fromUri(
                                "http://localhost:8080/exemple.jersey.first/rest/").build();
        }

}


--
View this message in context: http://jersey.576304.n2.nabble.com/Message-Body-Writer-tp6268231p6268493.html
Sent from the Jersey mailing list archive at Nabble.com.